How to check have the node a custom function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By divergy321

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 “Take_Damage( damage )” and sets a damage amount. problems starts when it face wall or any other environment, because it doesnt have function “Take_Damage”.
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?

:bust_in_silhouette: Reply From: Calinou

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”.

Thanks for the the tidbit information.

molesallegiance | 2022-10-25 07:05