Use FTimerManager outside UE class

Hello,

So we have a thread that every two seconds should excecute a function.
This thread got a “Strategy” attribute which launch a timer.

class TWITCHTEST_API Anarchy : public Strategy
{
public:
	 
	Anarchy(UWorld* w, BlockingQueue<FString>* q) : Strategy(q) {
		world = w;
		world->GetTimerManager().SetTimer(TimerHandle, this, &Anarchy::OnTick, 2, false);
	};

	FTimerHandle TimerHandle;
	UWorld* world;

	void Receive() override;
	void OnTick() override;

};

During compilation we got this error :
" You cannot use UObject method delegates with raw pointers. "

Is there a way to use FTimerManager in a none UE class, or should we use an other timer from third party library or std ?

1 Like

Use a Lambda. Besides that you need to set looping to true if you want it to fire every 2 seconds

world = w;
FTimerDelegate del;
del.BindLambda([this] {
	// Logic here or call the Function (this needs to be Captured like in this example)
	OnTick();
});
world->GetTimerManager().SetTimer(TimerHandle, del, 2, true);

It works perfectly , thank you !

thanks for this!