6 ms·
Simple coroutines for games in C++
- static_noise 11y agoShort version: The coroutines are regular threads. The library provides an interface for syncing which makes them behave as if they were "real" coroutines. This avoids common problems you run into when making coroutines in C++ which by itself does not support coroutines natively - but it supports threads very well. Pros: Very easy to implement and use. Will likely work very well anywhere without much thought. Cons: Resource hungry. Not really designed for thousands of simultaneous coroutines.
- vvanders 11y agoYeah, we'd traditionally use coroutines per-actor so this would be way too heavyweight. It's really nice for scripting things as you can tests a bunch of conditions, yield() and then re-test without needing to build an explicit state machine. Much easier for designers to grok. Usually done in Lua, which is a fantastic language for gamedev(low overhead, fast, coroutine support and integrates very nicely with C/C++).
- jasonjei 11y agoQuestion: couldn't you use OS fibers to do "coroutines" without being resource intensive? I know all major OS kernels have supported fibers for some time...
- gpderetta 11y agoLinux for one doesn't unless you count the obsolete (and slow) swapcontext.
- acconsta 11y agoGoogle implemented fibers. Hopefully they'll get upstreamed. https://www.youtube.com/watch?v=KXuZi9aeGTw https://www.youtube.com/watch?v=KXuZi9aeGTw
- gpderetta 11y agoIf I remember correctly, that's an additional interface for usermode initiated, cooperative scheduling of kernel threads (basically a variant of pthread_yield that allows specifying which thread is going to get the timeslice we are relinquishing). It still requites a syscall and the entities being scheduled are proper kernel level threads. So not really fibers. Pretty cool though. Edit: autocorrect
- rumcajz 11y agoYou can use this: http://libmill.org http://libmill.org
- thechao 11y agoI've recently just started using pthreads with blocking pipes. Not performant, but dead easy to use & reason about.
- slavik81 11y agoWhoops. I think I downvoted you by mistake. By the way, I found barriers to be very simple to work with for some rough multithreading. Just line all the threads up, then let them go.
- AnkhMorporkian 11y agoQuick question for you C++ gurus: Why doesn't C++ have native generators? Is there some technical reason that makes it hard to compile such a feature, or is there a political reason?
- ori_b 11y agoIt's hard to compile generators efficiently, and iterators can do a good portion of their job.
- gpderetta 11y agoIt is actually pretty easy, stackless generators desugar to a switch statement. MS implementation of their C++ coroutine proposal already demonstrates that. The problem is that, as currently specified, it requires memory allocation which can't always be optimised out and that has proven conttentious with many. Edit: s/watch/switch/
- repsilat 11y agoCan you do better than a switch statement? Say function A calls into generator B, which has entry points X,Y,Z. Maybe you could generate code for functions A<X>, A<Y> and A<Z>, specialised on the entry point into B. When B yields, it actually "calls" the appropriate A, but does something funny with the stack pointer. Maybe? The generated code would explode if you had too many coroutines going at once, and you need a lot of things to be known at compile time, and maybe the specific things you'd need to do to the stack would slow you down. I don't think you'd have to mess with the return address, though, so maybe it wouldn't be so bad.
- gpderetta 11y agoYes of course. At that point any standard optimisation that can be applied to normal code is applicable. In this case you wouldn't expect any call at all: the switch statement is online in the caller and standard constant propagation can remove the switch itself.
- jamesu 11y agoOne big problem with using coroutines for "scripting" long running events in games I don't often see mentioned is what to do - if anything - about saving their state. For cutscenes this can be ignored for sure, but what if you tie something like a long conversation tree into a coroutine? For this example library if your making some sort of traditional adventure game and you wanted the player to be able to save the game you'd have to restrict saving to checkpoints, use a loop with a switch (practically moving back to the state machine approach), or just split the conversation up into multiple coroutines as it progresses. And of course, there's the whole "what to do when you need to patch the game" problem on top of all that.
- arianvanp 11y agoThere was a discussion in the /r/haskell subreddit about serialisable coroutines a week back: https://www.reddit.com/r/haskell/comments/4a8wr8/how_to_get_serializable_coroutines/ https://www.reddit.com/r/haskell/comments/4a8wr8/how_to_get_... Perhaps it helps
- TillE 11y agoYeah, I've had the exact same problem while exploring coroutines. Stackless Python is the only implementation I'm aware of which allows you to trivially serialize its microthreads. In the end, I prefer to just use mostly "stateless" Lua scripts, with all the saveable data managed in C++.
- vmorgulis 11y ago> For cutscenes this can be ignored for sure, but what if you tie something like a long conversation tree into a coroutine? I would use an embeddable scripting language (like Duktape) for entities. The state would be available at any time. It can also be parallelized. http://duktape.org/ http://duktape.org/
- looki 11y agoAs a lot of people have mentioned Lua ITT, there's a library called Pluto[1] which can serialize coroutine states. It was used in the game Fable to store quest statuses[2]. [1]: http://lua-users.org/wiki/PlutoLibrary http://lua-users.org/wiki/PlutoLibrary see also Tamed Pluto [2]: http://www.slideshare.net/hughreynolds/lua-and-fable-jonathan-shaw-lionhead http://www.slideshare.net/hughreynolds/lua-and-fable-jonatha...
- fsloth 11y agoLightweight abstractions for control flow are a good idea. Animation control using threading primitives is not a good idea.
- derefr 11y agoRe: coroutines in games more generally, you can look to older console games (NES/SNES-era) for some good examples. Earlier games on the C64 et al were—as is commonly explained—purely event-loop based; but by the mid-1980s, the paradigm had switched to using a hardcoded coroutine-scheduled actor model. If you've never read it, the Super Mario Bros 1 disassembly[1] is extremely enlightening. It feels a lot like Erlang, with individual parallel "process" actors being responsible for updating the physics of each sprite on screen—but instead of a "process" being an abstraction handled by a scheduler, it's just an entry-point function that calls a finite-state-machine-state function, then either calls a hardwired next "process" function, or a trampoline function that calls whatever is left to run in that game state. Effectively, it's a "direct-threaded next-jmp-prefetched" implementation of scheduling. [1] https://gist.github.com/1wErt3r/4048722 https://gist.github.com/1wErt3r/4048722
- viperscape 11y agoDirected graphs are also a great way to create scripted dialogues and quests for games. Visually, a DG for your game dev is quite powerful and makes it very easy to reason about what's going on in the game story. I don't think the same can be said for coroutines hard coded in c++. In addition, it's easy to find a cycle in a graph, I'm not sure the same can be said for a coroutine. Here's a site to give some interest: http://twinery.org/ http://twinery.org/
- vmorgulis 11y agoVery interesting. I can add Inform 7 and rule-based programming of text-based fiction: http://eblong.com/zarf/essays/rule-based-if/ http://eblong.com/zarf/essays/rule-based-if/ http://inform7.com/ http://inform7.com/
- seivan 11y agoBehaviour Tree's would be easier to model and write with existing libraries that implement the decorators (wait for, etc)
- derFunk 11y agoSome time ago (10 years) we implemented this as 'continuations' into our own game engine. E.g. see https://felipec.wordpress.com/2008/09/28/continuations-in-c-easy-asynchronous-stuff/ https://felipec.wordpress.com/2008/09/28/continuations-in-c-... and http://c2.com/cgi/wiki?ContinuationImplementation http://c2.com/cgi/wiki?ContinuationImplementation
- jestar_jokin 11y agoI recommend having a look at the source code for ScummVM. The original SCUMM engine, first used in Maniac Mansion, implemented its own VM with cooperative multithreading, way back on the C64. Basically, the game ran "scripts" (containing custom bytecode), each given its own "slot" with space for locals. Each script is run in turn, and each script must voluntarily relinquish control when appropriate. Of course, this is a bit more advanced than simple coroutines. If you're going to the effort of creating your own embedded scripting language, you may as well an existing language like Lua. Example code: https://github.com/scummvm/scummvm/blob/master/engines/scumm/script.cpp https://github.com/scummvm/scummvm/blob/master/engines/scumm...