Problem With FTimerHandle overload

Hi everyone,

I am trying to loop a “Apply damage” event on a 5 second loop when the character overlaps with my component sphere. I can get the character to operlap and take damage, but I am having a bit of a problem attaching a timer handle to the function to have the point damage keep being applied on a 5 second loop.

Attached are the error messages I received when calling the GetWorld()->GetTimerManager().SetTimer method.

You’re missing a ‘f’ in your SetTimer call in your Rate argument.

You’re trying to call a function that requires an AActor OtherActor* parameter - but how is the timer supposed to know what parameter to pass in?

You have to (safely) store a reference to that character until the timer expires. The easiest way to do that is use the data payload of the timer delegate, which you can do by using a variation of SetTimer.

The problem is, you also want to pass a UObject. There’s no garauntee that the object won’t have been destroyed or GC’d by the time the timer expires, so you need to make the parameter a TWeakObjectPtr, and pass a TWeakObjectPtr and check the validity of it inside that function.

Example:

GetWorld()->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateUObject(this, &MyClass::SomeFunction, MakeWeakObjectPtr(Target)), 5.f, false);

void MyClass::SomeFunction(TWeakObjectPtr<AActor> Target)
{
	if (AActor* TheTarget = Target.Get())
	{
		// Do Damage
	}
}

Alternatively, you can use CreateWeakLambda, and use a lambda function that takes a TWeakObjectPtr then internally calls ApplyDamage() with the dereferenced pointer.

Thank you @Dutii. I just saw that and made the adjustment.

@TheJamsh Your analysis and solution are absolutely on the money. Also becasue of your feedback I began watching videos on function pointers and lamda functions. Thank you very much for the time you took to reply to my question. Your feedback has been extremely helpful.

@TheJamsh Your suggestion helped me find a solution.

I created an AActor in the header file to store the OtherActor that came off of the Overlapped function.

I then used the FTimerHandle and SetTimer function to call the ApplyPoisonGasDamage()

Once again @Dutii and @TheJamsh you guys both rock! Thank you guys for your help.