Last active
March 17, 2026 16:43
-
-
Save jspahrsummers/a9133beac2f116c6fffaa3949876ee9a to your computer and use it in GitHub Desktop.
await_all() and await_later() functions for Godot 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class _Awaiter: | |
| signal all_finished | |
| var finished_count := 0 | |
| var total_count := 0 | |
| func call_async(callable: Callable) -> void: | |
| assert(total_count > finished_count) | |
| await callable.call() | |
| finished_count += 1 | |
| if finished_count >= total_count: | |
| all_finished.emit() | |
| ## When called with [code]await[/code], this function will wait until all of the async [param callables] have completed. | |
| static func await_all(callables: Array[Callable]) -> void: | |
| if callables.is_empty(): | |
| return | |
| var awaiter := _Awaiter.new() | |
| awaiter.total_count = callables.size() | |
| for callable in callables: | |
| awaiter.call_async(callable) | |
| await awaiter.all_finished | |
| ## Starts an async [param callable] now, and returns a new [Callable] that can be [code]await[/code]ed to wait for its completion. | |
| static func await_later(callable: Callable) -> Callable: | |
| var awaiter := _Awaiter.new() | |
| awaiter.total_count = 1 | |
| awaiter.call_async(callable) | |
| return func() -> void: | |
| await awaiter.all_finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment