How to create a task in game thread and wait for it?

Make a static function in you .h file

static FGraphEventRef RunLambdaOnGameThread(TFunction< void()> InFunction);

In CPP

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

Now you can run a function on the game thread.

void YourClassName::YourFunc(params)
{
	RunLambdaOnGameThread([capture any vars by ref or by value] {
              // Your func Implementation
       });
}

Hope this helps.

2 Likes