Studio › Manual › Code and objects

Coroutines and for … in

A coroutine is a function with yield in it: it pauses at each yield and goes on later, so a cutscene or a sequence of moves reads top to bottom.

function intro() {
    yield wait(1.5);                     // 1.5 seconds later
    show_debug_message("Where am I?");
    yield until(dialogue_done);          // when the function returns true
    yield tween(door, "y", door.y - 64, 0.8);
}
coroutine_start(intro);
  • yield wait(seconds), yield until(fn), yield next_step, yield tween(target, "name", to, seconds), or yield another coroutine to wait for it.
  • coroutine_start(fn, args...) returns the coroutine; coroutine_stop, coroutine_is_running, coroutine_get_result. A coroutine stops with its instance; coroutine_start_global keeps going.
  • yield can't be inside with, switch or try: GameMaker builds turn each coroutine into a small state machine, and can't pause there. Problems says so.

for … in loops over a dict's or set's keys, an array's values, a struct's names, or a range: for (var k in d) { ... }, for (var i in range(0, 10)) { ... }. yield and in are coloured as keywords, and the hover card explains them.

Keys are shown as on the Mac: ⌘ Command, ⌥ Option, ⌃ Control, ⇧ Shift. Studio for Windows shows its own keys beside each menu item.