How replication works?

Can anyone explain to me better how replication works? There is nothing wrong with the anim instance part, in my BP_ThirdPersonCharacter there is a bool variable called bWalkInPlace that, through a flip flop and the B button pressed, i alternate the value between true and false. And in my anim instance this boolean is controlling the animation that my character should perform, this part is working, but I don’t understand why when I press this button on a client, nothing happens. Is my line of reasoning wrong? In my head I understood it this way: I press the B key, and only on the server is the WalkInPlace variable toggled, and as it is marked as Replicated:true, and replication occurs only from the server to the clients, the server sends back to the clients the value of this variable, which in turn will be consumed in the Anim instance and thus change the animation. When I do this on a server-side it works normally, but not the other way around. Could someone explain better how this works for me? (editado)



1 Like

Replication only works Server → Client.

Server sets a “replicated variable” the value of that variable will be replicated to all clients next NetFlush.

1 Like

Clients cannot modify replicated variables. You will have to make a RPC from client to server telling it to change the variables.

1 Like

Typically, for commands that change authoritative state, you will decode the command on the client, and from that decode command (simply the “B” event handler you have) call another event, that is “Run on Server.”
That event will change the variable, which (because it runs on the server) will be replicated to everyone seeing it.
Note: The player controller does NOT replicate, but the player actor DOES. Hence, the player actor should not typically have a reference to the controller; instead, the controller should poke values at the actor (such as Add Input already does for pawns.)

So,
Client: decode input commands, call “Run on Server” events.
Server: implements Run on Server events to update state
Client: notices updated state and changes local behavior

For cases where you need the local player to “feel snappy,” you can speculatively also change the state locally at the same time you send the command to the server, so it will change its behavior right away.

1 Like