codeAPI Reference
Player Component with Static Accessors
Player Component
Main player component that manages health, damage, camera, and interfaces with the inventory system.
Static Accessors
CSHARP
private static Player LocalPlayer { get; set; }
public static Player FindLocalPlayer() => LocalPlayer;
public static T FindLocalWeapon<T>() where T : BaseCarryable =>
FindLocalPlayer()?.GetComponentInChildren<T>(true);
public static T FindLocalToolMode<T>() where T : ToolMode =>
FindLocalPlayer()?.GetComponentInChildren<T>(true);Key Properties
CSHARP
[RequireComponent] public PlayerController Controller { get; set; }
[Property] public GameObject Body { get; set; }
[Property, Range(0, 100), Sync(SyncFlags.FromHost)]
public float Health { get; set; } = 100;
[Property, Range(0, 100), Sync(SyncFlags.FromHost)]
public float MaxHealth { get; set; } = 100;
[Property, Range(0, 100), Sync(SyncFlags.FromHost)]
public float Armour { get; set; } = 0;
[Sync(SyncFlags.FromHost)] public PlayerData PlayerData { get; set; }
public Transform EyeTransform => Controller.IsValid() ? Controller.EyeTransform : default;
public bool IsLocalPlayer => !IsProxy;
public Guid PlayerId => PlayerData?.PlayerId ?? Guid.Empty;
public long SteamId => PlayerData?.SteamId ?? 0;
public string DisplayName => PlayerData?.DisplayName ?? "Unknown";HUD Visibility
CSHARP
/// <summary>
/// True if the player wants the HUD not to draw right now.
/// </summary>
public bool WantsHideHud
{
get
{
var freeCam = Scene.Get<FreeCamGameObjectSystem>();
if (freeCam.IsActive)
return true;
var weapon = GetComponent<PlayerInventory>()?.ActiveWeapon;
if (weapon.IsValid() && weapon.WantsHideHud)
return true;
return false;
}
}IKillSource Implementation
CSHARP
string IKillSource.DisplayName => DisplayName;
long IKillSource.SteamId => SteamId;
void IKillSource.OnKill(GameObject victim)
{
PlayerData.Kills++;
PlayerData.AddStat(victim?.GetComponent<Player>().IsValid() ?? false
? "kills"
: "kills.npc");
}Lifecycle
CSHARP
protected override void OnStart()
{
if (IsLocalPlayer)
LocalPlayer = this;
// Clean up old death camera targets
var targets = Scene.GetAllComponents<DeathCameraTarget>()
.Where(x => x.Connection == Network.Owner);
foreach (var t in targets)
t.GameObject.Destroy();
}
protected override void OnDestroy()
{
if (LocalPlayer == this)
LocalPlayer = null;
}Usage Examples
CSHARP
// Get local player
var player = Player.FindLocalPlayer();
// Get local weapon
var physgun = Player.FindLocalWeapon<Physgun>();
// Get tool mode
var balloonTool = Player.FindLocalToolMode<BalloonTool>();
// Check eye position
var eyePos = player.EyeTransform.Position;
Was this helpful?