+1 vote

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 (21 points)

1 Answer

+2 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 (11,893 points)
selected by
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 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 webmaster@godotengine.org with your username.