How to properly use GetWorldTimerManager with replicated functions

Like the title says, im having a maddening issue with the GetWorldtimerManger.SetTimer when using it to call a replicated function for my grenade actor. As of right now it displays very funny behavior… It will sometimes work, and sometimes it will not. Video and code included. Notice that the grenade actor properly spawns on both of my clients 100% of the time properly, but the timer im setting right after it to call the delayed explosion only works “sometimes”, there has to be something im doing wrong somewhere. Please any help will be greatly appreciated.

This was originally a reddit question on /r/unrealengine
The videos and images can be found at this link. https://www.reddit.com/r/unrealengine/comments/m6q6go/please_assist_how_to_properly_use/

Are you 100% sure you really need to use replication system for that explosion effect?

Generally all visual effects (like particles) should run locally in reaction to replicated gameplay code, because they don’t need to be perfectly synced, you only wasting bandwidth.

That explosion effect right now is being NetMulticasted so it will display to everyone playing the match. My problem is, the explosion function(that calls said multicasted playeffects), is only being called occasionally, that includes the server side functionality of adding radial damage.

Is the multicast set to be “Reliable”?

Yes it is currently set to reliable.

So I figured this out after about two weeks of tirelessly searching the internet to no avail. Turns out my code was completely correct and need no real modification. The issue I was having was that Under the RootComponent in my blueprint the initial Life span was not set to 0(which defaults to never dying on a timer) it was set to 3.0. Turns out that my GetWorldTimerManager.SetTimer was using a timer of 3.0 as well. So I was ending up with a strange race condition between when my explosion would trigger on the server vs when it would automatically kill itself via life span. This needs to be archived for the future because this bug was a nightmare.

I’m not sure how to troubleshoot the problem, but an alternative to a timer you might consider, is to instead set the life span variable (inherited from Actor base class I think) to a certain amount that you want before the grenade explodes, and then cause the explosion to be called from the grenade’s destructor.
Life span if set to anything above zero, will trigger the actor’s destructor when it expires.

This approach should work as long as you are okay with grenades exploding no matter what any time they are destroyed, or add conditions for whether it should explode when destroyed.

thanks for your response, I actually just figured this issue out last night and am about to post my solution to the issue.

This problem ended up being a nasty race condition between my actors initial Life Span and the world timer I was trying to set. Turns out I was setting my world timer handle to 3.0f seconds before the explosion function was called. I also unwittingly had the initial life span of the actors root component set to 3.0 seconds inside my characters blueprint. This is what was resulting in the “it works every now and then, but not all the time”. Problem. My solution was to just turn the Initial Life Span attribute under my blueprint actor to 0 (which sets it to never die). After that, my world timer would be able to correctly call the function of the grenade actor.

So you were already using the lifespan, but when it came time to implement the explosion, you used a timer on top of that. Whichever expires first wins. Glad you were able to find it!

watch this