How can I set a value an exported variable from another scene?

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

I have a separate scene for my HUD, so I can make it reusable. I tried to set it implicitly but it didn’t work. Here’s my code below.

This is from the HUD scene:

extends CanvasLayer
export(String) var lobby_name
func _ready():
    $lobby_name.text = lobby_name + "'s lobby"

Then I tried to populate it on the parent scene:

$lobby_hud.lobby_name = "Player 1"

But it doesn’t work. Is there any way to set this dynamically?

What does your scene tree look like?

FortunePilot | 2020-04-23 12:59

:bust_in_silhouette: Reply From: njamster

Unlike in real life, in Godot children get ready before their parents. :wink: Check out the documentation here. If you print a variable on _ready it’ll always be the initial value (or null, if no initial value is defined). Setting an export variable from the parent will work, but it won’t influence printing that variable in _ready.

Thanks for the answer, I actually I figured out something else last night. The approached I took is to instantiate the HUD scene / node programatic, then I assigned a value on its script variable then I added that instance as a child of the parent scene.

hamtaro99 | 2020-04-24 09:08