S&box Wiki

Revision Difference

BasePlayer#529734

<cat>Code.Player</cat>⤶ <title>BasePlayer</title>⤶ ⤶ This is WIP.⤶ ⤶ Knowledge is based on the usage in the sample gamemodes. (I'm not sure if this doc will be approved, but lets find out.)⤶ ⤶ # What is BasePlayer⤶ ⤶ BasePlayer is an abstract class to define Player behaviour and functions. You can override Methods of BasePlayer to implement custom logic.⤶ ⤶ # Abstract Methods⤶ ## void Respawn()⤶ Handles what happens on respawn. It's smart to bind the Player Animator, Player Controller and Camera here.⤶ ⤶ ### Example: Basic spawn⤶ ```cs⤶ public override void Respawn()⤶ {⤶ SetModel( "models/citizen/citizen.vmdl" );⤶ ⤶ //⤶ // Use WalkController for movement (you can make your own PlayerController for 100% control)⤶ //⤶ Controller = new WalkController();⤶ ⤶ //⤶ // Use StandardPlayerAnimator (you can make your own PlayerAnimator for 100% control)⤶ //⤶ Animator = new StandardPlayerAnimator();⤶ ⤶ //⤶ // Use FirstPersonCamera (you can make your own Camera for 100% control)⤶ //⤶ Camera = new FirstPersonCamera();⤶ ⤶ EnableAllCollisions = true;⤶ EnableDrawing = true;⤶ EnableHideInFirstPerson = true;⤶ EnableShadowInFirstPerson = true;⤶ ⤶ base.Respawn();⤶ }⤶ ```⤶ ⤶ Source: [sbox-minimal MinimalPlayer.cs](https://github.com/Facepunch/sbox-minimal/blob/5bc57dd049b20bbf81ea0602780ccb40099e3aac/minimal/code/MinimalPlayer.cs#L9-L34)⤶ ⤶ ⤶ ## void Tick()⤶ Called every tick, clientside and serverside. You can handle user's inputs here.⤶ ⤶ ## void OnKilled()⤶ Will be called when player was killed. If you want to stop the player from moving, you should set the `Controller` to `null` in here.⤶ ⤶ ## void UseFail()⤶ Guess: `UseFail` will be called when the player presses the "use" button, but the interaction fails (for whatever reason).⤶ ⤶ By default this plays a sound. ⤶ ⤶ ## void OnActiveChildChanged( Entity from, Entity to )⤶ Called when the item in the player's hand changed. For example: the player changes his current weapon.⤶ ⤶ ### Example: Disable current weapon's flashlight on weapon change⤶ ```cs⤶ public override void OnActiveChildChanged( Entity from, Entity to )⤶ {⤶ if ( to is Weapon weapon && HasFlashlightEntity )⤶ {⤶ ShowFlashlight( false );⤶ }⤶ ⤶ base.OnActiveChildChanged( from, to );⤶ }⤶ ```⤶ ⤶ Source: [dm98 Player.cs](https://github.com/Facepunch/sbox-hidden/blob/5e72e9d310ee4ab12bbd9fea59c39005af95fefb/code/player/Player.cs#L182-L190)⤶ ⤶ ⤶ ## void PostCameraSetup( Camera camera )⤶ WIP, used in [dm98 Player.cs](https://github.com/Facepunch/dm98/blob/2bebd230696b2a95b53225c22f722545ae41894d/code/Player.cs#L149-L183)⤶ ⤶ ## void TakeDamage( DamageInfo info )⤶ Gets called when the player takes damage.⤶ ⤶ Don't forget to call ``base.TakeDamage( info );`` here if you want the player to take damage.⤶ ⤶ ### DamageInfo⤶ - `DamageInfo::Damage` - float, the damage amount⤶ - `DamageInfo::HitboxIndex` - integer, the hitbox index (obviously), 0 = head⤶ ⤶ ⤶ ## PlayerController GetActiveController()⤶ WIP, used in [sandbox Player.cs](https://github.com/Facepunch/sandbox/blob/d251c6b6b39f57bba0df7caba1fe81fce06faac8/code/Player.cs#L61-L67)⤶ ⤶ ## Camera GetActiveCamera()⤶ WIP, used in [sandbox Player.cs](https://github.com/Facepunch/sandbox/blob/d251c6b6b39f57bba0df7caba1fe81fce06faac8/code/Player.cs#L69-L75)⤶ ⤶ ## PlayerAnimator GetActiveAnimator()⤶ WIP, used in [sandbox Player.cs](https://github.com/Facepunch/sandbox/blob/d251c6b6b39f57bba0df7caba1fe81fce06faac8/code/Player.cs#L77-L82)⤶ ⤶ ## void StartTouch( Entity other )⤶ Called when the player entity touches another entity. ⤶ ⤶ ## override bool HasPermission( string mode )⤶ Some sort of custom permission check. Used in [sandbox Player.cs](https://github.com/Facepunch/sandbox/blob/d251c6b6b39f57bba0df7caba1fe81fce06faac8/code/Player.cs#L170-L178)⤶ ⤶ ⤶ # Examples & References⤶ ⤶ - [sbox-hidden Player.cs](https://github.com/Facepunch/sbox-hidden/blob/main/code/player/Player.cs)⤶ - [dm98 Player.cs](https://github.com/Facepunch/dm98/blob/master/code/Player.cs)⤶ - [sandbox Player.cs](https://github.com/Facepunch/sandbox/blob/master/code/Player.cs)⤶ - [sbox-minimal MinimalPlayer.cs](https://github.com/Facepunch/sbox-minimal/blob/master/minimal/code/MinimalPlayer.cs)⤶