S&box Wiki

Revision Difference

Pawn#548354

<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: - [Entity.Simulate( Client )](https://asset.party/api/Sandbox.Entity.Simulate(Client)) - Called each tick serverside and clientside, this is [predicted](Prediction). - [Entity.FrameSimulate( Client )](https://asset.party/api/Sandbox.Entity.FrameSimulate(Client)) - Called each frame on the client only to simulate things that need to be updated every frame. - [Entity.BuildInput()](https://asset.party/api/Sandbox.Entity.BuildInput()) - Called every frame on the client to process input for your pawn. More info [here](https://wiki.facepunch.com/sbox/Input_System). - [Entity.PostCameraSetup( ref CameraSetup )](https://asset.party/api/Sandbox.Entity.PostCameraSetup(CameraSetup)) # Setting a Client's Pawn # 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}"; } } ```