# How to do async fuctions?

**URL:** <https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [December 11, 2015, 11:08pm UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210 "2015-12-11T23:08:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Invius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Invius](https://forums.unrealengine.com/u/Invius)\
**Post date:** [December 11, 2015, 11:08pm UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/1 "2015-12-11T23:08:53Z")

</div>

Hi guys, i´m studing the delegate types in order to do an async function, and my question is, when i execute the delegate,is it going to create a thread for the called functions?

Im wacthing this exemple:

[https://forums.unrealengine.com/showthread.php…](https://forums.unrealengine.com/showthread.php%E2%80%A6)

If anyone of you has a better ideia on how to do async functions, i would greatly apreciate it if you can show one exemple. I´ve use them in C#, where o can create a thread or put async on the fuction. But here i´m a bit lost.  
Thanks for anyone who can help.

---

<div class="post-metadata">

**Author:** ![getnamo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/getnamo/32/1194624_2.png) [@getnamo](https://forums.unrealengine.com/u/getnamo)\
**Post date:** [December 11, 2015, 11:23pm UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/2 "2015-12-11T23:23:32Z")

</div>

Unless you specifically create a thread or dispatch something in the task graph system, all calls will generally happen on the thread you’re calling from (usually the game thread).

To do multi-threading [this wiki](https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4) is your go to guide.

If you just want to run a one-off task you can run lambdas or functions via a [GraphTask](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Async/FSimpleDelegateGraphTask/CreateAndDispatchWhenReady/1/index.html) function call with nullptr as your pre-requisites e.g.

```
FFunctionGraphTask::CreateAndDispatchWhenReady(InFunctionOrLambda, TStatId(), nullptr, ENamedThreads::AnyThread);

```

where C++ lambda can look like (pass by value)

```
[=]
 {
     //... code here
 }

```

and you can [specify which thread from this reference](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Async/ENamedThreads__Type/index.html).

---

<div class="post-metadata">

**Author:** ![Invius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Invius](https://forums.unrealengine.com/u/Invius)\
**Post date:** [December 12, 2015, 12:33am UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/3 "2015-12-12T00:33:59Z")

</div>

Ok, i think FFunctionGraphTask it is the think im looking for, but how does that work?  
If i call that CreateAndDispatchWhenReady, it will call my function right away?  
Or is it going to stack it to some array of callbacks, and when i call my fuction it will work on the specified thread?  
Thanks for tip anyways.

---

<div class="post-metadata">

**Author:** ![getnamo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/getnamo/32/1194624_2.png) [@getnamo](https://forums.unrealengine.com/u/getnamo)\
**Post date:** [December 12, 2015, 10:36am UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/4 "2015-12-12T10:36:32Z")

</div>

If you have no _FGraphEventArray_ pre-requisites then it will dispatch as soon as possible. I usually specialize this function into something like (in the case of wanting something to call on game thread from an off-thread):

```
FGraphEventRef RunLambdaOnGameThread(TFunction< void()> InFunction)
{
	return FFunctionGraphTask::CreateAndDispatchWhenReady(InFunction, TStatId(), nullptr, ENamedThreads::GameThread);
}

```

and then I just call

```
RunLambdaOnGameThread([=]
{
    //code I want to run on game thread                    
});
```

---

<div class="post-metadata">

**Author:** ![Invius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Invius](https://forums.unrealengine.com/u/Invius)\
**Post date:** [December 12, 2015, 4:44pm UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/5 "2015-12-12T16:44:40Z")

</div>

![70359-img1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/1/5/f15d19124b78be678ffef9956b6d33f9859c7601.png)

I´ve tried to include “TaskGraphInterfaces.h” based on this page:  
[https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Async/FFunctionGraphTask/CreateAndDispatchWhenReady/2/index.html](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Async/FFunctionGraphTask/CreateAndDispatchWhenReady/2/index.html)

But it gives me this error, which .h do i have to include in my .cpp? And i´ve also used FORCEINLINE.

Once again thanks for the time spent with me.

PS it doesnt recognize the FGraphEventRef type.

---

<div class="post-metadata">

**Author:** ![Invius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Invius](https://forums.unrealengine.com/u/Invius)\
**Post date:** [December 13, 2015, 2:50am UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/6 "2015-12-13T02:50:50Z")

</div>

I dont know what i did, but now it compiles, the thing im trying to do, is use a fuction like a event on BP, where we can put delay fuction and the similar thing that i found is FPlatformProcess::Sleep(500);

 ![70394-img1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/4/454d6f2cee3b0e2a2c51f2a23d76f75a66d050dd.png)

And in this image you can see that i am trying to do something like this:

 ![70395-img2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/7/8/8788d465d4f9797daacb032705ee4059d14790c6.png)

Do you have any suggestion? I´m trying to simulate this, because in the future i´ll need to use something similar to delay in BP.

---

<div class="post-metadata">

**Author:** ![getnamo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/getnamo/32/1194624_2.png) [@getnamo](https://forums.unrealengine.com/u/getnamo)\
**Post date:** [December 13, 2015, 10:29am UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/7 "2015-12-13T10:29:54Z")

</div>

Regarding the include, you should just be able to include the Core module instead.

---

<div class="post-metadata">

**Author:** ![getnamo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/getnamo/32/1194624_2.png) [@getnamo](https://forums.unrealengine.com/u/getnamo)\
**Post date:** [December 13, 2015, 10:34am UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/8 "2015-12-13T10:34:10Z")

</div>

For that type of functionality you would be better off using UE’s [SetTimer function](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/FTimerManager/SetTimer/4/index.html). You would call this on the game thread and the function you would pass in would be called when it has finished.

```
World->GetTimerManager().SetTimer(SampleTimerHandle, this, &AMyCharacter::SampleTimerExpired, 10.0f); 

```

and then after 10 seconds it would call e.g.

```
void AMyCharacter::SampleTimerExpired()  
{  
    //do delayed stuff here
} 

```

Edit:  
Just wanted to add that if you wanted to do this using the above lambdas you would need two lambdas. One to run on an off-thread with the sleep and then within that lambda after the sleep another lambda/function call which runs the stuff you want to call when the time has elapsed.

In your above setup the sleep would happen on the game thread, which is bad and it would be called every tick which would spawn an ever increasing queue of waiting for 0.5 sec.

---

<div class="post-metadata">

**Author:** ![Invius](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Invius](https://forums.unrealengine.com/u/Invius)\
**Post date:** [December 13, 2015, 8:20pm UTC](https://forums.unrealengine.com/t/how-to-do-async-fuctions/341210/9 "2015-12-13T20:20:02Z")

</div>

Even if this doesn´t work like i would like it to do, now i know how to make a fuction in a thread. Thanks 🙂

Now i´m testing the KismetSystemLibrary, to use directly the delay function, but i dont understant how to setup the FLatentActionInfo:

 ![70478-img1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/2/4/624ab97fabcc37b84c5e9576806ea8a60fcc4e54.png)
