Trouble Understanding Await

Godot Version



I’m having trouble with await and understanding how it works. I have the following code:

while ! readyToIdle:
	await queue.isEmpty()
	Logger.debug(self, "iterating %s" % queue.isEmpty())

Basically, I want to yield to the rest of the program but wait in this function until a queue (players) is empty. When the queue is empty the “readyToIdle” will be set to true in another function that is listening for a signal from the queue. The result of this snippet is continual printing of “iterating false”.

Why doesn’t await actually wait? Am I using the wrong logic?

Don’t add parentheses in front of it. I’m pretty sure await only works for signals

Your code is intuitively good, but calling await only waits on two things

  • A signal.
  • Another await inside a function.

So the way I would implement your code is with a signal on your queue object:

await queue.has_changed # Signal
if queue.is_empty():
   # Logic here.

Now in your queue object you need to emit this signal whenever something is added/removed. Then await will wait until this signal is next called.

I hope this helps :slight_smile:

1 Like

Thanks! That worked perfectly. Appreciate it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.