StateMachine<TKey, TState> Class

Summary

A simple Finite State Machine system that registers each state with a particular key.

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.
Assembly
Animancer.dll
Namespace
Animancer.FSM
Interfaces
Base Types
graph BT Type-->Base0["StateMachine<TState>"] Base0-->Base1["Object"] Type-.->Interface0["IDictionary<TKey, TState>"] Type["StateMachine<TKey, TState>"] class Type type-node

Syntax

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

Attributes

Type Description
HelpURLAttribute

Type Parameters

Name Description
TKey
TState

Constructors

Name Summary
StateMachine() Constructs a new Animancer.FSM.StateMachine`2 with a new Animancer.FSM.StateMachine`2.Dictionary.
StateMachine(IDictionary<TKey, TState>) Constructs a new Animancer.FSM.StateMachine`2 which uses the specified `dictionary`.
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 current state.
Dictionary IDictionary<TKey, TState>
The collection of states mapped to a particular key.
IsReadOnly bool
Indicates whether the Animancer.FSM.StateMachine`2.Dictionary is read-only.
Keys ICollection<TKey>
Gets an System.Collections.Generic.ICollection`1 containing the keys of the Animancer.FSM.StateMachine`2.Dictionary.
this[TKey] TState
Gets or sets a particular state 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 an `array` starting at the specified `arrayIndex`.
ForceSetState(TKey) TState
Uses Animancer.FSM.StateMachine`2.ForceSetState(`0,`1) to change to the state mapped to the `key`. If nothing is mapped, it changes to default(TState).
ForceSetState(TKey, TState) void
Calls Animancer.FSM.IState`1.OnExitState on the current state then changes to the specified key and state and calls Animancer.FSM.IState`1.OnEnterState on it.

Note that this method does not check Animancer.FSM.IState`1.CanExitState(`0) or Animancer.FSM.IState`1.CanEnterState(`0). To do that, you should use Animancer.FSM.StateMachine`2.TrySetState(`0,`1) instead.
GetEnumerator() IEnumerator<KeyValuePair<TKey, TState>>
Returns an enumerator that iterates through the Animancer.FSM.StateMachine`2.Dictionary.
GetState(TKey) TState
Returns the state associated with the specified `key`, or null if none is present.
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 actually changing the state.
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 associated with the specified `key` in the Animancer.FSM.StateMachine`2.Dictionary.
TryResetState(TKey) TState
Attempts to enter the specified state associated with the specified `key` and returns it if successful.

This method does not check if the `key` is already the Animancer.FSM.StateMachine`2.CurrentKey. To do so, use Animancer.FSM.StateMachine`2.TrySetState(`0) instead.
TryResetState(TKey, TState) bool
Attempts to enter the specified `state` and returns true if successful.

This method does not check if the `state` is already the Animancer.FSM.StateMachine`1.CurrentState. To do so, use Animancer.FSM.StateMachine`2.TrySetState(`0,`1) instead.
TrySetState(TKey) TState
Attempts to enter the specified state associated with the specified `key` and returns it if successful.

This method returns true immediately if the specified `key` is already the Animancer.FSM.StateMachine`2.CurrentKey. To allow directly re-entering the same state, use Animancer.FSM.StateMachine`2.TryResetState(`0) instead.
TrySetState(TKey, TState) bool
Attempts to enter the specified `state` and returns true if successful.

This method returns true immediately if the specified `state` is already the Animancer.FSM.StateMachine`1.CurrentState. To allow directly re-entering the same state, use Animancer.FSM.StateMachine`2.TryResetState(`0,`1) instead.