How to find out if a node has a method

Godot Version

4.2 Mono (C#)

Question

I want to call from an AIManager node to a Pig node and use its GetCurrentState() method. It turns out that Pig is in the namespace Game.Object.Entity and I could add a Pig node to the AIManager using:
[Export]
private Pig pig pig;
However, in that case, the AIManager would not be scalable and I would not be able to attach it to other entities.
There is sort of a method to know if a node has a certain method. If there is such a method let me know about it, and if there is, how would I be able to access Pig’s GetCurrentState() method from the AIManager.

The more logical way to do this, without using reflection, would be to have a base class for all objects that have the GetCurrentState method and extend that (let’s call it StatefulEntity) to make your Pig object. Then you can check for if some_object is StatefulEntity: and that would be true for Pig, so you then can call some_object.GetCurrentState(). I think im mixing GDS and C#, hope it’s understandable tho?

I roughly get the gist of it, however this would not be a solution for me right now as it would force me to rewrite a lot of code. And I will not be able to create a class to create superclasses based on it, due to the use of the principle of composition instead of inheritance in the development of this project. (this is my thesis project)

Fair enough. Then use hasMethod.
https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-has-method

Alternatively, ClassDB.class_has_method:
https://docs.godotengine.org/en/stable/classes/class_classdb.html#class-classdb-method-class-has-method

1 Like

I tried has_method(first), it detected one method (which is just there and is not needed now), but the second method, which is needed and gives the current state, was not detected

Then you’re gonna have to post some actual code, I can’t guess what could be wrong there.

Code_m7LujuH7OE

The tooltip doesn’t show my method.
First screenshot from Pig, second from AIManager

Can you print out the result of Pig.getMethodList()? I am not sure how automatic getters are transformed in C#.

Godot_v4.2.1-stable_mono_win64_tFYwC0scMS
I guess it’s not what you wanted to see, but here it is.

Well, loop over that and get the name in each MethodInfo.
https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-get-method-list

Godot_v4.2.1-stable_mono_win64_O2eC2w4XKo
I think the method is not highlighted, so it returns a type not originally declared in godot (DelegateStateMachine.State). I am using an add-on from Firebelley called GodotUtilities, from where DelegateStateMachine is also from

I think you are just looking for a method when you have a property with default getter and setter, so the correct thing to do would be to check for characterBody2D.HasProperty(Pig.PropertyName.CurrentState). I think? Hopefully someone more versed in the C# interface can offer better advice.

I solved my problem, I think I should have originally posted my problem here, not how to make it a possible solution. The point is that AIManager is responsible for character movement by trivial logic.
Is the player in the zone? If yes, then go in his direction.
Will the next step be to the abyss? Jump.
Is the speed equal to zero? Jump
I thought that if the speed is zero, then the player has hit the wall and it is true, but not all, also when spavin speed is zero and when I spavin a pig, if it noticed me, then immediately jumped and ran to me (from joy apparently). The solution to the problem turned out to be eerily simple. I set the direction of movement of their AIManager, and I also have connected to it VelocityComponent storing the current speed. All I had to do to fix the bug was to add an exception that in addition to the velocity being zero, there should also be a direction of motion along the oX axis, i.e. left or right (-1, 1) and in that case make a jump. I added it and everything worked fine.
Thanks for your help, after a lot of question and answers I was able to find a solution to my problem.