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?
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.
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):
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);
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.
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.