Check overlapping areas

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

I need to position an area and check for any overlaps. Since it takes some time for the physics to update overlaps how can I know when the update is done and it is safe to call functions such as Area.get_overlapping_areas().

:bust_in_silhouette: Reply From: sparkart

You can use: _physics_process()

It’s a callback that is called before each physics step at a constant frame rate (60 times per second by default).

I already tried that, it seems to me as it takes more than one frame to update. My code was something like this:

func move_area:
    area_to_check.transform = calc_transform()

func _physics_process(delta):
    if area_to_check.get_overlapping_areas().size() > 0
        # area overlaps
        # no overlaps

With this code it does not work, collisions are not detected. if I remove the set physics process functions it will update but not on the first call, takes a couple of frames.

J-Camilleri | 2019-07-05 07:08

When I had problems scanning an area for viable attack targets (allies rescan area when an enemy dies) in the process or physics_process functions, I was able to overcome it by using timers, and rescan when the timer reaches timeout(), and resets the timer at the same time.

var s_timer = get_node("Scan_Timer")

func _on_Scan_Timer_timeout():

func rescan_for_target():
 var scan_area = $Detect_Area
 var scan_bodies = scan_area.get_overlapping_bodies()
 for scan_body in scan_bodies:
	if scan_body == self:
	if scan_body.has_method("process_UI"): #_hit"):
		print ("viable body")

And then I adjusted the time value for the timer to about 0.03 (about 30 times a second).

DamonR | 2019-07-05 23:58

:bust_in_silhouette: Reply From: deaverrer4

Example of my code:
extends Area

const CLOSED = 0
const OPEN = 1

var state = CLOSED

func _process(delta):
if Input.is_action_just_pressed(“ui_E”):
if get_overlapping_areas():
if state == CLOSED:
state = OPEN
state = CLOSED