S&box Wiki

Revision Difference

Prediction#549151

<cat>Code.Network</cat> <title>Prediction</title> ⤶ # What is Prediction?⤶ Imagine you shoot a gun in the game. Without prediction you would press the mouse button, it'd send a message to the server, the server would run the command, the gun would shoot on the server, it'd send sounds and effects down to the client, then you'd see the effects happen. We're talking about a 30ms round trip at best, half a second at worst. It would be noticeable. With prediction it runs the same, but while it's waiting for the server it predicts what's going to happen. It runs the command clientside, so everything is instant. With prediction it does the same thing, but while it's waiting for the server it predicts what's going to happen. It runs the command clientside, so everything appears instant. ⤶ # What Should be Predicted?⤶ ⤶ Generally speaking anything that involves your pawn's movements or actions should be predicted. For example, movement code should match on both client and server. In your weapons, you should make things like ammo count predictable. It also makes sense to predict animations.⤶ ⤶ You can only reliably predict things that are deterministic (i.e. things that can be consistently calculated with the same end result on both client and server).⤶ ⤶ For one reason or another anything involving the physics engine typically isn't very deterministic, so you can't use physics in a reliably predictable fashion. Physics in this context doesn't include deterministic things like raycasts; a raycast straight down from 100 units will always hit the world with the same result whether you're on the client or the server. However, if you're raycasting from or into a physically simulated body, that's when you may run into difficulties. ⤶ # Prediction Errors When the server runs the Tick and sends the results to the client, the client can compare the entity and it's predicted variables to the actual results from the tick that ran on the server. ⤶ If any of the values are different then we have a prediction error. So the client copies all the variables from the server and re-runs any subsequent ticks to try and fix it all. ⤶ # What should be predicted? ⤶ Anything that changes your pawn's position or rotation is predicted. Movement code should match and be deterministic on both client and server. The physics aren't deterministic so you can't use physics in a predictable fashion. ⤶ In your weapons, you should make things like ammo count predictable. ⤶ # Where is Prediction?⤶ ⤶ The main entry point for prediction is in your game class. This is the default behaviour if you don't override it.⤶ When the server runs the tick and sends the results to the client, the client compares these results to what it had predicted for that tick. If any of the values are different, then a "prediction error" occurs. In that case, the client copies all the variables from the server and re-runs any subsequent ticks to try and fix it all. ⤶ The prediction error that appears in scary red text in your console isn't an error in the typical sense - it doesn't mean that anything "broke". It's merely there to tell developers that they probably didn't design their code properly. Save for extreme cases, at worst, prediction errors will just result in jittery or inconsistent gameplay. ⤶ Prediction has a certain degree of [tolerance](https://developer.valvesoftware.com/wiki/Prediction), and for certain data types like floats, the value that the server calculates doesn't have to match the value the client calculates exactly. In these cases, a prediction error is not shown. However, the client probably still takes the values from the server, just to be safe. ⤶ It is unusual to get prediction errors when testing in the editor. This is because there is no (or very very little) network latency in the editor, so in theory prediction errors should not be happening. If you are getting prediction errors in the editor, then your code is very likely not handling prediction properly. ⤶ Outside of the editor, some prediction errors are to be expected due to things like lag spikes, dropped packets, and other stuff that you cannot control. The difference between prediction errors caused by bad code and prediction errors caused by external factors is that prediction errors caused by bad code will be reproducible in a consistent fashion (e.g. only when a certain button is pressed, or only when crouching). Prediction errors caused by external factors may still occur more commonly in certain scenarios, but they will not be consistently reproducible. Prediction errors caused by bad code may also occur more often. ⤶ ## Fixing Prediction Errors⤶ ⤶ A prediction error just means that you calculated some value differently on the client than you did on the server. To fix it, you have to do the following:⤶ ⤶ 1) Find what variable was mis-predicted. The prediction error should tell you this.⤶ ⤶ 2) Find all the places in your code where that variable is written to. You can do this easily in most code editors by right clicking on the variable and clicking "Find All References", or whatever your editor's equivalent is.⤶ ⤶ 3) In each place the variable is being written to, ask yourself: _"Is this code being run on both the client and the server? Is it being calculated in the same way, with the same values?"_. What happens if you comment out that code? Add logging to confirm it's using the values you think it's using.⤶ ⤶ # How do you use Prediction?⤶ ⤶ The main entry point for prediction is the `Simulate` function in your game class.⤶ ⤶ This is the default behaviour if you don't override it:⤶ ``` /// <summary> /// Called each tick. /// Serverside: Called for each client every tick /// Clientside: Called for each tick for local client. Can be called multiple times per tick. /// </summary> public override void Simulate( Client cl ) public override void Simulate( IClient cl ) { cl.Pawn?.Simulate( cl ); } ``` As you can see the default behaviour is to call Simulate on the client's Pawn. In this function you should do movement and input. If an entity has your Pawn as the owner it becomes predictable too. So, for example, you might call `Weapon.Simulate( cl )` in your pawn's Simulate method to allow the weapons to fire. ⤶ ⤶ # Turning off Prediction⤶ Imagine again if you shoot a gun, but this time it plays a sound. ⤶ The sound would play:⤶ - On the client shooting the gun (in this case, you)⤶ - On the server (heard by everyone else)⤶ As you can see the default behaviour is to call `Simulate` on the client's pawn. This is the function that you should predict the player's actions in. The idea is that `Simulate` is called on both client and server, and both run (roughly) the same code. Because they run the same code, the variables (e.g. player position) should match between client and server. If an entity has your pawn as the owner it becomes predictable too. So, for example, you might call `Weapon.Simulate( cl )` in your pawn's `Simulate` method to allow the weapons to fire in a predicted way. ⤶ By default, things like player position and rotation are predictable variables. You can also make your own predictable variables by using the `[Predicted]` attribute:⤶ ⤶ ```⤶ [Net, Predicted] public float Stamina { get; set; } = 100; ``` ⤶ ⤶ You could then predict it like this in your player class:⤶ ⤶ ```⤶ public override void Simulate( IClient client )⤶ {⤶ if ( Input.Down( InputButton.Run ) )⤶ Stamina -= Time.Delta * 10f;⤶ }⤶ ```⤶ ⤶ # Turning off Prediction⤶ Imagine again if you shoot a gun. Normally, this would play a sound. The sound would play:⤶ ⤶ - On the client shooting the gun (in this case, you).⤶ - On the server (heard by everyone else).⤶ The server assumes that the client has already heard the sound, so doesn't send it again. This same concept applies to RPCs. Sometimes, however, you might want to force the client to hear a sound, for example, if you shot something and someone died; in these cases, you can turn prediction off like so: ``` using ( Prediction.Off() ) { // Play sound here } ```