- Home /
How to use Coroutines in C++/CLI?
I somehow managed to use C++/CLI
in Unity
by compiling it with /clr:safe
. Now I want to learn how can I use Coroutine
in C++/CLI
. There is no keyword in either C++
or C++/CLI
like yield
and without yield I cannot make it run. So how am I supposed do this? If anyone knows how to do this, please help. I searched it on Google and found nothing but only for IEnumerable<T>
.
You probably may want to start with google. Also, this is c++ question, so considered mostly off-topic on this forum.
Answer by Bunny83 · May 05, 2021 at 08:55 PM
Why do you want to use C++ CLI? Even Microsoft does not really recommend to use it as an actual development language. It's mainly a bridging language between native C++ and the .NET world.
Though to answer your question specifically: Coroutines in Unity are build completely on the IEnumerator interface of the .NET framework. The yield keyword in C# is a pure compiler feature that does all the heavy lifting for you which makes creating coroutines / statemachine very easy. C++ / CLI does not have such a compiler feature. You could still create your own IEnumerator and run it as "coroutine", but it's pretty pointless to manually construct a statemachine in order to have it run as a "coroutine" in unity.
You may want to read my answer to this question over here where I explained in detail how coroutines work in Unity and more importantly what the yield keyword actually does for you.
You may have your reasons to use C++ / CLi, however I would highly recommend to stay away from it if possible. It's not C++ and not a proper .NET language either. It essentially brings all bad parts from both worlds together and has a lot of additional syntax that's neither C++ nor C# just in order to make the language support the .NET features.
Your answer
Follow this Question
Related Questions
Ienumerator return string 0 Answers
Coroutines not passing yield 1 Answer
Corutine not behaving like it should? 1 Answer
Yield return inside a loop slowdown problem 2 Answers
WaitUntil doesn't work and the coroutine starts anyways 1 Answer