Download

Passing parameters to a timer

Hi, is there a way to pass three parameters to a timer? Because I want to pass the camera location, rotation and forward vector and I want the timer to be on the server for security purposes.

Here’s a quick example of how you can pass multiple parameters using a timer.


class MyClass
{
	void TimeDelayFunction(float Var1, bool bVar2);

	FTimerHandle StartTimeDelay(float Delay, float FloatVar, bool bBoolVar);

	FTimerManager* MyTimerManager;

	// other class stuff goes here
}

FTimerHandle MyClass::StartTimeDelay(float Delay, float FloatVar, bool bBoolVar)
{
	FTimerHandle TimerHandle;
	MyTimerManager->SetTimer(TimerHandle, FTimerDelegate::CreateUObject(this, &MyClass::TimeDelayFunction, FloatVar, bBoolVar), Delay);
	return TimerHandle;
}

This is a great answer, thanks :slight_smile:

I’m getting: No instance of overloaded function “FTimerManager::SetTimer” matches the argument list argument types are: (FTimerHandle, TBaseDelegate<void>, float)
object type is: FTimerManager.

My mistake. In this version of SetTimer you need to pass an additional bool after Delay. The bool is optional in some of the other overloads but not this one. The bool should be true if you want the timer to loop and false otherwise.

Thanks, now it works. But how can I call this timer and stop it with the timer handles?

You can stop the timer by calling



MyTimerManager->ClearTimer(TimerHandle);


To start it again you can call SetTimer again.

Yes, but the Timer Handle is a function, so I need to call the Timer Handle function, or I need to call SetTimer?

Intellisense wasn’t getting errors, but when I compile, I get:

FTimerManager::SetTimer : no overloaded function takes 6 arguments.

Here is a thread about the same question: SetTimer with parameters

Let me link a piece of code from that thread:



void AMyGameMode::RespawnPlayerWithDelay(APlayerController * Player)
{
	const float Interval = 3.f;
	const bool Loop = false;
	const float FirstDelay = 0.f;
	FTimerHandle UniqueHandle;
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject( this, &AMyGameMode::RespawnPlayer, Player );
	GetWorldTimerManager().SetTimer( UniqueHandle, RespawnDelegate, Interval, Loop, FirstDelay );
}


Its essentially the same as what Fred K posted, but seeing the FTimerDelegate on a seperate line may clear things up for you. **Edit: ** Added more variables to the example code for sake of readability.

An FTimerHandle is not a function, it is simply an ID that you create to reference a timer that you create with SetTimer(). An FTimerDelegate in the above code is a wrapper around a function plus any arguments that should be passed to it. Now, by calling SetTimer with a created FTimerHandle and FTimerDelegate, it results in the function call represented by FTimerDelegate to be called after a delay/periodically based on the other parameters you pass to SetTimer. If you want to stop the timer later, you need to store the FTimerHandle and call ClearTimer(MyTimerHandle) where MyTimerHandle is the handle you called SetTimer with before. Be aware that in the above example the FTimerHandle isn’t being stored, because the timer just triggers once and does not need to be stopped.

Thanks. Now it works perfectly.