Im making a game in Godot 4 and have a main world scene that the player is on. It has an Area2d node that changes the current scene to a UI scene when the player enters the area2D. The area2D takes a PackedScene and locationName as export variables. It loads the UI scene (named "Location") fine but I cannot get the UI scene to load the locationName I want. Also, the UI scene is not in the world scene (ie. where the area2d is and player are)
I have tried multiple methods including moving the UI scene to the world scene and using signals, also creating set gets. The problem seems to be coming from the fact that the label node on the UI is ready after the name is changed, so I get a nil exception.
Location.gd:
extends Node
@onready var itemList = $VBoxContainer/HBoxContainer/VBoxContainer/ItemList
@onready var nameLabel = $VBoxContainer/HBoxContainer/VBoxContainer/LocationName
@onready var image = $VBoxContainer/HBoxContainer/Image
var localName: String = "test"
func setLocationName(newName: String):
self.localName = newName
print(self.localName)
func setLocationImage(newImage: Image):
image.texture = newImage
# Called when the node enters the scene tree for the first time.
func _ready():
print("ready")
if is_node_ready():
update()
#generate 3 rgcs and add them to itemList, display their name, gang, and favor
#image.texture = ResourceLoader.load(location image)
func update():
print("update")
nameLabel.text = self.localName
print(self.localName)
EntryPoint.gd:
extends Area2D
@export var targetLocation : PackedScene
@export var locationName: String
func _ready():
pass
func _on_body_entered(body):
var location = targetLocation.instantiate()
location.setLocationName(locationName)
get_tree().change_scene_to_packed(targetLocation)