Menu
Coddy logo textTech

What is a Coroutine?

Part of the Logic & Flow section of Coddy's Lua journey — lesson 48 of 54.

Think about playing a video game. You can pause it at any moment, go do something else, and then come back to resume exactly where you left off. Coroutines in Lua work in a similar way.

A coroutine is like a special function that can pause itself in the middle of execution and then continue later from that exact point. Unlike regular functions that run from start to finish without stopping, coroutines give you control over when they pause and resume.

This ability enables something called cooperative multitasking. Instead of running multiple tasks at the exact same time, coroutines let you switch between different tasks voluntarily.

One coroutine runs for a bit, pauses itself, another one takes over, and so on. Each coroutine cooperates by deciding when to give control back.

Coroutines are useful when you need to break up long-running operations, manage turn-based systems, or create sequences that need to remember their state between calls. In the upcoming lessons, you'll learn how to create, pause, and resume coroutines in Lua.

Cheat sheet

A coroutine is a special function that can pause itself during execution and resume later from the exact same point, unlike regular functions that run from start to finish without stopping.

Coroutines enable cooperative multitasking, where tasks voluntarily switch control between each other rather than running simultaneously. One coroutine runs, pauses itself, another takes over, and the cycle continues.

Coroutines are useful for breaking up long-running operations, managing turn-based systems, or creating sequences that need to remember their state between calls.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow