+2 votes

I have an NPC node that is a tool and one of its script variables has a setter function attached that takes a string and uses it to find a sprite image (saved a .PNG) in the game folder to choose its sprite. I type in something like "CITPED2" and it looks inside the folder to find PED2.PNG in the CIT folder.

enter image description here

However, the line of script that sets the texture always returns an error:

enter image description here

 Node not found: Sprite
 res://Maps/Actors/{Scripts/NPC.gd:28 - Attempt to call function 'set_texture' in base 'null instance' on a null instance.

From what I read of this error, the function is not recognizing the Sprite and returning it as a 'null instance'. It's weird because despite getting an error, the script does was its supposed to do. In the editor, when I change the sprite_sheet variable (and the string is a valid location), the Sprite's Texture changes without any other problems! But running this game is impossible because that error comes up on runtime just as it does in the editor.

I'm not sure what causes it, but if I remove that line changing the texture then I don't get the error.

The error doesn't pop up whenever I change the sprite_sheet variable, but whenever I save or load in the node. Maybe there's something about what happens to a tool during save time that could enlighten us, but I'm still learning how tools work.

So I'm guessing this has to do with either setter functions or how tool nodes function in the editor. I've been trying at this for awhile and I'm really out of ideas. I couldn't find the help I needed.

in Engine by (33 points)
retagged by

2 Answers

0 votes

Did you find any solution?

by (14 points)
0 votes

I had a similar issue, I fixed it by adding a check if the node is in the tree in the setter.

Here is a look at multiple solutions:

Another example: https://github.com/godotengine/godot/issues/30460#issuecomment-509697259

Example of what I did

export(Texture) var sprite_sheet setget set_sprite_sheet
func set_sprite_sheet(new_sprite_sheet: Texture) -> void:
    sprite_sheet = new_sprite_sheet
            $Sprite3D.texture = sprite_sheet
            $Sprite3D.texture = null

I think the setter may run multiple times, sometimes before any children have been added? But I'm not 100% sure on that.

Similar to the second example I linked, you may need to re-set those values in the ready function.

by (14 points)
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.