Unity-like coroutines in plain old C
Let’s just begin with this: Coroutines are great!
For those who don’t know what coroutines are, let me explain:
Coroutines allow a function to be paused and resumed. This is often used to allow the concurrent execution of multiple functions (and is often what is used to implement async
/ await
in most programming languages), but in Game Development, they are also used to spread a long running function over multiple frames.
In the above screencast, clicking on the button “DAMAGE” sets the red bar (representing health) to a specific value, and triggers a coroutine that changes the value of the orange bar progressively over time.
In this article, we’ll see how to implement this in C.
In-depth explanation of coroutines
At its core, a coroutine can be modeled as a state machine: