Coroutine alternative?

Hi L&G

Came from Unity, there i was using StartCoroutine() and yield return new WaitForSeconds(), coroutines is strongly useful to do certain things like temporal alghoritms:

1- Play sound
2- Play animation
3- wait untill animation finishes
4- play sound
5- wait untill sound finishes
6- disable object

I have seen yield is a C# only stuff, so i would like to know is somebody has threated with this before… how can i replicate the example above using Unreal C++ ?

Thanks in advance.

Unreal has Latent Actions though they are not as straightforward to use as Unity’s coroutines. To implement a latent action first create a class derived from FPendingLatentAction. The basic idea behind it is that it calls Update until you tell it that the action has finished. Second, after you create your action class, expose it to Blueprint by using specially decorated function.

To learn more details take a look at the implementation of the Delay node (search for FDelayAction) - this is the simplest possible latent action.

Also, while you will typically implement Latent Actions in C++, I strongly suggest using Blueprint to actually call them. It is quite clear and straightforward to create a sequence of actions in Blueprint. It might not be so in C++.

Thanks! I will give a try…

What about this compared to Timers ?.. do both do the same ?

Timers are slightly different. Timers allow you to call a function or an event periodically while latent actions usually perform some operations until finishing condition is met. Free tip: if you need a simple delay it might be more handy to use a Delay node instead of timer.

2 Likes

Thank so much…