It seems logical to me, I mean I program a random time so that it is not so monotonous and I start the timer then I do a yield to wait for the function to send the timeout signal …
And the editor ignores me and keeps running often, so I ran out of the wander, but I have a nice shake effect on the body to which I apply it
After a while manipulating and testing things I achieved the effect I wanted by moving the yield to the beginning of the function:
of course the console tells me that the function is running non-stop, which is not true? since in the remote tab and in the game I am having everything as I wish.
yield() immediately returns you to the function that calls _do_wander() then once it gets the timeout signal it drops back into _do_wander() at your print_debug() statement. If you are calling this from _process() or _physics_process() you’re probably just creating a huge call stack of _do_wander() function calls since your wander_timer.wait_time is always longer than the time between frames.
The more appropriate way to handle this behavior would be to just handle this logic in the callback for the timeout signal of your Timer:
onready var wander_timer = $Timer # or whatever your path is
func _ready():
wander_timer.connect("timeout", self, "_do_wander")
wander_timer.one_shot = true
_do_wander()
func _do_wander():
print_debug("Change Direction?")
var random_direction = [Vector2.RIGHT, Vector2.LEFT]
random_direction.shuffle()
velocity.x = random_direction[0].x * wander_speed
wander_timer.wait_time = rand_range(1, wander_time)
wander_timer.start()
This ensures there’s only ever one call to _do_wander() queued to execute at any given time.