Skip to main content

PlayerStateManager

A class in Root.Township, Alta.PlayerStates.

Component on each player that holds its current state (first join, alive, dead and so on) as a stack of nested states, switches between them, syncs changes to clients and saves them. Get it with Player.StateManager.

Constructors​

MemberWhat it is
PlayerStateManager()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

9 private and internal fields
MemberWhat it is
private IPlayerState currentStateBacking field for CurrentState.
private bool isDuringStateSaveLoadingTrue while states load from a stream; passed to ValidateStateActivation.
private bool isDuringStateSavingTrue while a state saves to disk. SetState refuses to run then.
private static Logger loggerNLog logger for this class.
private Player playerThe player these states belong to, set by SetPlayer.
private uint[] playerSavingDataPooled buffer the save stream writes into, then handed to the player save.
private IPlayerStateFactory playerStateFactoryInjected factory that creates states by type.
private MethodSyncStruct<PlayerStateType> syncStateChangeMethod sync for SyncStateChange.
private MethodSync syncStatesMethod sync for SyncStates.

Properties​

MemberWhat it is
IPlayerState CurrentState { get; private set; }The innermost active state. Its base states are reached through BaseState.
PlayerStateType CurrentStateType { get; private set; }Type of the current state, or None if there is none.
bool IsLeavingCurrentState { get; private set; }True while a state is being deactivated during a switch.
1 private and internal property
MemberWhat it is
private string PlayerName { get; }The player's username for log messages, or the object name if there's no user info.

Events​

MemberWhat it is
event PlayerStateHandler StateActivatedRaised after a state's Activate runs.
event PlayerStateHandler StateDeactivatedRaised after a state's Deactivate runs while leaving it.
event PlayerStateHandler StateMadeCurrentRaised after a new state becomes current, whether or not it was freshly activated.

Methods​

MemberWhat it is
void LoadFrom(PlayerSave save)Rebuilds the state stack from the player save, or starts the default state if none was saved.
void SaveTo(PlayerSave save)Writes the whole state stack to the player save.
void SetPlayer(Player player)Binds the manager to its player and creates the network method syncs. Call before loading.
void SetState(PlayerStateType stateType, PlayerStateHandler beforeStateLeaveCallback = null, PlayerStateHandler beforeStateEnterCallback = null)Switches to a state by type or instance. Shared base states are kept, others deactivated. Server validates the new state and sends the change to chunks.
void SetState(IPlayerState newState, PlayerStateHandler beforeStateLeaveCallback = null, PlayerStateHandler beforeStateEnterCallback = null)Switches to a state by type or instance. Shared base states are kept, others deactivated. Server validates the new state and sends the change to chunks.
void StartWithDefaultState()Sets the player into FirstJoinState. Used when there's no saved state.
void SyncStatesToRemotePlayer(Player remotePlayer)Sends the full state stack to one remote player, if there is a current state.
protected void Inject(IPlayerStateFactory playerStateFactory)VContainer injection of the factory that creates states by type.
12 private and internal methods
MemberWhat it is
private void DeactivateCurrentStateAndFindCommonBase<T>(IPlayerState leavingState, T newState)Walks up from the leaving state, deactivating each state until it reaches one the new state is built on, which it decorates instead.
private uint[] DoubleDataSize(uint[] oldData, ref uint wordIndex)Grows playerSavingData to twice its size from the array pool while the save stream writes.
private void EnterNewState(IPlayerState newState, StreamWriter stateSyncingStream, PlayerStateHandler beforeStateEnterCallback)Makes the state current, server syncs the change to chunks, activates it if needed and fires the events.
private IPlayerState GetCurrentStateOrBase(PlayerStateType stateType)Finds the current state or one of its base states with the given type, or null.
private StreamWriter GetSyncStreamForLeavingState(IPlayerState leavingState)Server: writes a leaving state's leave data for a remote player. Nothing calls it.
private void LoadFromStream(Stream stream, Action<IPlayerState, Stream> serializationMethod)Reads a stack of state types, creates and loads each, then sets them in order; stops if one forwards elsewhere.
private void SerializeDisk(Stream stream)Writes or reads the whole state stack in the disk save format.
private void SerializeStateToDisk(IPlayerState state, Stream stream)Saves or loads one state for disk, blocking SetState while it runs.
private void SerializeStateToRemote(IPlayerState state, Stream stream)Writes or reads one state in the format sent to other players.
private void SyncStateChange(IPlayer player, Stream stream, PlayerStateType stateType)Syncs a new state type; remote clients call SetState with it.
private void SyncStates(IPlayer player, Stream stream)Syncs the full state stack to a remote player, who loads it on their client.
private void WriteToStream(Stream stream, Action<IPlayerState, Stream> serializationMethod)Writes the state stack from the bottom up: count, each type, then each state's data.

Used in​

Members of other types that take, return or hold a PlayerStateManager: