Revision Difference
Pawn#560470
<cat>Code.Player</cat>⤶
⤶
<title>Pawn</title>⤶
<deprecated></deprecated>⤶
# 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(IClient)) - Called each tick serverside and clientside, this is [predicted](Prediction).⤶
- [Entity.FrameSimulate( Client )](https://asset.party/api/Sandbox.Entity.FrameSimulate(IClient)) - 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).⤶
⤶
⤶
# 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( IClient 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 Game.Clients )⤶
{⤶
// 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 Game.LocalPawn:⤶
⤶
```csharp⤶
public class MyHealth : Panel⤶
{⤶
Label healthLabel;⤶
⤶
// blah blah⤶
⤶
public override void Tick()⤶
{⤶
healthLabel.Text = $"Health: {Game.LocalPawn.Health}";⤶
}⤶
}⤶
```⤶