Instant event on client, then replicate to server and other clients?

Hypothetically suppose I have a flashlight I want to activate in a multiplayer game.

Client A COULD input activate, run an activation event on the server, and then the server could use a RepNotify to set the flashlight Boolean to ON on each client.

However, this means there would be a delay (ping / pkt lag) between the player inputting the flashlight, and it actually appearing in their game.

How can I have the player activate the flashlight INSTANTLY on their game, THEN send it to the server, and then have that flashlight activate on all other clients (after the ping / pkt lag?)

i.e. client A inputs activate, flashlight is turned on for client A instantly, an RPC is sent to the server which then RepNotifies the new flashlight Boolean to ALL CLIENTS EXCEPT the player who did the activating? How do I exclude this player from acting on the RepNotify?

I am working in blueprints only in Unreal 5.2

Thank you!

Do this, just On input → switch has auth (remote) : Set rep_notify var → call Server RPC.

Doing a local set will activate it on the autonomous client, then the server will set the authoritative rep notify.

If you duplicate your rep_notify variable (e.g. FlashLightLastState) then compare it against the updated rep state you can use that as a gate control.

So OnRep Function
Flashlight != FlashLightLastState → update FlashLightLastState → Do something, else ignore.

Sometimes with onreps it’ll reset the state then apply the new state.
e.g. flashlight toggling off then back on.


Don’t worry about not sending it to the owning client. A bool is 2 bits in a packet that’ll be a few hundred bits.

You could apply the “Skip Owner” condition, but I don’t recommend it. Consider packet loss, server being the authority etc.

Why do you need to skip the action on the triggering client?

Also, a trick that’s often helpful, is to replicate “ticks of state change” rather than “state.”
In this case, you’d have two variables:

  1. Tick number when light was last switched on
  2. Tick number when light was last switched off
    The lamp is on, if value 1 is greater than value 2.
    (and it doesn’t need to be tick numbers, could be game time or whatever distributed, shared, advancing quantity your clients/server can agree on.)

Client would set “tick last switched on” to “current tick” when turning it on, and send RPC to server. Server would echo it back. When client gets the update, the value will be “set” to the same value again, idempotently.

Even if there is some glitch in transmission, the state will be eventually consistent. Lamp is on whenever “turn on tick” is later than “turn off tick,” and vice versa.

1 Like