#Script losing a reference after _ready()

7 messages · Page 1 of 1 (latest)

humble pond
#

SOLVED: but read if you want a cautionary tale...
I have a script with a reference set by @export var hitpoints_bar.
The correct node is selected in the inspector.
_ready(): runs and I do this:

if hitpoints_bar:
        hitpoints_bar.modulate.a = 0.0 if $Hitpoints.value >= $Hitpoints.total_maximum_value else 1.0

That works just fine, except for the main instance that uses this setup. Other instances work fine.

However, any attempt to use that reference after that results in an error for attempting to call something on a null reference, and the debug shows the reference is gone...
Checking remote when running the game shows that the node still exists and that the reference to it still exists (in third image)...so I'm at a loss for what is breaking here. The only thing I'm doing here that's slightly different than any other time is this script is an extension of another script, because I have a set of component nodes that extend the base "Component" class I wrote for them with their own scripts to do things on my entities. However, they don't lose any other references this way... Just this one so far that I know of. Can it be extending that way that's causing this? It's references in the script extension, not the class being extended that are being (lost? misplaced?...but the remote inspector shows them existing just fine...). I have no clue what is happening at this point.

#

Sorry, I had to upload those as a file, because I don't have nitro...

humble pond
#

I thought at first that a "constitution" node (extended from "entity_component.gd") was trying to continue executing code on itself after queue_free() was called in it, but that seems quite impossible... I wont totally rule it out, as I'm a tad fuzzy on how that would pan out, but I checked to make sure nothing was happening in the same process tick as when one of the entities that has these had queue_free() called in it, and that is the last thing that can be happening to that entity in a process tick as far as I can tell...

#

Weirdly... you can see right above the "hitpoints_bar" member is the "take_damage_sprite" member, and it is verifiably working correctly with no errors. I'm treating them no differently...So I really don't know how this problem is even possible.

humble pond
#

SOLVED: I have to admit stupidity...
My player-specific instance that uses this node has it's own unique instance of it...that did not have this reference set...so in fact everything was working correctly...I'm just being dumb. When I was looking at the remote scene tree, I was not checking the player's instance, but all of the other ones.
Moral of the story: if you're using the same stuff between things that are instanced at runtime and things that aren't, check both to make sure any @export members have been set that need to be... Explains all the above and fixes the problem.

calm rain
#

Thanks for posting your solution for some of us to refer to later 👍