What is the equivalent of Coroutines in UE4?
Hi guys, I come from Unity3D. A coroutine in Unity is a way to execute a specific code every frame without putting the code in the Update() function which in UE4 is the Tick() function. Is there a way to implement such behavior in Unreal?
Checkout the class I made for courtine/async style programming: https://gist.github.com/grisevg/d5abfb9ff3f5f1a50875 It doesn't do multi-threading and only intended to be be used with methods that take longer than one frame to execute (play animation, move to, open UI Widget and wait for it to be closed, etc).
It only works with functions (delegates), that accept callbacks as their parameter. It's very easy to write your own Async methods that run Unreal's MoveTo, PlayAnimation, etc. I can provide examples if you're interested.
answered Mar 31 '16 at 10:13 PM
Not in C++. The language doesn't really support it in a cross platform way, like some platforms implement a 'Fiber' which is a native coroutine. Blueprints sort of support coroutine style scripting, in that some of the nodes automatically are executed over multiple frames without you needing to worry about it. It just executes the next node when the previous node in the chain finishes, similar to how a coroutine allows for arbitrary yield/resume. (for example the Delay Node)
If you're executing the same code every frame, you're better off just putting it in Tick. If you need a state machine around it, just having a switch statement that executes a different case base on an Enum state is my preferred approach to mimic a coroutine flow in C++.
answered Jun 25 '14 at 05:51 PM
Nick Darnell ♦♦ STAFF
For those who is still looking. Here is the most effective and lightweight solution how to achieve a similar result like it is in Unity's coroutines (no timers, no delay nodes, no timelines, not inside tick with a bunch of checks). In Unreal the same functionality is easily achieved by using Latent Actions.
In order to make it happen and to use it in your blueprints, you will need to write a code in C++. The best example is to look at how Delay node is made. But instead of a counting down a variable, you should just call TriggerLink() in Update. Here is an example of how a final node looks like in my blueprint:
To have Update/Finished nodes you need to use enum and expand it in meta. Result - is an object that controls your coroutine (so you can stop it when it is needed).
answered Jan 23 '19 at 05:20 AM
Old Question but I thought I would provide an approach I use that has the same effect. If blueprinting, create a custom event in the Event Graph. Perform whatever scripting you want and then add a delay node. This delay node acts like a co-routine, as it won't prevent the rest of the event graph from processing. Co-routines are something I do miss from Unity/C# workflow.
answered Nov 26 '16 at 03:38 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here