Async Task Crash

I would like to run a process asynchronously and execute a delegate when it is completed, when this happens between operations an audio is played.
Code is like:

AsyncTask(ENamedThreads::AnyThread, [this]()
{
    ...
   AsyncTask(ENamedThreads::GameThread, [this]()
	{
		ChunkState = EChunkState::Recognized;
		OnChunkComplete.ExecuteIfBound(this);
	});
})

I also tried adding a nexttick on the bounded function.

GetWorld()->GetTimerManager().SetTimerForNextTick([this]{
    ....
    AudioComponent->Play();
});

but crashes occur on the function:
IsInGameThread()
FTimerHandle FTimerManager::InternalSetTimerForNextTick(FTimerUnifiedDelegate&& InDelegate): 533

or
IsInGameThread() [File:D:\build++UE5\Sync\Engine\Source\Runtime\Engine\Private\AudioDevice.cpp] [Line: 5925]

How I can solve this crashes?

Thanks

1 Like

in both cases as in the info from the functions - check Game Thread failed.

and as a result, I would suggest that you can’t play Audio in not in the Game Thread.
Audio will be played in the separated thread anyway, but before this it wants some Game Thread preparation - I guess to synchronize relevant data.

and not only in your callback and all Game Thread relative function calls, you need to make sure that they will be on Game Thread.

Audio must be played in a GameThread, that is obvious, cause that is the error. The question seems to be as to how, rather than if. Here is how you would make sure this task is in a game thread. You can put this AsyncTask in any code block.

AsyncTask(ENamedThreads::GameThread, [AudioComponent, this]()
{
AudioComponent->Play();
});

Hope that helps, :wink: