Is there a better way to check if a variables exist?

Godot Version

Godot 4.2.1

Question

I’m working on something that relies heavily on composition (I’ve tried inheritance, and I don’t like it), and I need to know if a node has variable ( I’m not going to post the tree, as this method should work for any node, regardless of tree position). I’ve come up with a solution to this myself, as Godot seems to have no built-in method for this, but I don’t know if it’s the best way.

static func has_property(argument:String,node:Node)->bool:
	var script=node.get_script()
	var propertylistarray=script.get_script_property_list()
	for propertydic in propertylistarray:
		var propertyname:String=propertydic.get("name")
		if not propertyname.ends_with(".gd"):
			if propertyname==argument:
				return true
			else:
				return false
	return false

Ok, this was documented in an unintuitive place, but if you read the documentation for the Object class, it mentions that:

(there are also methods called has_signal and has_method, but no has_attribute or similar, weirdly enough)

3 Likes

Yo, thanks bro! Now I don’t have to worry about errors in my helper function. Thanks!

1 Like

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