In this lesson, you will learn how the CharacterMovementComponent (CMC) orchestrates physics and state synchronization to maintain a consistent simulation across a network.
The Physics-Replication Loop
The CMC is unique because it isn't just a component; it is an authoritative simulation engine. It manages movement by splitting execution into two paths: Predicted Movement (on the Client) and Authoritative Correction (on the Server).
At the heart of this architecture is the MovementMode (e.g., MOVE_Walking, MOVE_Falling, MOVE_Swimming). This mode acts as a state machine that dictates which physics logic is applied during the PerformMovement() tick.
Managing State Synchronization
Because the client performs "Client-Side Prediction" to hide latency, the server must be able to verify that the client’s position is valid. The architecture achieves this through a FSavedMove_Character structure. When the client moves, it saves its input and predicted state into this structure, sends it to the server via an RPC, and the server re-runs the simulation using that exact input.
If the server’s result deviates from the client’s predicted result beyond a threshold, the server forces a correction:
// Internal snippet illustrating the reconciliation flow
void UCharacterMovementComponent::ServerMove_Implementation(...)
{
// 1. Re-simulate the move based on saved input
// 2. Validate position against the authoritative state
// 3. If invalid, trigger ClientAckGoodMove or ClientAdjustPosition
}
The Role of Prediction
By keeping track of PendingAdjustment and ClientPredictionData, the CMC can identify "mismatches." If the client is currently at location A, but the server calculates location B, the server sends a ClientAdjustPosition RPC. The client then "snaps" to the server’s result and re-plays any local moves that occurred after the time the error was detected. This allows for smooth movement despite the latency inherent in networking, ensuring that while the client is always predicting the future, the server remains the final arbiter of truth.
Key Takeaway: The CharacterMovementComponent architecture relies on a deterministic state machine where clients predict movement locally, while the server validates those inputs and enforces corrections to ensure the authoritative state remains synchronized.