Best way for Node2D/Area2D detection _on_area_2d_area_entered or get_overlapping_bodies()

Godot Version

4.x`

Question

`Hello,
I am trying to make a kind of “TowerDefense” game.
I can have a lot of turrets and every turret has a unique array with all the enemy-nodes in it, that are reachable.
My question is as follows:
Is it better to sort every new enemy that collides with the turrets view via the
_on_area_2d_area_entered(area) Function
or is it better to sort them EVERY FRAME new via
get_overlapping_bodies() function.

I have a lot of problems via the _on_area_2d_area_entered(area) function as well as its pendant _on_area_2d_area_exits(area) function…
but my main fear is that sorting and all that stuff every frame for every turret with the get_overlapping_bodies() function is to ressourse costing
is that true?

Area2D/3D.get_overlapping_areas has basically no cost to be called, people seems to confund that function with ShapeCast2D/3D.force_shapecast_update. Area2D/3D.get_overlapping_areas don’t redo the overlap check, just return the array of current areas/bodies that was detected as colliding in the physics step.

Also, this array is updated in the same point as the area_entered/exited is triggered, so if you having trouble with signals you’ll also have trouble with the function or your code has some wrong logic.

Call get_overlapping_areas every frame will not destroy your fps but also is not the correct way to use this function, signals is made for that.

1 Like

Thanks for the very very fast respond=D

Well idk why everyone recommends high level tools like Area2D, and collisions shapes, when you can run Geometry2D/Geometry3D single func without need of additional CanvasItem (Node2D)
to filter enemies in range you may just call using timer 0.33s
fast:

turret.global_position.distance_squared_to(enemy.global_position)

or a bit slower

turret.global_position.distance_to(enemy.global_position)

Because this approach require you have a reference of every enemy and do a loop in every tower to make this check, also using Area2D/3D in this case will not have any perfomance penality

but it done by engine, isn’t it? How then engine detects if collision is happen? Every area detects if anybody is entering inside of it, thats what monitoring/monitorable property is doing. Which is done every tick, 60 times in a second which is not necessary in most situations, when mine approach is done only 3 times. I suppose it uses similar algorithm to do cached checks - checking distance before then check form if close enough.
Getting enemies references not a problem if they were grouped.

@solver10 The question is: How many areas you have and how complex is their shapes? Because if you leading with dozens or even some hundreds, the difference will be pointless. Don’t make any sense do that type of otimization for scenarios that the normal solution simple works and don’t make the game slow, unless you have specific needs more than detect the body/area is in the attack area.