What is the replacement of GDscript "has_method" the doc is short and unclear to me

Godot Version



I try to understand what is the equivalent to has_method

In the second paragraph it explans about how and what to use in c# , but i dont understand what does it means .
for example i have this code :


 for weapon in weapons:
	 if weapon.has_method("set_inactive"):

C# i dont know how to continue from here …

private Array<Node> weapones = GetNode<Node3D>("Weapones").GetChildren();
 foreach(var weapon in weapones)


Thanks for your help

maybe weapon.HasMethod("SetInactive") ? not tested

I haven’t tried Godot with C# yet, I’m not sure if C# has such a function, rather not necessary because it’s not a dynamic scripting language.

Check the docs (type-conversion-and-casting)

weapon.GetType().GetMethod("set_inactive") != null

But I’d advise to never rely on method name checks (aka magic strings). It’s an anti-pattern, i.e. design that can lead to issues down the road. It’s better to make sure all weapons inherit from a base class that has “set_inactive” method (or “deactivate”) and then specific weapons override that method to just do nothing if they don’t support deactivation.

Good luck!

Hey , thanks
Well the weapon is MeshInstance3D and inheriting from MeshInstance3D That in the end its Node3d … none of them have those methods i also don’t see in the references
that this method exist

So i don’t see any other options , no?

Impossible to answer without knowing what you’re trying to do, exactly. Why is it important to know if set_inactive exists? What’s this method supposed to do? Like what is actually happening in the app, and where does this code even live that checks for a method on each weapon?

Context is needed to provide any answers specific to your situation.

Ok found it it is part of the script that is attached to simple Node3d
But i got your point tnx

But without your help i never guessed that the answer is this ,
The godot documentation is not clear at all . for me at list .

1 Like

Glad you found a solution, regardless of method =) Cheers!

1 Like

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