Godot Version
4.4 stable Windows 11
Question
I made a character selection screen letting the player pick which playable character to spawn when the game starts. I used this code to change scenes:
var spawned_stage : PackedScene
PlayerDB.CurrentPlayableCharacter = selected_character
match selected_stage :
###list of selectable stages with their file paths
get_tree().change_scene_to_packed(spawned_stage)
And in my stage node, I have this code:
var player : CharacterBody2D
var player_scene : PackedScene
func _ready() -> void:
match PlayerDB.CurrentPlayableCharacter :
###list of selectable players and their file paths
player = player_scene.instantiate()
add_child(player)
player.position = Vector2(0,40)
PlayerDB.PLAYER = player
PlayerDB is a global, and PlayerDB.PLAYER is a variable called by many things in my game that is meant to refer to the player character.
Problem is, this code, where it sets the global player variable to the spawned player, is ran in the main stage node, which means it runs after every other ready function of the main node’s children. And I do have code in those ready functions that uses PlayerDB.PLAYER and requires a valid player character, which gives me an error when I run this because the actual player character is only spawned and the global variable is only set afterwards.
My question is, can I somehow instantiate the player character and add it as a child of the main stage node, from the previous character selection node, before all the children of the stage node run their code? I know I could theoretically tell the problem code to run one frame after, but I feel like that’s a bandaid solution, and I would rather fix the issue now than having to permanently worry about not using PlayerDB.PLAYER too early.