Node variable changes not persisting.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By segaman098

Hello all! This is my first question.

So I have a root node of CharacterCreation. It has a child that is a CharacterChoices node and another child that is a RaceCreationScreen node. PlayerChoices has a member variable of race.

My goal is that when someone chooses a race on the race creations screen, that change persists in the PlayerChoices node. However, it doesn’t seem to be doing that. Even though I update PlayerChoices.race, when I call it later it has no value.

Some more detail below

On the the RaceCreationScreen there are race option buttons. When they are clicked, they emit a race_changed signal. CharacterCreation listens for this signal, and updates race in CharacterChoices when someone clicks a race button. E.x., if someone clicks the “Human” button, CharacterChoices.race is set to “Human”.

Also on the CharacterCreation screen is a button to navigate to the RaceCreationScreen. When that button is clicked, it calls a RaceCreationScreen.createPageButtons passing in the parameter of CharacterChoices.race.

Code:

# Character Sheet 
extends Control

var screen_to_load : String

onready var CharacterChoices = $CharacterChoices
onready var RaceCreationScreen = $RaceCreationScreen

func _ready() -> void:
	$Menu/StepOptions/RaceButton.grab_focus()
	for button in $Menu/StepOptions.get_children():
		button.connect(
			"pressed", 
			self, 
			"_on_Button_pressed", 
			[button.screen]
		)

func _on_Button_pressed(screen) -> void:
	screen_to_load = screen
	$Menu.hide()
	match(screen_to_load):
		"race":
			print("Current Race: ", CharacterChoices.get_instance_id())
			if(CharacterChoices.race):
				RaceCreationScreen.createPageOptions(CharacterChoices.race)
			RaceCreationScreen.show()
	

func _on_RaceCreationScreen_race_changed(race : String) -> void:
	CharacterChoices.race = race
	print(CharacterChoices.get_instance_id())
	

# Race Creation Screen
extends Control

signal race_changed(race)

var TitleLabel := preload("res://character_creation/race_creation_screen/title_label.tscn")
var OptionLabel := preload("res://character_creation/race_creation_screen/option_label.tscn")
var GridLabel := preload("res://character_creation/race_creation_screen/grid_label.tscn")

onready var SocialClassOptions := $FullPageContainer/Body/SocialClassOptions
onready var RaceInfo := $FullPageContainer/Body/RaceInfo
onready var TraitsGrid := $FullPageContainer/Body/RaceInfo/TraitsGrid
onready var SaveGrid := $FullPageContainer/Body/RaceInfo/SaveGrid
#onready var CharacterChoices := $".."/CharacterChoices

func _ready() -> void:
	for button in $FullPageContainer/Body/Options.get_children():
		button.connect("pressed", self, "_on_Button_pressed", [button.race])


func _on_Button_pressed(race : String) -> void:
	createPageOptions(race)
	emit_signal("race_changed", race)
	

func createPageOptions(type) -> void:
	var race_data : Resource
	var race_data_path : String
	
	race_data_path = "res://database/race_data/" + type + ".tres"
	race_data = load(race_data_path)
	
	createClassOptions(race_data.social_classes)
	createRaceInformation(race_data)


func createClassOptions(selectedClassOptions) -> void:
	for node in SocialClassOptions.get_children(): node.free()
	createTitleLabel("Social Classes", SocialClassOptions)
	for option in selectedClassOptions: createOptionLabel(option, SocialClassOptions)


func createRaceInformation(raceData) -> void:
	for node in TraitsGrid.get_children(): node.free()
	for node in SaveGrid.get_children(): node.free()
		
#	var sprite := Sprite.new()
#	sprite.texture = load("res://icon.png")
#	RaceInfo.add_child(sprite)
	var title : String = raceData.type + " Traits"
	$FullPageContainer/Body/RaceInfo/TraitsLabel.text = title
	
	for trait in raceData.traits: createGridLabel(trait, TraitsGrid)
	for bonus in raceData.save_bonuses: 
		createGridLabel(bonus, SaveGrid)
		

func createOptionLabel(text, list) -> void:
	var newOption = OptionLabel.instance()
	newOption.text = text
	list.add_child(newOption)


func createTitleLabel(text, list) -> void:
	var newTitle = TitleLabel.instance()
	newTitle.text = text
	list.add_child(newTitle)

	
func createGridLabel(text, list) -> void:
	var newGridOption = GridLabel.instance()
	newGridOption.text = text
	list.add_child(newGridOption)

# CharacterChoices

extends Control

export var race : String setget update_race, get_race

func update_race(new_race):
	print("updating to", new_race)
	race = new_race
	
func get_race():
	print("You're race is: ", race)
	return race
:bust_in_silhouette: Reply From: segaman098

I figured out the problem. It turns out that when I was clicking the back button on the race creation screen to go back to the Character Creation screen, it was using change_scene, which created a new instance of the Character Creation screen, erasing the info that was stored earlier in Player Choices.

Thanks to anyone who ended up reading this!