S&box Wiki

Revision Difference

Pawn#545802

<cat>Code.Player</cat> <title>Pawn</title> # What is a Pawn A Pawn is an Entity any Client (player) can possess, this is the physical representation of your player within the world. This entity doesn't need to be anything specific, you can make it a physics object or a traditional animated player. When a Client possesses a Pawn additional methods become active on the Entity: - `Simulate( Client )` - Called each tick serverside and clientside, this is <page text="predicted">Prediction</page>. - `FrameSimulate( Client )` - Called each frame on the client only to simulate things that need to be updated every frame. - `BuildInput( InputBuilder )` - Called every frame on the client to process input for your pawn. - `PostCameraSetup( ref CameraSetup )`⤶ - <page text="Entity.Simulate( Client )">Sandbox.Entity.Simulate</page> - Called each tick serverside and clientside, this is <page text="predicted">Prediction</page>. - <page text="Entity.FrameSimulate( Client )">Sandbox.Entity.FrameSimulate</page> - Called each frame on the client only to simulate things that need to be updated every frame. - <page text="Entity.BuildInput( InputBuilder )">Sandbox.Entity.BuildInput</page> - Called every frame on the client to process input for your pawn. - <page text="Entity.PostCameraSetup( ref CameraSetup )">Sandbox.Entity.PostCameraSetup</page>⤶ There are several examples on how to use pawns in various ways: - [sbox-minimal](https://github.com/Facepunch/sbox-minimal/blob/master/code/MinimalPlayer.cs) - a basic standard player pawn - [sbox-netlab](https://github.com/Facepunch/sbox-netlab/blob/master/code/LabPawn.cs) - a more complex pawn controlling it's own animations and movement # Setting a Client's Pawn You can set the Client's Pawn at any point, a standard place to set it would be when the client first joins. ```csharp /// <summary> /// A client has joined the server. Make them a pawn to play with /// </summary> public override void ClientJoined( Client client ) { base.ClientJoined( client ); var player = new MinimalPlayer(); client.Pawn = player; player.Respawn(); }⤶ ```⤶ ⤶ # Accessing Pawns⤶ ⤶ A good way to get all pawns in the game is to iterate through all clients and access their pawns:⤶ ⤶ ```csharp⤶ // Loop through every Client (remember, these are our pawn owners)⤶ foreach ( var client in Client.All )⤶ {⤶ // Always check if the pawn is assigned and the correct type⤶ if ( client.Pawn is not MyPawn pawn )⤶ continue;⤶ ⤶ pawn.Respawn();⤶ }⤶ ```⤶ ⤶ Any time you want to access your pawn clientside you can do so through Local.Pawn:⤶ ⤶ ```csharp⤶ public class MyHealth : Panel⤶ {⤶ Label healthLabel;⤶ ⤶ // blah blah⤶ ⤶ public override void Tick()⤶ {⤶ healthLabel.Text = $"Health: {Local.Pawn.Health}";⤶ }⤶ } ```