How to do async fuctions?

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…

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.

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 is your go to guide.

If you just want to run a one-off task you can run lambdas or functions via a GraphTask 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.

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.

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                    
});

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

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.

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

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

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.

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

For that type of functionality you would be better off using UE’s SetTimer function. 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.

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 :slight_smile:

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