r/cpp • u/mropert • Mar 20 '26
Looking at Unity finally made me understand the point of C++ coroutines · Mathieu Ropert
https://mropert.github.io/2026/03/20/unity_cpp_coroutines/
88
Upvotes
r/cpp • u/mropert • Mar 20 '26
22
u/KFUP Mar 20 '26
Yeah, coroutines make much more sense when seen in the whole picture. They only clicked for me when I saw an explanation of std::generator by Nicolai Josuttis, seeing how std::generator works with coroutines helps to understand why would you want to use coroutines instead of using multiple functions.
16
u/RoyAwesome Mar 20 '26
I've been doing this for a while now! It's really useful when doing gameplay programming because it lets you write gameplay code synchronously and run it "over time". It only takes a few building blocks (Waiting for positions, waiting for animations to finish, etc) and you're in deep.
One benefit though to the struct/state machine system though is that you can re-enter at any "pause point". This is very helpful in multiplayer when you may predict what will happen as the client, and if the server says "no you can't do that because your state at this checkpoint is XYZ", you can simply rerun your state machine from that checkpoint with the last good state and re-predict where you are at now. You can't do this with coroutines, making their utility somewhat limited in a multiplayer gameplay context. I hope that code generation in C++ will give the tools to build out a state machine style thing trivially.
14
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Mar 20 '26
One big issue is that the state of C++ coroutines is completely opaque -- that makes it impossible to serialize/deserialize them to memory/disk.
Therefore, they're unfortunately not suitable for game logic if you value the ability of deterministically saving/loading their state, which is important for persistance, networking, testing, and so on.
This is yet another example of where the design of Core Coroutines would have been superior.
If you want to both (1) coroutine-like code and (2) serialization/deserialization you need to resort to macros. Here's an example of how you can do that (unpolished, just an experiment): https://gcc.godbolt.org/z/9s9ssze8P
6
u/SavingsCampaign9502 Mar 20 '26
There is something that always confuses me.
2
u/Raknarg Mar 20 '26
being in python world for a while Ive been waiting a long time for generators to get support. Very useful and lets you design very nice APIs.
2
u/XeroKimo Exception Enthusiast Mar 20 '26
I've been messing around with the raw coroutine primitives and I'm not quite understanding this.
co_yielditself returns anAwaiterobject so it also does something similar toco_awaitat the end, though from the user, you can't customize what thatAwaiteris... but there isn't really much difference toco_yield std::monostateand something likeco_await std::suspend_alwaysno?1
u/NotMyRealNameObv Mar 21 '26
That's a very random Rocky Horror Show reference.
1
u/foriequal0 Mar 23 '26
Yeah. Compiler generates state machines for you. I like to (ab)use it to implement fairly linear choreography (e.g. cutscenes) in Unity.
Nowadays most Unity devs uses async/await with UniTask instead of coroutines.
1
u/pjmlp Mar 20 '26
Which is quite understandable when you know the history behind them.
They were initially proposed by Microsoft, based on a C++/CX extension, that was inspired by .NET async/await implementation, as the WinRT runtime was designed to only support asynchronous code.
Thus if one knows how the .NET compiler and runtime magic works, including custom awaitable types, there will be some common bridges to how C++ co-routines ended up looking like.