Local Variables getting null after first iteration of the change_game_state

Godot Version

4.2

Question

Hi guys, for some reason, on the second iteration of the change_game_state function, the local variables of the game_manager that hold the

  • ui_game
  • ui_player
  • player

Are getting null for some reason that I can`t figure it out, can someone please help me?
Code below:
extends Node

const PLAYER_NODE = preload("res://Scenes/Player/player.tscn");
const ENEMY_MANAGER_NODE = preload("res://Scenes/Managers/Enemy/enemy_manager.tscn");
const UI_GAME = preload("res://Scenes/Interfaces/Game Interface/ui_game.tscn");
const UI_PLAYER = preload("res://Scenes/Interfaces/Player Interface/ui_player.tscn");
const UI_BATTLE = preload("res://Scenes/Interfaces/Battle Interface/ui_battle.tscn");
const UI_HAND = preload("res://Scenes/Interfaces/Hand Interface/ui_hand.tscn");
const AOE_CARDS_HITBOX = preload("res://Scenes/Hitboxes/aoe_and_skill_cards_hitbox.tscn");
const OPTIONS_MENU = preload("res://Scenes/Menus/Options/options_menu.tscn");
const PROFILE_MENU = preload("res://Scenes/Menus/Profile/profile_menu.tscn");
const INFO_MODAL = preload("res://Scenes/Interfaces/Modal/info_modal.tscn");

enum GameStates {
	FIRST_EVENT,
	BATTLE,
	RANDOM_EVENT,
	MERCHANT,
}



var player: Player
var enemy_manager: EnemyManager
var ui_game: UI_Game;
var ui_player: UI_Player;
var ui_battle: UI_Battle; # Condicionally rendered on battles
var ui_hand: UI_Hand; # Condicionally rendered on battles
var hitbox; # Condicionally rendered on battles

var night: int = 1;
var hour: int = 1;

var active_game_state: GameStates;
var next_game_state: GameStates;

func _ready():
	print("GameManager is Ready")
	
func _on_tree_entered():
	print("Tree entered!")
	instantiate_ui();
	player = instantiate_player(selected_class);

	self.add_child(ui_game);
	self.add_child(ui_player);
	self.add_child(player);

func _on_child_entered_tree(node):
	print("Node entered tree: ")
	print(node)
	if node.name == "Player":
		print("Player node added to the scene");
		change_game_state();

func instantiate_ui():
	ui_game = instantiate_game_ui();
	ui_player = instantiate_player_ui();
	ui_player.current_night = night;
	
func instantiate_game_ui() -> UI_Game:
	var game_ui = UI_GAME.instantiate();
	return game_ui

func instantiate_player_ui() -> UI_Player:
	var player_ui = UI_PLAYER.instantiate();
	return player_ui
	
func instantiate_battle_ui() -> UI_Battle:
	var battle_ui = UI_BATTLE.instantiate();
	return battle_ui;
	
func instantiate_hand_ui() -> UI_Hand:
	var hand_ui = UI_HAND.instantiate();
	return hand_ui;
	
func instantiate_player(player_class) -> Player:
	var instantiated_player = PLAYER_NODE.instantiate();
	instantiated_player.characther = player_class;
	return instantiated_player;
	
func instantiate_random_event_ui():
	pass

	
func instantiate_enemy_manager() -> EnemyManager:
	var manager = ENEMY_MANAGER_NODE.instantiate();
	return manager;

func instantiate_hit_box() -> Area2D:
	var aoe_hitbox = AOE_CARDS_HITBOX.instantiate();
	return aoe_hitbox;

func advance_time():
	if self.hour > 9:
		end_night();
		return
	
	hour += 1;
	print("Log: changing scene");
	change_game_state();
	
func end_night():
	night += 1;
	if night == 7: 
		trigger_boss_of_the_run();

func deinstantiate_active_game_state():
	if active_game_state == null:
		return
	match active_game_state:
		GameStates.FIRST_EVENT:
			pass
		GameStates.BATTLE:
			ui_battle.queue_free();
			ui_hand.queue_free();
		GameStates.RANDOM_EVENT:
			pass
		GameStates.MERCHANT:
			pass

func determine_next_game_state():
	## Hardcoded;
	next_game_state = GameStates.BATTLE;
#
func load_active_game_state():
	match active_game_state:
		GameStates.FIRST_EVENT:
			pass
		GameStates.BATTLE:
			ui_battle = instantiate_battle_ui();
			ui_hand = instantiate_hand_ui();
			enemy_manager = instantiate_enemy_manager();
			hitbox = instantiate_hit_box();
			
			self.add_child(ui_battle);
			self.add_child(ui_hand);
			self.add_child(enemy_manager);
			self.add_child(hitbox);
			
			enemy_manager.set_enemies_of_the_round(night, hour);
			player.connect_to_battle_signals(); 
			print(player)
				
		GameStates.RANDOM_EVENT:
			pass
		GameStates.MERCHANT:
			pass
	
func change_game_state():
	deinstantiate_active_game_state(); # Clear active_game_state;
	determine_next_game_state(); # Define next_game_state;
	active_game_state = next_game_state; # Define active_game_state as next_game_state;
	load_active_game_state(); # Load active_game_state;