Alternative method on how to check what body just entered the Area2D

Wired signal body_entered in Area2D in lightning.gd.

func _on_Lightning_body_entered(body):
    if body == Global.Player:
        Global.GameState.hurt()
    queue_free()

This will work fine - I used collision masks because I didn’t want Godot to have to check every time an NPC entered a spike, but it’d totally work!

1 Like

I tried the same thing as @p4nu, but now I’m confused because your response makes it sound like there may be some optimization reason to not choose signals in this instance.

@Yann_Burrett, if you have time, could you explain why your method is preferred in this scenario?

The Godot documentation seems to suggest using signals for collisions.

Array get_overlapping_bodies() const
Returns a list of intersecting PhysicsBody2Ds. For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.

We used a signal for the spike enemy and the spike trap. It’s unclear how this case differs.

John

Well, almost. We are using a signal, we’re just running an if condition every time anything enters the area. Instead, we’re setting it up so that only one thing can trigger the signal and therefor don’t need to run an additional condition.

Privacy & Terms