Skip to main content

Player

Player in Root.Township (no namespace). A NetworkEntity, and it implements IPlayer (Alta.Networking.Scripts.Player). Both sides.

Each person on a server has one Player entity for as long as they're connected. It holds who they are, their connection, their save, their states and progression. Their body is a separate entity, the PlayerController, that comes and goes (it's destroyed when they die and a new one is spawned when they respawn). See PlayerController and the body.

On the server, every connected player has a Player. On a client, you have your own and one for every other player on the server: the server spawns all Player entities for everyone, in the global chunk.

Getting one​

MemberWhat it gives youNotes
static HashSet<IPlayer> AllPlayers { get; }Every Player entity this side knows about.Cast to Player if you need its non-interface members.
static IPlayer Current { get; }The local player.Client only. null on a server.
static IPlayer Get(int userId)By user id, from a lookup.Filled on the server at join, on a client once that player's identity has arrived.
static Player GetPlayer(int playerId), static bool SafeGetPlayer(int playerId, out Player result)By user id, searching AllPlayers.
static Player GetPlayer(string playerName)By username, any case.
static bool IsOnServer(int playerId)
PlayerController.NetworkPlayerFrom a body.
connection.PlayerFrom a server connection.
foreach (IPlayer player in Player.AllPlayers)
{
var body = player.PlayerController;
string where = body != null ? body.PlayerFeetPosition.ToString() : "no body";
MelonLogger.Msg($"{player.UserInfo.Username}: {player.StateManager.CurrentStateType}, {where}");
}

Members​

