Skip to content

How does coroutine execution work?

Juju Adams edited this page Oct 31, 2021 · 25 revisions

Coroutine execution via this library centres around the "coroutine root struct", a data container that both manages the execution of the coroutine as well as stores coroutine state (the variables that you read and write whilst the coroutine is executing). When a coroutine is created, it is registered in a global list of coroutines that are processed every frame. When a coroutine [completes](When does a coroutine complete?) it is removed from that global list meaning the memory allocated to it can be freed as well, provided that your code isn't holding a reference to it.

When a coroutine is defined, the root struct is given a sequence of instructions it must carry out. Instructions can simply be "run this block of GML", or the can be flow control (loops and branching), or they can be behaviours that require the root struct to wait for further input.