Class StateMachineMb
Implements
Inherited Members
Namespace: Fwt.Core.StateMachines
Assembly: fwt.core.dll
Syntax
public abstract class StateMachineMb : MonoBehaviour, IStateMachine
Fields
CurrentState
Declaration
public ISmState CurrentState
Field Value
| Type | Description |
|---|---|
| ISmState |
SortedStates
Declaration
public Dictionary<Type, SmStateMb> SortedStates
Field Value
| Type | Description |
|---|---|
| Dictionary<Type, SmStateMb> |
States
Declaration
public List<SmStateMb> States
Field Value
| Type | Description |
|---|---|
| List<SmStateMb> |
Methods
Awake()
Declaration
protected virtual void Awake()
GetState(int)
Retrieves the state at the specified index within the state collection.
Declaration
public ISmState GetState(int stateIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | stateIndex | The zero-based index of the state to retrieve. Must be greater than or equal to 0 and less than the total number of states. |
Returns
| Type | Description |
|---|---|
| ISmState | The state at the specified index if it exists; otherwise, |
Remarks
If the state collection is null or the specified index is out of range, the
method returns null.
GetState(Type)
Retrieves the state instance associated with the specified type.
Declaration
public ISmState GetState(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type for which to retrieve the corresponding state. Cannot be null. |
Returns
| Type | Description |
|---|---|
| ISmState | The state instance associated with the specified type, or |
GetState<TState>(Type)
Retrieves the state object of the specified type and attempts to cast it to the requested state interface.
Declaration
public TState GetState<TState>(Type type) where TState : ISmState
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type of the state object to retrieve. |
Returns
| Type | Description |
|---|---|
| TState | An instance of |
Type Parameters
| Name | Description |
|---|---|
| TState | The state interface type to return. Must implement ISmState. |
OnValidateStates()
Declaration
[ContextMenu("Setup States")]
protected virtual void OnValidateStates()
Start()
Declaration
protected virtual void Start()
SwitchState(ISmState)
Transitions the state machine to the specified new state.
Declaration
public void SwitchState(ISmState newState)
Parameters
| Type | Name | Description |
|---|---|---|
| ISmState | newState | The state to switch to. If |
Remarks
This method prepares the current state to stop before switching, and prepares the new state to run after the transition. If the new state is null, the state machine will have no active state after the call.
SwitchState(int)
Transitions the state machine to the state at the specified index.
Declaration
public ISmState SwitchState(int stateIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | stateIndex | The zero-based index of the state to switch to. Must correspond to a valid state within the state machine. |
Returns
| Type | Description |
|---|---|
| ISmState | The state object representing the new active state after the transition. |
SwitchState<TState>()
Switches the state machine to the specified state type and returns the new state instance if successful.
Declaration
public TState SwitchState<TState>() where TState : ISmState
Returns
| Type | Description |
|---|---|
| TState | An instance of |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of state to switch to. Must implement ISmState. |
Remarks
If the requested state type is not available or cannot be switched to, the method
returns the default value for TState. This method is typically used to transition the
state machine to a specific state and obtain a strongly-typed reference to it.
SwitchState<TState, TContext>(TState, TContext)
Transitions the state machine to the specified new state, providing the associated context for the state.
Declaration
public void SwitchState<TState, TContext>(TState newState, TContext context) where TState : ISmStateWithContext<TContext>
Parameters
| Type | Name | Description |
|---|---|---|
| TState | newState | The state to transition to. If equal to the current state, no transition occurs. |
| TContext | context | The context object to associate with the new state. This is set on the state before it is started. |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state to transition to. Must implement ISmStateWithContext<TContext>. |
| TContext | The type of the context object to be associated with the new state. |
Remarks
If the current state is not null, it is prepared to stop before the transition. The new state's context is set before it is prepared to run. No action is taken if the new state is the same as the current state.
SwitchState<TState, TContext>(TContext)
Declaration
public TState SwitchState<TState, TContext>(TContext context) where TState : ISmStateWithContext<TContext>
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context |
Returns
| Type | Description |
|---|---|
| TState |
Type Parameters
| Name | Description |
|---|---|
| TState | |
| TContext |
Tick()
Advances the state machine by invoking the tick operation on the current state, if one is set.
Declaration
public virtual void Tick()
Remarks
Call this method periodically to update the state machine's behavior. If no current state is assigned, this method has no effect.