• Godot Help
  • For Area2D, is there a way to check "inside" rather than "entered" or "exited"?

I think the subject line is basically the question, but I'll try to elaborate.

For checking object collisions (like players touching enemies, for example) I've been using the body_entered() and body_exited() signals. This works great in general but causes a problem for stationary objects (like spikes, for example) because the player can just stand still and avoid any further damage.

This is of course because the signal functions only get called when the player enters the specified Area2D, and don't get called again unless the player leaves and re-enters. I'm hoping there's something that can still use the same general logic of body_entered(body) but rather than checking for the entering body, I can verify whether the body is currently overlapping with the Area2D so that it would get called without having to exit/re-enter.

Is there such a thing? I feel like I've missed an obvious solution here.

EDIT: I was looking for get_overlapping_bodies(). There's an example in a reply below.

    trevbot
    I dont actually know but my idea would be to memorize what was called at last, so for example if "body_entered" was called at last then you would know that the object is currently in the area aka overlapping it.

    So like setting a hasEntered bool or something and using that to determine whether they're still overlapping?

    It would definitely work, but it seems like a lot of extra lines (set it in body_entered(), reset it in body_exited(), evaluate it in _physics_process(delta)). Might get muddy if the player is overlapping with two damaging objects (unless the bool flag is on the damaging object and not on the player?)

    That's the stuff!

    Here's how I'm using it:

    func _physics_process(delta):
    	for body in $SpikeArea.get_overlapping_bodies():
    		if(body.get_collision_layer() == 1):   # Check to see that overlap is Player layer
    			body.getHit()             # Func 'getHit()' on player deals w/ damage, etc

    This negates the need for the body_entered() signal altogether.

    EDIT: Should clarify that the above code is on the 'spikes' object, in case that wasn't clear form context.