Add child to new scene when changing scenes

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.

use an int and give each character a number.
make an array in the autoload with the packedscenes to the characters (use preload), or use paths and dynamically call load with the path.

selecting a player selects a number.

scene loads

autoload gives the packedscene to spawn (the selected character).

var characters : Dictionary[int, PackedScene] = [0 : preload("res://characters/player1.tscn")]#drag n drop your files here

var current_playable_character : int = 0

var player : CharacterBody2D
func _ready() -> void:
	var scn : PackedScene = PlayerDB.characters.get(PlayerDB.current_playable_character, null)
	if scn:
		var player = scn.instantiate() as CharacterBody2D
		if player:
			add_child(player)
			player.position = Vector2(0, 40)
			PlayerDB.player = player

keep in mind that the player exists in the scene and will be deleted everything the scene is changed. the autoload only holds a reference to it.