StateMachine<TKey, TState> Class

Summary

A simple Finite State Machine system that registers each state with a particular key.
Assembly
Animancer.dll
Namespace
Animancer.FSM
Interfaces
Base Types
Derived Types
graph BT Type-->Base0["StateMachine<TState>"] Base0-->Base1["Object"] Type-.->Interface0["IStateMachine"] click Interface0 "/animancer-v7-4/api/Animancer.FSM/IStateMachine" Type-.->Interface1["IKeyedStateMachine<TKey>"] Type-.->Interface2["IDictionary<TKey, TState>"] Type["StateMachine<TKey, TState>"] class Type type-node Derived0["StateMachine<TKey, TState>.WithDefault"]-->Type click Derived0 "/animancer-v7-4/api/Animancer.FSM/WithDefault"

Syntax

[HelpURL(StateExtensions.APIDocumentationURL + nameof(StateMachine<TState>) + "_2")]
[Serializable]
public class StateMachine<TKey, TState> : StateMachine<TState>, IStateMachine, 
    IKeyedStateMachine<TKey>, IDictionary<TKey, TState>
    where TState : class, IState

Remarks

This class allows states to be registered with a particular key upfront and then accessed later using that key. See Animancer.FSM.StateMachine`1 for a system that does not bother keeping track of any states other than the active one.

See Animancer.FSM.StateMachine`2.InitializeAfterDeserialize if using this class in a serialized field.

Documentation: Keyed State Machines

Attributes

Type Description
HelpURLAttribute
SerializableAttribute

Type Parameters

Name Description
TKey
TState

Constructors

Name Summary
StateMachine() Creates a new Animancer.FSM.StateMachine`2 with a new Animancer.FSM.StateMachine`2.Dictionary, leaving the CurrentState null.
StateMachine(IDictionary<TKey, TState>) Creates a new Animancer.FSM.StateMachine`2 which uses the specified `dictionary`, leaving the CurrentState null.
StateMachine(IDictionary<TKey, TState>, TKey, TState) Constructs a new Animancer.FSM.StateMachine`2 which uses the specified `dictionary` and immediately uses the `defaultKey` to enter the `defaultState`.
StateMachine(TKey, TState) Constructs a new Animancer.FSM.StateMachine`2 with a new Animancer.FSM.StateMachine`2.Dictionary and immediately uses the `defaultKey` to enter the `defaultState`.

Properties

Name Value Summary
Count int
Gets the number of states contained in the Animancer.FSM.StateMachine`2.Dictionary.
CurrentKey TKey
The key which identifies the Animancer.FSM.StateMachine`1.CurrentState.
Dictionary IDictionary<TKey, TState>
The collection of states mapped to a particular key.
GUILineCount int
[Editor-Only] The number of standard size lines that Animancer.FSM.StateMachine`1.DoGUI will use.
Keys ICollection<TKey>
Gets an System.Collections.Generic.ICollection`1 containing the keys of the Animancer.FSM.StateMachine`2.Dictionary.
NextKey TKey
The Animancer.FSM.KeyChange`1.NextKey.
PreviousKey TKey
The Animancer.FSM.KeyChange`1.PreviousKey.
this[TKey] TState
The state registered with the `key` in the Animancer.FSM.StateMachine`2.Dictionary.
Values ICollection<TState>
Gets an System.Collections.Generic.ICollection`1 containing the state of the Animancer.FSM.StateMachine`2.Dictionary.

Methods

Name Value Summary
Add(KeyValuePair<TKey, TState>) void
Adds a state to the Animancer.FSM.StateMachine`2.Dictionary.
Add(TKey, TState) void
Adds a state to the Animancer.FSM.StateMachine`2.Dictionary.
AddRange(TKey[], TState[]) void
Adds the specified `keys` and `states`. Both arrays must be the same size.
Clear() void
Removes all state from the Animancer.FSM.StateMachine`2.Dictionary.
Contains(KeyValuePair<TKey, TState>) bool
Determines whether the Animancer.FSM.StateMachine`2.Dictionary contains a specific value.
ContainsKey(TKey) bool
Determines whether the Animancer.FSM.StateMachine`2.Dictionary contains a state with the specified `key`.
CopyTo(KeyValuePair<TKey, TState>[], int) void
Copies the contents of the Animancer.FSM.StateMachine`2.Dictionary to the `array` starting at the `arrayIndex`.
DoGUI(Rect) void
[Editor-Only] Draws GUI fields to display the status of this state machine in the given `area`.
ForceSetState(TKey) TState
Uses Animancer.FSM.StateMachine`2.ForceSetState(`0,`1) to change to the state registered with the `key`. If nothing is registered, it use null and will throw an exception unless Animancer.FSM.StateMachine`1.AllowNullStates is enabled.
ForceSetState(TKey, TState) void
Calls Animancer.FSM.IState.OnExitState on the Animancer.FSM.StateMachine`1.CurrentState then changes to the specified `key` and `state` and calls Animancer.FSM.IState.OnEnterState on it.
GetEnumerator() IEnumerator<KeyValuePair<TKey, TState>>
Returns an enumerator that iterates through the Animancer.FSM.StateMachine`2.Dictionary.
GetState(TKey) TState
Returns the state registered with the specified `key`, or null if none is present.
InitializeAfterDeserialize() void
Call this after deserializing to properly initialize the Animancer.FSM.StateMachine`1.CurrentState.
Remove(KeyValuePair<TKey, TState>) bool
Removes a state from the Animancer.FSM.StateMachine`2.Dictionary.
Remove(TKey) bool
Removes a state from the Animancer.FSM.StateMachine`2.Dictionary.
SetFakeKey(TKey) void
Sets the Animancer.FSM.StateMachine`2.CurrentKey without changing the Animancer.FSM.StateMachine`1.CurrentState.
ToString() string
Returns a string describing the type of this state machine and its Animancer.FSM.StateMachine`2.CurrentKey and Animancer.FSM.StateMachine`1.CurrentState.
TryGetValue(TKey, TState) bool
Gets the state registered with the specified `key` in the Animancer.FSM.StateMachine`2.Dictionary.
TryResetState(TKey) TState
Attempts to enter the state registered with the specified `key` and returns it if successful.
TryResetState(TKey, TState) bool
Attempts to enter the specified `state` and returns true if successful.
TrySetState(TKey) TState
Attempts to enter the state registered with the specified `key` and returns it if successful.
TrySetState(TKey, TState) bool
Attempts to enter the specified `state` and returns true if successful.