MemberWhat it isSide
UserInfoAndRole UserInfo { get; }Who it is. See Identity.Both, but see the warning below.
Connection ConnectionToRemotePlayer { get; }Their connection.Server. null on a client.
bool IsLocalPlayer { get; }This is Player.Current.Client.
PlayerMode PlayerMode { get; set; }VR, fly cam and so on. Raises PlatformChanged when it changes.Both.
PlatformTarget PlatformTarget { get; }PC or Quest.Server.
PlayerController PlayerController { get; }The body, or null while there isn't one.Both.
PlayerCharacter PlayerCharacter { get; }The body cast to the VR body. Throws (invalid cast) if the body is a fly cam.Both.
Transform PlayerTransform { get; }The body's transform if there is one, else the Player's.Both.
PlayerStateManager StateManager { get; }Their states. See Player states.Both.
PlayerProgressionManager ProgressionManager, PlayerUnlockManager UnlockManagerSkills and unlocks.See Progression and Skills and unlocks.
PlayerEventsManager PlayerEventsManager { get; }Gameplay events about this player (armed, and others).
PlayerChunkManager ChunkManager { get; }Which chunks are loaded for them.Server mostly. See Chunk loading and saving.
PlayerCharacterCustomizationManager Customization { get; }Their cosmetics.
FriendshipManager FriendshipManager { get; }Friends (Alta's API, dead).
PlayerSave Save { get; }Their save data. See Saving.Server.
bool IsSavingBlocked { get; }, void AddSaveBlocker(object), void RemoveSaveBlocker(object)Stop their save being written while any blocker is in.Server.
HashSet<Chunk> LoadedChunks { get; }
bool IsLoaded { get; set; }Set when the body's chunk finished loading.
DamageDisplayer DamageDisplayer { get; }
void Kick(string reason)Disconnect them.Server. Does nothing on a client.
Vector3 GetSpawnPosition(bool isRespawning)Where they'd spawn: the saved position on join, the respawn point (or saved home) on respawn, the scene spawn point plus 1.5 m in fly mode.Server.
Task SpawnNewController(PlayerController prefab, bool isRespawn)Spawn a body. Waits for their client to load the chunk first.Server. The states call this.
void SafeMoveToChunk(LocationChunk chunk, Vector3 position, Action callback)Move them into a chunk, waiting for their client to load it. Zeroes their body's senses meanwhile (probably so creatures ignore them) and parks the body in the void chunk.Server, async void.
void DestroyPlayerControllerAndOldEntities(bool isUndocking, bool isUnloading)Destroy the body and what's linked to it.Server.
void SaveControllerAndState(bool isSavingIfDead = false), void WriteSave(bool isUnloading), bool IsInSavableState(bool isSavingIfDead = false)Saving.Server.
static void SaveAll()Save every player.Server.
Task InitializePlayerOnServer(Connection, PlayerMode, PlatformTarget, IAltaFile saveFile)Set up a new Player for a connection.Server. Called by ServerHandler.
void SetLocal()Tell the client this Player is theirs.Server.
UserInfo on a client is wrong at first

On a client, every Player starts with the local user's UserInfo (from the logged-in account), whoever it really is. The real one arrives a moment later in the player's SyncA message, which sets UserInfo and adds it to Player.Get's lookup. So in OnPlayerAdded on a client, UserInfo is not yet right. Wait for the body (PlayerControllerSet), or check again later.

Events​

EventWhenSide
static event Action<Player> OnPlayerAddedA Player entity was set up (Initialize).Both. On the server, before UserInfo is set.
static event Action<Player> OnPlayerRemovedA Player was destroyed.Both.
static event PlayerHandler LocalPlayerSetThe server said which Player is ours. PlayerHandler is void (IPlayer).Client.
event PlayerControllerHandler PlayerControllerSetThe body was set or cleared. Handlers get (Player, PlayerController); the controller is null when it's cleared. Each handler is called on its own, and exceptions are logged.Both.
event Action<PlayerController> FinishedLoadingControllerA new body has been spawned and loaded from the save.Server.
event Action LoadCompleteThe Player loaded its save (states, progression).Server.
event PlatformHandler PlatformChangedPlayerMode changed.Both.
event PlayerSystemHandler PlayerSystemChangedPlatformTarget changed.Both.

For join and leave on the server, use ServerHandler.Current.PlayerJoined and PlayerLeft (ServerHandler). They fire later than OnPlayerAdded, once the player is fully set up.

Every Player's Awake subscribes to the static LocalPlayerSet, and on a server nothing ever raises it to unsubscribe them, so every player who ever joined stays referenced (Steady clears these).

Identity​

UserInfoAndRole​

UserInfoAndRole (no namespace) is what Player.UserInfo and Connection.UserInfo hold.

MemberWhat it is
UserInfo UserInfo { get; }Id and name.
int Identifier { get; }The user id. On TavernLib servers, the id from users.json.
string Username { get; }The name.
PlayerSubscriberStatus MemberStatus { get; }See below.
bool IsSubscriber { get; }Subscriber, Developer or AltaMod.
ToString()(name:id).

UserInfo​

Alta.Api.DataTransferModels.Models.Responses.UserInfo in Alta.Api.DataTransferModels: int Identifier, string Username. Two are equal when their ids are equal, whatever the names.

PlayerSubscriberStatus​

None, Subscriber, Developer, AltaMod (0 to 3). The game checks it with < and >=, so AltaMod passes every >= Developer check: moderators can do what developers can, including joining in fly mode. On a TavernLib server, TavernLib sets it from the player's roles: dev gives Developer, mod gives AltaMod, supporter gives Subscriber. It's sent to every client with the player's identity.

PlayerMode​

PlayerMode (no namespace, in Alta.Platform.dll), a flags enum:

ValueName
0Unassigned
1Fly (keyboard and mouse fly cam)
2AutoCam
4OpenVR (SteamVR)
8Oculus
0x1COpenXR
0x28MetaQuest
0x40PicoXR

PlayerModeUtilities.IsNormalPlayerMode(mode) is false for Fly and AutoCam. Those players aren't saved and don't load a save. Which body each mode gets is on PlayerController and the body.

PlatformTarget​

PC = 1, Quest = 2 (flags, in Alta.Platform.dll). The client sends it in its join request. A server whose ServerInfo.Target includes Quest is a "simple server": NetworkScene.IsSimpleServer, player limit at most 8.

Hooking it​

  • InitializePlayerOnServer: async Task, so a postfix runs at its first await, after UserInfo, ConnectionToRemotePlayer and the save data are set, and before cosmetics load. TavernLib postfixes it to send roles and raise ConsoleEvents.PlayerJoined.
using HarmonyLib;
using MelonLoader;

[HarmonyPatch(typeof(Player), nameof(Player.InitializePlayerOnServer))]
static class OnPlayerInitialized
{
static void Postfix(Player __instance)
{
MelonLogger.Msg($"{__instance.UserInfo.Username} is being set up on connection {__instance.ConnectionToRemotePlayer.Identifier}");
}
}
  • SyncModeControllerAndUserInfoWithRemote(IPlayer, Stream) (private) is where a client learns each player's id, name, mode and status. A postfix there is the reliable "now I know who this is" point on a client.
  • SyncCosmetics is async void. TavernLib replaces it with a prefix to serve cosmetics from the save.
  • StartUp() loads the save on the server (or starts the first-join state for a new player).
  • Kick only works on the server, and the player sees a lost connection, not your reason.

See also​