How to check for an element?

Godot Version



Example, I have two objects, one object has a script, the other does not, and I have a situation that I access both this object and the other, and where there is no script, of course, an error pops up, so how do I check in advance for the presence? In js I would just put a “?.”

Error: Invalid get index ‘isEnemy’ (on base: ‘KinematicBody2D (’).

You might want to try object.has_method("isEnemy"), it should be available on all objects.

(You could also do so from the ClassDB singleton if you manage your own classes and be strict about your objects, but you wouldn’t need to check with inheritance in the first place.)

Edit: my bad I thought “isEnnemy” was a method, glad you got it working with get

1 Like

That worked for me, but thank you

1 Like

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