How to properly decrease a variable every tick in RPC

  • The character has an Energy variable;
  • Everytime it moves (AxisInput MoveForward, MoveRight, MoveUp) this energy will be consumed (decreased).
  • Currently, when energy is consumed, we call the “ConsumeEnergy(float Value)”(Server RPC) method. So, when it’s a client, we’ll call as RPC.
  • The server can’t follow the number of updates from AxisInput tick rate, resulting that the client-character will have it’s energy reduced slower than the server-character (cause the server won’t need a RPC to consume energy).

How to properly synch with the server a variable that updates every tick? Also, the updated value must be used to check if the player has energy to move, so at the same time we update the value, we must received the correctly updated value.

You inherently can’t keep a value that is updated each frame on the client in sync with a server that is some ms in time behind the client. You have to find a way to hide the fact that there is some lag between the two. You also don’t want to clog your network with a per-frame update.

You should likely have the Server figure out if the player is moving ( Either just sending a simple bool that says they have some controller input going and then again when it turns off, or determine it based on velocity - although you may run into situations, like falling, where you don’t want to drain energy). Have the client operate on its own copy of the value, but the server should regularly stomp it whenever the client gets an updated server value to prevent the values from diverging too much.