Thanks a lot!
This is my code that finally works:
AMyPawn, not much going on there. Only
- Replicate Movement: false, no need to replicate location or anything
- Replicated property: velocity, this together with the tick function takes care of correct movement
Not a fan of updating parameters on tick. It can cause desync between client and server
This is valuable advice indeed. As this is just a multiplayer learning experience, I simply removed the dampening from the tick function such that velocity only ever changes when I hit a button. If I were to implement dampening and I just had to change the velocity in the tick function, I would chose not to replicate velocity
and instead, I would use a client rpc to make sure AccelerateLeft
and AccelerateRight
get executed on the client.
And MyPlayerController.h and MyPlayerController.cpp
Two design choices here:
- I created an enum for all the actions. This is unfortunately redundant with unreals action binding system, but I use the
EAction
enum inSetupInputComponent
to define only once what will happen at both client and server. This way, I can’t accidently move left on the client and right on the server due to some copy-paste mishap BindAction
then takes care of registering both cases, server and client. This implies use of closures, otherwise I can’t use theEAction
parameter.HandleAction
is where stuff actually happens andServerRPC_HandleAction
only wraps that one.