Problems with finding properties in a tool script

Godot Version

4.4

Question

I am trying to make a tool script to show some visuals in the editor in order to make my inventory system more usable, but I’m having trouble accessing variables right now I’m using this line

if get_parent().MyItem != null:
			Display(get_parent().MyItem, get_parent().MyCount)

but when the node this script is on doesn’t have the right parent with the MyItem variable it returns an error, this makes sense to me, but I can’t figure out a way to do this without an error, other things I have tried are lines like if get_parent().get_script() == (pre)load("Script.gd") but this didn’t work either, what am I missing?

You need a way to check if the parent is of the type you are looking for. One way to do this is to use Object.get() function, which returns null if the property doesn’t exist, but I think it might be better to do something like if MyItem is MyClass: which checks the class of the object you’re looking for.

1 Like

In can also be used to check for a property:

if "property" in object:
1 Like

cool, i didn’t know you could do that

Thanks for all the solutions, they worked great!