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: