Using SetTimer() on a Function with Parameters

I am currently trying to implement a method that calls 2 functions. The catch is I don’t want the second one to run until the animation of the first has finished playing.

I am trying to unequip a weapon then equip a new one. Each weapon in the game has its own holster/equip animation, that’s why I need to play the animations separately.

I have been trying to do it by:

GetWorldTimerManager().SetTimer(this, &AArenaCharacter::EquipWeapon, Duration, false);

The problem is AArenaCharacter::EquipWeapon(class AArenaRangedWeapon* NewWeapon) takes a parameter in, so the timer gets upset. Does anyone know of a way I can have the timer run a function that takes in parameters?

8 Likes

It is possible using FTimerDelegate as described here: SetTimer with parameters - C++ - Unreal Engine Forums

void AMyGameMode::RespawnPlayerWithDelay(APlayerController * Player, float Delay)
{
	FTimerHandle UniqueHandle;
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject( this, &AMyGameMode::RespawnPlayer, Player );
	GetWorldTimerManager().SetTimer( UniqueHandle, RespawnDelegate, Delay, false );
}
7 Likes

it’s possible to do the same thing but with two parameters instead of only one?

it’s possible to do the same thing but with two parameters instead of only one?

1 Like

Hello, of course it’s possible to have the requested functionality you seek! Inside your header file type in the following code:

UFUNCTION()
void MyUsefulFunction(int32 x, float y);

Then, inside your source file:

FTimerDelegate TimerDel;
	
FTimerHandle TimerHandle;

int32 MyInt = 10;
float MyFloat = 20.f;

//Binding the function with specific values
TimerDel.BindUFunction(this, FName("MyUsefulFunction"), MyInt, MyFloat);
//Calling MyUsefulFunction after 5 seconds without looping
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDel, 5.f, false);

Don’t forget to include the MyUsefulFunction logic in your source file as well:

void AMyActor::MyUsefulFunction(int32 x, float y)
{
	GLog->Log("x="+FString::FromInt(x));
	GLog->Log("y=" + FString::SanitizeFloat(y));
}

For more information about delegates, check out the official documentation and my blog post here

-Orfeas

21 Likes

Thanks for your answer i use it and it’s working but i have a issue.
I have a loop timer and my parameter changed from different function but that Delegate just use initial value ! is it possible to use dynamic parameter .

In you example you used a local variable, but i using a public variable and it’s changing from different functions.

1 Like

Yes, as FTimer::CreateUObject signatures says:

inline static TBaseDelegate CreateUObject(UserClass* InUserObject, typename TMemFunPtrType::Type InFunc, VarTypes… Vars)

so you can pass multiple parameters.