This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+4 votes

Hello!

Im seekeng a way to check, have some node a custom function.

Problem description:
I have area2d bullet, that flys forward. when it face Enemy or Player it calls a function "TakeDamage( damage )" and sets a damage amount. problems starts when it face wall or any other environment, because it doesnt have function "TakeDamage".
I want to modify bullet script to check have faced object "Take_Damage" function or it doent, and only if it have, then call it.
But i dont know how to check it.

Maybe someone knows how to solve this problem?

Godot version 3.3.2
in Engine by (24 points)

1 Answer

+6 votes
Best answer

Use Object's has_method(method: String) to check whether the object has a method defined:

if body.has_method("take_damage"):
    body.take_damage()

This is also called "duck typing".

by (12,908 points)
selected by

Thanks for the the tidbit information.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.