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 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.
Alternatively, you can use CreateWeakLambda, and use a lambda function that takes a TWeakObjectPtr then internally calls ApplyDamage() with the dereferenced pointer.
@Jambax 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.