The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Based on the Design the GUI tutorial and the Control the game’s UI with code Tutorial, I tried to implement those tutorials in my project. In the GUI script, when I tried to assign int MAXHP to a TextureProgress node (which on the code is the variable bar called as onready var bar = $HBoxContainer/Bars/Bar/Gauge) in the following code:

func _on_HUD_start_instanced(MAXHP):
    bar.max_value = MAXHP
    bar.value = bar.max_value

It returned the error Invalid set index 'max_value' (on base: Nil) with value of type 'int'.
Why is it returning Nil, even if the Gauge node is a child of GUI?

in Engine by (20 points)

what event did you connect to _on_HUD_start_instanced?

I've connected with an emit_signal("start_instanced", MAXHP) in the HUD script. The HUD is a CanvasLayer and the parent of Interface (Control node). The Interface is the parent of an instanced GUI scene, so the problem should be on the GUI scene. I've called print(MAXHP) in the first line of _on_HUD_start_instanced and it is the correct int value (which is 6).

1 Answer

0 votes
Best answer

I think you emit_signal too early before parent node is ready.

onready var bar = $HBoxContainer/Bars/Bar/Gauge is executed just before _ready
so if you emit signal to early, bar is actually null

I guess it can be fixed by call_defered("emit_signal", "start_instanced", MAXHP)
or... handle like below.

onready var bar = $HBoxContainer/Bars/Bar/Gauge
var max_hp = 0

func _ready():
    update_bar()

func _on_HUD_start_instanced(MAXHP):
    max_hp = MAXHP
    update_bar()

func update_bar():
    if bar == null:
        return
    bar.max_value = max_hp
    bar.value = bar.max_value
by (9,800 points)
selected by

The call_deffered function is not available for CanvasLayer nodes but the if bar == null worked! I'll pay more attention about emiting signals when the _ready functions begins. Thanks for the answer!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.