i wasnt sure where to post this so I posted it in programming

how would i go about coding a character select screen? do I turn a boolean to true and get that in another scene? if so how do i do that? i would love to get a few code examples but it is fine if not. thank you in advance!

I don't have the source code handy, so I am writing this mostly from memory, but I have made a character selection screen in a jam game I've made. I used an autoload script that, among other things, has a variable to store an integer for each of the character slots. Something like this:

var player_one_character = 0 # 0 = default character
var player_two_character = -1 # -1 = no character selected

If I remember correctly, I then used an array with preloaded scenes in the autoload script to hold all of the character choices:

var player_choices = [
	preload("res://character_one_scene_here.tscn"),
	preload("res://character_two_scene_here.tscn")
]

Then in each level, I have a spawn node that reads the values in the autoload script and instances/creates the correct character based on which was chosen:

extends Spatial
export (int, "player_one", "player_two") var player_spawner_id = 0
func _ready():
	# autoload script is called "globals" in this example
	# player one:
	if (player_spawner_id == 0):
		var player_one_char_idx = globals.player_one_character
		if (player_one_char_idx != -1):
			var player_one = globals.player_choices[player_one_char_idx].instance()
			# setup the player here!
			get_parent().add_child(player_one)
	# player two:
	if (player_spawner_id == 1):
		var player_two_char_idx = globals.player_two_character
		if (player_two_char_idx != -1):
			var player_two = globals.player_choices[player_two_char_idx].instance()
			# setup the player here!
			get_parent().add_child(player_two)

If I remember everything correctly, than that is more or less the whole thing :smile:

Because the game was a jam game, I was going for speed rather than scalability. A lot of what I wrote above could be turned mostly generic and converted into reusable functions, which would make it easier when working with a large volume of characters, players, or special situations (like networking).


Edit:

i wasnt sure where to post this so I posted it in programming

No worries! That works fine :+1:

@TwistedTwigleg if it isn't too much trouble would you mind explaining how the player_spawner_id variable works and how it relates to selecting the character? thank you in advance!

@takedabro1 - sure! Here's how it works:

You need to place the player spawner, which will be a node that create the players based on their selection. However, you need some way to know which player corresponds to each spawner, so you do not spawn players inside of each other, and it will also help with choosing the correct selected character for each player. To do this, you use the player_spawner_id variable, which you export so you can set its value from the Godot editor. This way, you can visually set which spawner corresponds to the spawn/starting-position for each player.

So, I would attach the player spawner (and script) to a Spatial or Node2D, depending on if the game is 3D or 2D respectively. Once placed and the same script attached to both, click another node and then click one of the spawners. You should find there is a section called "script variables" (or something like that) that will in the property tab, and underneath this there will be a property called "player spawner id". Set this to 0 (player 1) or 1 (player 2) based on which player the spawner is intended for. Then, when _ready is called, it will check the exported variable, spawning the correct player (and their choice) at the correct position (assuming the autoload and other stuff is setup correctly).

Basically, player_spawner_id is just a way to tell the spawner which player it is supposed to be instancing/creating when the game starts. Outside of that, it doesn't really serve any function. There are other ways you could handle it too, without needing to use something like player_spawner_id, I just find it can be a helpful way to setup the spawn positions from the Godot editor. :smile:

3 years later