Async() function usage

I want to create a background task with using Async function : (Async | Unreal Engine Documentation)

I read and follow the documentation but i’ve got an compile error. This is the code:

int32 TestFunc()
{
	GEngine->AddOnScreenDebugMessage(0,5,FColor::Red,TEXT("This is an async task"));
	return 123;
}

TUniqueFunction<int32()> Task = TestFunc;

// Called when the game starts or when spawned
void ATempActor::BeginPlay()
{
	Super::BeginPlay();
	auto Result = Async(EAsyncExecution::Thread,Task);
}

There is no intellisense error. And the compile error is :

Async.h(297): [C2280] ‘TUniqueFunction<int32 (void)>::TUniqueFunction(const TUniqueFunction<int32 (void)> &)’: attempting to reference a deleted function

How can i use Async() function corectly? Thx a lot in advance.

TUniqueFunction is non-copyable. Use MoveTemp

auto Result = Async(EAsyncExecution::Thread, MoveTemp(Task));
1 Like

Thx so much, it works. I wanna create a task in background. And in that task, i want to create timer that increase a value every one second. Is that possible? I could not handle this with using Async() and ASyncTask() methots. GetWorld() and GetWorldTimerManager() funcitons does not work in that methots. Thanks a lot again

Creating a task just to make it fire a timer doesn’t seem like a good idea. Additionally, it requires synchronization of access to data shared between threads.

Async/background jobs is mainly used for time-consuming, heavy tasks.