0 votes

Which code is better for performance and why?

variant 1:

func _on_KillArea_body_entered(body):
if body.has_method('take_damage'):

variant 2:

func _on_KillArea_body_entered(body):
if body.name == player:
Godot version 3.2.3.stable
in Engine by (142 points)

1 Answer

+1 vote
Best answer

variant 2

variant 1 has to check every method in your script, which, depending on how many methods your player has, can take more time. Plus this check seems unnecessary since we assume the player always has such a method, unless you plan to create additionally , invulnerable players.

I would change variant 1 into a group... I call them entities, put every enemy and player in it and check with if body.is_in_group("entities")

but Variant 2 is fine but a bit prone to errors, you have to change the code every time you change the player objects name.

by (143 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.