Partial Player component static helpers. Stores a static reference to the local player and provides convenience methods to get the local player, local weapon or tool mode, and to find players by Connection or player Guid via the scene.
public sealed partial class Player : Component, Component.IDamageable, PlayerController.IEvents, Global.ISaveEvents, IKillSource
{
private static Player LocalPlayer { get; set; }
public static Player FindLocalPlayer() => LocalPlayer;
public static T FindLocalWeapon<T>() where T : BaseSandboxWeapon => FindLocalPlayer()?.GetComponentInChildren<T>( true );
public static T FindLocalToolMode<T>() where T : ToolMode => FindLocalPlayer()?.GetComponentInChildren<T>( true );
/// <summary>
/// Find a player for this connection
/// </summary>
public static Player FindForConnection( Connection c )
{
return Game.ActiveScene.GetAll<Player>().FirstOrDefault( x => x.Network.Owner == c );
}
/// <summary>
/// Get player from a connection id
/// </summary>
public static Player For( Guid playerId )
{
return Game.ActiveScene.GetAll<Player>().FirstOrDefault( x => x.Network.Owner?.Id == playerId );
}
}