Async function still stops game

Hi,
I am trying out the Async Framework and it seems even with all that the function is still executing on game thread as my game sleeps(stops) for 8 seconds which I used to simulate the delay on another thread.

here is my code:

FString AAsyncFutureExample::AsyncFoo()
{
	int para =1;
	TFunction<FString()> bla = [para](){ FPlatformProcess::Sleep(8.0f); return FString::FromInt(para);};
	
	auto A = Async(EAsyncExecution::Thread, bla);

	return A.Get();
}

I call this blueprint Callable Function on begin paly and it stops my game for 8 seconds then it throws the 1 output.

Try using a async task w/ background thread:
AsyncTask(ENamedThreads::AnyBackgroundThreadNormalTask, …);

You can also create a async blueprint node using UBlueprintAsyncActionBase:
Creating Asynchronous Blueprint Nodes - Old UE4 Wiki (nerivec.github.io)

1 Like

If you call AsyncFoo(), then this is to be expected, because AsyncFoo() is not Asynchronous itself, but it calls one inside, which means it will only return after the async function called inside returns from sleep to return the value at the A variable, resulting in BeginPlay to have to wait for AsyncFoo() to complete after the sleep time.

2 Likes

Ahh, So a delegate would be useful here I suppose.
I can pass in a DYNAMIC_DELEGATE with parameter and ExecuteIfBound() inside the Async.

I will try this and get back.

This is a really nice resource I will try a few methods and see what works.