Coroutines are an underutilized feature of C++20 that not many understand. Under the hood, they are very complex, but luckily Geode makes it quite simple for you. Geode lets you leverage the power of coroutines to write clean asynchronous code, tackle Result propagation, and build Python-style generators with ease.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/geode-sdk/docs/llms.txt
Use this file to discover all available pages before exploring further.
Task
For most asynchronous tasks, Geode provides the Task class. SeeTasks for more information. Any function that returns a Task can be converted into a coroutine by simply using co_await on a different task.
Here’s an example of a simple coroutine:
co_await, we no longer have to set up an event listener or anything too complicated. The best part is, you can use multiple co_await statements within the same place instead of nesting a thousand callbacks!
There are a few specific things you should be aware of when using this syntax:
- The body of the coroutine is ran in the main thread, possibly only in the next frame.
- If the task the coroutine is waiting on is cancelled, the whole coroutine is cancelled
- If the task returned by the coroutine is cancelled, any pending task that is running is cancelled
co_yield
Spawning from regular functions
The correct way to launch a coroutine from a regular function is to usecoro::spawn. You can do this in a number of ways:
coro::spawn on coroutines that return Task to prevent it from canceling when it goes out of scope. In order to prevent Task’s [[nodiscard]] attribute from getting in the way, using coro::spawn on a Task-based coroutine will return a one-item tuple containing the Task. If you wish to get the underlying Task out of the spawn, you can do so simply:
$async
Creating a new function for just the asynchronous bits might get tedious. Luckily, you don’t have to with the$async macro:
$async sets a coroutine lambda and immediately spawns it via coro::spawn. Any arguments within the macro body are lambda captures, so you could just as easily put = in there to. It is not recommended to capture by reference as it’s not guaranteed that the lambda will finish before the references go out of scope.
Result propagation
The most annoying part of working with Results is propagation. In Rust, it’s as easy as using the? operator, but we don’t have such nice things in C++. Lucky for us, Geode implements coroutines for Result that allow for easy propagation:
co_await isn’t really awaiting anything at all. Instead it’s being used as a way to suspend execution and return if the underlying Result contains an error. If the Result is Ok, it extracts the value and continues execution. This allows you to write freely without having to manually check for errors everywhere.
With the help of another macro, $try, you can extend this functionality into non-coroutines as well:
$try macro sets up a coroutine lambda that gets immediately invoked. This time, since the lambda is guaranteed to be evaluated immediately, $try automatically captures everything by reference. The int template value represents the Ok output. You can use this to chain multiple Result evaluations together and do error checking as a group.
Generators
When you need to perform operations on a series of values, it’s sometimes preferred to lazily evaluate them instead of collecting everything into a vector. Thecoro::Generator object allows you to create coroutines that lazily yield values, just like Python generators.
Here’s what a basic range generator looks like:
begin() and end() function that returns an STL-compatible iterator, meaning you can use them with any read-only standard library function that takes in an iterator:
map and filter. These transformation functions yield more generators, allowing you to chain them as you please. You can use them on any generator to transform their output like so:
coro::makeGenerator function to construct a generator based on a vector or a CCArray.