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.
- Listed in Players and characters
- Inherits or implements
MonoBehaviour - Documented in Player states
Constructors
| Member | What it is |
|---|---|
PlayerStateManager() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
9 private and internal fields
| Member | What it is |
|---|---|
private IPlayerState currentState | Backing field for CurrentState. |
private bool isDuringStateSaveLoading | True while states load from a stream; passed to ValidateStateActivation. |
private bool isDuringStateSaving | True while a state saves to disk. SetState refuses to run then. |
private static Logger logger | NLog logger for this class. |
private Player player | The player these states belong to, set by SetPlayer. |
private uint[] playerSavingData | Pooled buffer the save stream writes into, then handed to the player save. |
private IPlayerStateFactory playerStateFactory | Injected factory that creates states by type. |
private MethodSyncStruct<PlayerStateType> syncStateChange | Method sync for SyncStateChange. |
private MethodSync syncStates | Method sync for SyncStates. |
Properties
| Member | What 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
Events
| Member | What it is |
|---|---|
event PlayerStateHandler StateActivated | Raised after a state's Activate runs. |
event PlayerStateHandler StateDeactivated | Raised after a state's Deactivate runs while leaving it. |
event PlayerStateHandler StateMadeCurrent | Raised after a new state becomes current, whether or not it was freshly activated. |
Methods
| Member | What 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
| Member | What 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: