Which code is better for performance?

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

Which code is better for performance and why?

variant 1:

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

variant 2:

func _on_KillArea_body_entered(body):
if body.name == player:
	body.take_damage()
:bust_in_silhouette: Reply From: Wurzelpilz

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.