Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: Not Selected
more ▼

asked Jun 25 '14 at 05:19 PM in C++ Programming

avatar image

324 21 22 42

avatar image dbrizov Jun 25 '14 at 06:53 PM

Thanks for the redirection, I didn't see the duplicate question.

avatar image Fred82 Mar 26 '17 at 09:43 AM

This answer is a little bit too late for you I guess :) But I was just googling for the same question and this came up; so for anyone else wondering about the same problem I would suggest an alternative.

Instead of using the OnTick event (which is equivalent to Unity's Update) I suggest that you use a Timer or Timeline event. I can't really tell you much about Timelines so you'll have to check that out, but here's a solution that might work for you regarding Timers:

You create a trigger for which to call the Timer Event, and you set it to update by a loop which uses DeltaTime. But because the Timer Event itself will only be ran once you have to make sure that the function you are calling can pause and clear the timer itself! Check out the examples in my images, hope it helps!

alt text

ur1.png (189.5 kB)
(comments are locked)
10|2000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Mar 31 '16 at 10:13 PM

avatar image

136 7 15 20

(comments are locked)
10|2000 characters needed characters left

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++.

Cheers, Nick

more ▼

answered Jun 25 '14 at 05:51 PM

avatar image

Nick Darnell ♦♦ STAFF
6k 77 36 150

(comments are locked)
10|2000 characters needed characters left

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:

alt text

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).

     UFUNCTION(BlueprintCallable, Category = "GameBPLibrary", meta = (Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject", ExpandEnumAsExecs="Exec"))
         static void RunCoroutine(UObject* WorldContextObject, ECoroutineStateExec &Exec, struct FLatentActionInfo LatentInfo, UCoroutineActionResult *&Result, float &DeltaTime);

more ▼

answered Jan 23 '19 at 05:20 AM

avatar image

794 9 15 24

(comments are locked)
10|2000 characters needed characters left

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.

more ▼

answered Nov 26 '16 at 03:38 PM

avatar image

21 2 6 10

avatar image dbrizov May 09 '17 at 09:11 AM

There are also Timers. I use them a lot :)

(comments are locked)
10|2000 characters needed characters left

Please visit the link below if you are interested in yet:

more ▼

answered Dec 30 '17 at 08:56 PM

avatar image

11 1 4 4

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question