S&box Wiki

Revision Difference

Lag_Compensation#546218

<cat>Code.Network</cat> <title>Lag Compensation</title> # Introduction Lag compensation happens server-side during `Simulate` and essentially rewinds lag compensated entities to the position they were in when the client being simulated sent their input commands. This means that traces will hit entities that the client would expect to be hit from their point-of-view. # Enabling If you want to enable it for any entities, such as a pawn, you can simply set the `LagCompensation` property to true on that entity. This is usually does in the entity's `Spawn` method. # Traces⤶ Within `Simulate` you can call `UseLagCompensation()` on any trace to enable lag compensation for that trace and any subsequent traces until the end of the simulation. ⤶ ## Example Usage⤶ ⤶ The following code could be used within a weapon's `PrimaryAttack` method. This is because `PrimaryAttack` is called within `Simulate` and so this trace would be lag compensated.⤶ ⤶ **Note: all subsequent traces will also be affected by lag compensation until the end of the simulation.**⤶ ⤶ ```csharp⤶ var bulletTrace = Trace.Ray( Owner.EyePos, Owner.EyePos + Owner.EyeRot.Forward * 5000f )⤶ .UseLagCompensation()⤶ .UseHitboxes()⤶ .Ignore( Owner )⤶ .Ignore( this )⤶ .Size( radius )⤶ .Run();⤶ ``` If you want to enable it for any entities, such as a pawn, you can simply set the `EnableLagCompensation` property to true on that entity. This is usually does in the entity's `Spawn` method. # Usage⤶ Within `Simulate` you can do `using ( LagCompensation() ) { }` and within that scope the transforms of all eligible entities will be lag compensated. This means any traces or physics queries you run within that scope will yield the rewound positions and rotations for those entities. ⤶ By default, the `BaseWeapon` class included in the base addon already has `PrimaryAttack` and `SecondaryAttack` enclosed in a block just like this.