0 votes

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.getoverlappingareas().

in Engine by (24 points)

2 Answers

0 votes

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).

https://docs.godotengine.org/en/3.1/tutorials/physics/physics_introduction.html

by (343 points)

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()
    set_physics_process(true)

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

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.

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():
 rescan_for_target()
 s_timer.start()

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:
        continue
    if scan_body.has_method("process_UI"): #_hit"):
        print ("viable body")
        calculate_path()

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

0 votes

Example of my code:
`
extends Area

const CLOSED = 0
const OPEN = 1

var state = CLOSED

func process(delta):
if Input.is
actionjustpressed("uiE"):
if get
overlappingareas():
if state == CLOSED:
state = OPEN
$AnimationPlayer.play("Open
door")
$Sounddooropen.play()
else:
state = CLOSED
$AnimationPlayer.playbackwards("Opendoor")
$Sounddoorclosed.play()
'

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.