Download

Pass arguments to GetWorldTimerManager().SetTimer function

Hello.

I need to call a function after a certain ammount of time, so I decided to use GetWorldTimerManager().SetTimer(…);

The problem is that the function I want to call expects a const FHitResult& as parameter.

How do I call this function from the SetTimer, passing an argument?

I’ve tried this:



FTimerHandle ExplosionTimer;
FTimerDelegate ExplosionDelegate = FTimerDelegate::CreateStatic(this, &AProjectile_Explosives::Explode, **FHitResult()**);
GetWorldTimerManager().SetTimer(ExplosionTimer, ExplosionDelegate, ProjectileLifeTime, false);


Where FHitResult() is the object to pass to the function.

But it doesn’t work.

Any tip?

Try using CreateUObject instead of CreateStatic, since AProjectile_Explosives::Explode is not a static method.

Already tried that, same erros.

07bab062562d7a67a2f11b1f5977b7ffddc41972.jpeg

Ah, I think I know why it’s not working. Delegates can only accept parameters that are passed by value, not references (because there no way to tell if the referenced value would still exist when the delegate is called) and the Explode() method expects a reference.

Try using a lamdba like this:



FTimerHandle ExplosionTimer;
FTimerDelegate ExplosionDelegate = FTimerDelegate::CreateLambda(=]()
{
	this->Explode(FHitResult());
});

GetWorldTimerManager().SetTimer(ExplosionTimer, ExplosionDelegate, ProjectileLifeTime, false);

The code inside the lambda will execute when the delegate is fired and then it will call Explode().

Hello OP, I ran across the same GetWorldTimerManager() error, and this is how I solved it:


GetWorld->GetTimerManager().SetTimer(ExplosionTimer, ExplosionDelegate, ProjectileLifeTime, false);

I believe the timers and events programming documentation is out of date.

Please fix Epic! =)

Thanks dude, I was going crazy.


 
 GetWorldTimerManager().SetTimer 
 

This still works, just add the include:


#include "TimerManager.h"