S&box Wiki

Revision Difference

Prediction#551234

<cat>Code.Network</cat> # What is Prediction Prediction is a technique in online games improving gameplay smoothness by calculating what will happen next instead of waiting for the server to respond. It runs input logic locally, compares it with the server's response, and corrects any discrepancies. In other words, the client tries to make a best guess of what the server's response will be, in order to show things sooner. Prediction is linked to [lag compensation](Lag_Compensation), but is separate from [interpolation](Networking/Interpolation). # Why use Prediction Prediction removes delay between player input and game response, reducing lag. For example, in a first-person shooter game, shooting a weapon can appear instantaneous (we can draw particles, sound effects, animations), rather than having a noticeable delay due to server processing. # What Should be Predicted When predicting something, it **must** be deterministic (must always produce the same results if given the same inputs), yielding consistent results on both client and server. **What can be predicted** Anything involving direct [Input](Input_System) of the player interacting with networked entities. - Movement code - Weapon variables (ammo count, animations) - [Traces](Traces) **What can't be predicted** Anything that can't be deterministically calculated. - Dynamic physics # Prediction Errors Prediction errors occur when client actions calculate a result that differs from the server's, causing inconsistent gameplay. These errors, caused by poor coding, network issues, etc., signify differences between server and client, with the server always retaining authority over networked values. ## Fixing Prediction Errors 1. Identify the property that was mis-predicted. The prediction error should reference the property name. ⤶ 2. Find all the code segments that alter the property, and ensure that they run consistently on both the client and server. ⤶ 3. Check whether all the logic is predictable and running on both the client and server in a predictable context. 2. Find all the code segments that alter the property, and ensure that they run consistently on both the client and server. 3. Check whether all the logic is predictable and running on both the client and server in a predictable context. # Prediction in Practice The main entry point for prediction is the `Simulate( IClient cl )` function in your GameManager class. By default, the client's pawn will have its Simulate function called, which is where the player's actions can be predicted. ``` public override void Simulate( IClient cl ) { cl.Pawn?.Simulate( cl ); } ``` Any entity that has the predicted client as its Owner can be Simulated this way. So, for example, you might Simulate an active weapon in your pawn's Simulate method to allow the weapons to fire in a predicted way. Predictable properties can be created by using the [Predicted] attribute: ``` [Net, Predicted] public float Stamina { get; set; } = 100; ``` Predicted properties can then be Predicted in any valid predictable context: ``` public override void Simulate( IClient client ) { if ( Input.Down( InputButton.Run ) ) Stamina -= Time.Delta * 10f; } ``` # Specific Behavior Effects like sounds and particles are locally simulated on the client. For sounds already heard by the client, server instructions via RPCs are culled to avoid repetition. ## Disabling Prediction For logic that branches off from the main simulation loop but cannot be predicted, you'd turn off prediction within a specific scope: ``` using ( Prediction.Off() ) { // Non-predictable logic } ``` ## First Time Predicted The client may sometimes re-run predicted ticks to try and correct predicted properties. In this instance, client-side effects might be run multiple times. To avoid replaying client-side effects during prediction correction, you can use `Prediction.FirstTime`. ``` if ( Prediction.FirstTime ) { // Play client-side effects } ```