S&box Wiki

Revision Difference

Prediction#548769

<cat>Code.Network</cat> <title>Prediction</title> 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. # 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. ``` /// <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 ) { 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. # Culling⤶ During prediction all sounds, particles and RPCs will get culled. Imagine you shoot a gun and that makes a sound. Since your code is predicted, it will run on both realms and the server will assume that the sound has already played for your client and cull it. ⤶ Using `Prediction.Off()` lets you ignore culling:⤶ ```cs⤶ # 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)⤶ ⤶ 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() ) { // this will play twice: first clientside and later once the server sends this sound to everyone⤶ PlaySound("gun.shoot");⤶ // Play sound here⤶ } ```