Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coroutines for game design?

I've heard that coroutines are a good way to structure games (e.g., PEP 342: "Coroutines are a natural way of expressing many algorithms, such as simulations, games...") but I'm having a hard time wrapping my head around how this would actually be done.

I see from this article that coroutines can represent states in a state machine which transition to each other using a scheduler, but it's not clear to me how this applies to a game where the game state is changing based on moves from multiple players.

Is there any simple example of a game written using coroutines available? Or can someone offer a sketch of how it might be done?

like image 603
Kiv Avatar asked Aug 08 '09 03:08

Kiv


People also ask

What are coroutines in C#?

A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes.

Should you use coroutines unity?

When to use a Coroutine in Unity. It's worth considering using a Coroutine whenever you want to create an action that needs to pause, perform a series of steps in sequence or if you want to run a task that you know will take longer than a single frame.

What are coroutines used for?

Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.


1 Answers

Coroutines allow for creating large amounts of very-lightweight "microthreads" with cooperative multitasking (i.e. microthreads suspending themselves willfully to allow other microthreads to run). Read up in Dave Beazley's article on this subject.

Now, it's obvious how such microthreads can be useful for game programming. Consider a realtime strategy game, where you have dozens of units - each with a mind of its own. It may be a convenient abstraction for each unit's AI to run as such a microthread in your simulated multitasking environment. This is just one example, I'm sure there are more.

The "coroutine game programming" search on Google seems to bring up interesting results.

like image 154
Eli Bendersky Avatar answered Sep 23 '22 02:09

Eli Bendersky