top of page

Templates for writing Movement code for Online Multiplayer in Unreal Engine 4

Updated: Sep 23, 2019

Writing movement code that works in online multiplayer games in Unreal Engine 4 is not a very well documented topic, so I figured I would write my own tutorial/templates for doing this. I use these as starting points whenever I add a new mode, since it can be difficult to remember all the details.

These are designed to work even with huge amounts of network latency, frame rate issues, etc., which was a difficult task!


Important Note: While I use examples of sprinting and getting up ledges in these templates, I have not included the full gameplay programming implementations of them. The primary goal of these templates is to outline the structure of networked movement code, so I didn't want to include superfluous content. I would recommend becoming familiar with UE4 C++ and gameplay programming code first if you want to make best use of this.


Also, I begun this project by looking at this tutorial. If you see any similarities between it and my code that's why, but my code adds a lot of functionality onto it. If you are new to UE4 network coding I'd recommend starting there or by taking this Udemy course.


Section 1: General Needs


Create a character class (We'll call it AMyCharacter). Create a movement component class (We'll call it UMyMovement).


I generally derive these classes from ACharacter and UCharacterMovementComponent respectively, but this might not be wanted if you are making a character that isn't humanoid, for example.


In you custom (character) movement component class, which I called UMyMovement:

1. In the header, create new subclasses for Saved moves and Prediction data


Section 2: Template for Movement Modifiers (e.g. sprinting):

You'd think creating network-friendly code for something as common as sprinting would be easy, but there are a million ways to do it and many have pitfalls. This is the way that has worked best for me.


In your character class (AMyCharacter):

1. in GetLifetimeReplicatedProps have a bool for the modifier to be replicated:

2. Bind an action to a function that sets this bool to true/false in the movement component (feel free to implement this differently in the case where it isn't being enabled via direct player input).

You might also want separate functions for gamepad input. Something like sprinting might want to behave differently with a gamepad -- you would want to hold shift to sprint on KBM, but toggling sprint (e.g. with left stick click) on gamepad might be better.


3. Create functions to set the replicated variable:

4. Create a getter to ask if the modifier is active.

5. Create OnStartModifier/OnStopModifier functions that will handle setting the replicated variable on the server.

In you custom (character) movement component class, which I called UMyMovement:


1. Create a new variable for saving the movement modifier. Be sure to override the saved move’s clear() function and clear the saved move’s values.

2. Create a new variable in the movement component for your new movement type

3. We will save and communicate this state using a compressed flag, so we need to override both GetCompressedFlags and UpdateFromCompressedFlags. Since we have limited flags, you don't want to overuse these, but they're a great option for storing a bunch of critical movement flags.

5. Optionally, override CanCombineWith if you want to be able to compress more flags at some point. In this example we will just be using a simple one-bit-per-flag approach, so this isn’t needed.


6. Override SetMoveFor and PrepMoveFor, and AllocateNewMove – these are used for storing move information locally, which is then sent to the server to be replayed.

8. Create SetSprinting function.

9. Override UpdateCharacterStateBeforeMovement and create associated functions for starting/stopping sprint to make sure both the client and server start sprinting after receiving info that the player wants to sprint.

10. Use the bWantsToSprint variable for the actual gameplay code.

Section 3: Template for Contextually Activated Custom Movement Modes (e.g. ledge getups, climbing, wall run):


Let's say your character collides with a platform's edge and you'd like them to climb up it. Since this is a contextually activated action rather than a simple input-initiated change of state, you can't use the same structure.

I think this solution works really well, but it is not a subject I have seen implemented online, so let me know if you have a different way of achieving this.


In the Character class (AMyCharacter):

I actually don't feel any code is essential in this file for this kind of networked movement!


In the CharacterMovement class (AMyMovement):

1. Create a new enum to be used for your custom movement modes:

2. Create a variable for your saved move class, e.g. uint8 bSavedWantsToLedgeGetup : 1; and make sure it gets set to false in your saved move’s Clear() override:

3. Override ApplyNetworkMovementMode in the movement component and add the appropriate code that is needed for executing the movement properly on simulated proxies. In the case of a ledge getup, this is as simple as playing the getup animation.

4. Override ServerMove to see if the controlling client has requested a movement mode change.

5. Add to your code a place to initiate a new movement state. For the ledge getup, I opted to override PhysFalling so it would be checked on every physics tick, but since physics tick is called very frequently, you should consider better places to put this code on a case-by-case basis.

6. Override PhysCustom so you can define how the player automatically moves in your new mode. Create a new function for physics simulation during your new move.

7. Implement other movement functions as necessary. Since a ledge getup might be entirely automatic after touching a ledge, you might not need additional control, but if you want to allow the player to hang from or climb along a ledge at their own discretion, you would need to add more code. Maybe in MoveForward/MoveRight if you are going off of the UE4 Third Person Character template.


Conclusion

That's it! Hopefully these templates can help you create lag- and stutter-free multiplayer gameplay. Please let me know if any parts are incomplete or unclear.

bottom of page