So I'm quite new to Godot, only been using it for three days, so I'm still very much in the learning phase. I'm trying to find an random position that doesn't collide with my StaticBody2D's in order to set it as a target position when my main scene loads but I can't figure out how to do it inside a loop. Here's the code I currently have, running inside the _ready function of my main scene node:
var valid = false
var pos
while !valid:
var x1 = $BG.global_position.x + 100
var y1 = $BG.global_position.y + 100
var x2 = x1 + $BG.size.x - 200
var y2 = y1 + $BG.size.y - 200
pos = Vector2(randi_range(x1, x2), randi_range(y1, y2))
$target_tester.position = pos
if !$target_tester.has_overlapping_bodies():
valid = true
This doesn't work (as in, it always sets valid to true on the first loop regardless), I'm guessing because the physics collisions doesn't update correctly until the next physics frame, so the overlapping areas of my Area2D $target_tester and static bodies don't trigger during the loop.
What would be the Godot method for achieving this without having to iterate over multiple frames (it seems to me, depending on how busy the scene is, it could take quite a number of frames to find a free position without doing it inside a loop)?