Null instance for no reason

Godot Version

godot-4 gdscript game 2d

Question

get_node(“/HBoxContainer/VBoxContainer/HBoxContainer/” + stat + “/Background/Stats/change”).set_text(“+” + str(get(stat.to_lower() + “_add”)) + " ")

what this is for is a stat system its supposed to get and change the label but is calling a null instance and i can’t figure out why i am spoon feeding its location to it but it still can’t find it

Leading forward slash in get node, like so get_node("/ means it’s looking from the very top of the scene tree, if this is intended then you need to look through the root node too. If you mean to be looking through children then remove the leading forward slash.

var from_root := get_node("/root/HBoxContainer/")
var from_children := get_node("HBoxContainer")

so i know the order in which it calls it goes
root/node/hbox/vbox/hbox/stat/background/stats/change
ive tried just using
hbox/vbox/hbox/ + stat + background/stats/change
but it came back as null instance as well
the error is
"attempted to call function ‘set_text’ in base ‘null instance’ on a null instance so im not understanding what going on

Any chance you could show your scene tree? if it’s generated you can check it out in the remote tab when running.

Could also try walking down the scene to debug it. i.e. and see where exactly it holds up.

var a = get_node("hbox")
var b = a.get_node("vbox")
var c = b.get_node("hbox")

Seems like the Mainstats container was missing from all of your examples, is that a new addition? If not, your get_node will looks like this.

var stat := get_node("HBoxContainer/VBoxContainer/HBoxContainer/Mainstats/%s/Background/Stats/change" % stat) as Label
var add_value = get(stat.to_lower() + "_add")
stat.text = "+%d " % add_value

Of course make sure your stat variable matches the node’s name exactly, capitalized too. I just noticed Strength is the only capitalized name, might make it tough to access that one specifically.


no i just forgot to type it in the above questions sorry

but stat is already getting its name from the code in the ready function you can see it in the screenshot var button_parent_name: = string button.get_parent().get_parent().get_name() could you explain how yours is different would like to learn more

stat isnt the name of the label its the name of the node you would find the label in

You’re right, that’s superb! At this point the main difference in my example is using string formatting with % instead of concatenation, could help with missing forward slashes like the one that should be after Mainstats

1 Like

dude that is such a power move, i was missing a / after Mainstats thank you thank you i have no one to talk these things out with, im teaching myself how to code thank you so much

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.