Skip to content

Finite State Machine Library for .NET Core (with EASY and ADVANCED implementation)

License

Notifications You must be signed in to change notification settings

Dentrax/Finite-State-Machine

Repository files navigation

Finite State Machine Public Source Repository

What It Is

How To Use

Requirements

About

Collaborators

Branches

Copyright & Licensing

Contributing

Contact

What It Is

Finite State Machine with .NET Core

Finite State Machine guide for .NET Core language is an easy and advanced way to learn how algorithms works in theory.

Uses : .NET Core -> Official .NET Core

Finite State Machine

A finite-state machine, or FSM for short, is a model of computation based on a hypothetical machine made of one or more states. Only a single state can be active at the same time, so the machine must transition from one state to another in order to perform different actions.

FSMs are commonly used to organize and represent an execution flow, which is useful to implement AI in games. The "brain" of an enemy, for instance, can be implemented using a FSM.

Preview Thumbnail

Preview Thumbnail

Preview Thumbnail

Preview Thumbnail

Reference: https://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867

Who is the target audience?

This course is meant for anyone who wants to learn Finite State Machine algorithm and theory in C#. The examples are made with C# using .NET Core.

  • Warning : This course assumes you have some C# knowledge, and does not teach C# itself.

  • Warning : These example tutorials are not a "How to make State Machine" or "How State Machines works" and will not teach "State Machine techniques"

How To Use

Example Usage

Preview Thumbnail

StateType -> Main states (like Booting, Launching, Initializing, etc.)

Classes

Class Access Modifier Explanation
StateBase Abstract Main state base. When creating a new state, you need to inheritance this
StateInfo Abstract Use this to move information between states. But not mandatory

StateBase

Function Access Modifier Explanation
Begin Public Override Trigger when a state is initializing
Load Public Override Need to call manually, after state was initialized
Update Public Override Need to call manually, if you want the update with deltaTime
End Public Override Trigger when a state is terminating

Events

Event Type Explanation
FiniteStateBeganEventArgs Action Trigger when a state is initialized with Begin()
FiniteStateChangeEventArgs Action Trigger when a state is changed to another
FiniteStateEndedEventArgs Action Trigger when a state is terminated with End()

State Machine

Function Args Explanation
Initialize - Initialize the FSM
AddState - Add a State to FSM
MoveTo StateType, OnEvent Change the current State to given State

Example State

public sealed class ExampleState : StateBase {

    public ExampleState(FiniteStateMachine fsm, StateType stateKey) : base(fsm, stateKey) {
        //Your Constructor() codes
    }

    public override void Begin(FiniteStateChangeEventArgs eventArgs, StateType previousStateKey) {
        //Your Begin() codes 
    }

    public override void End() {
        //Your End() codes
    }

    public override void Load() {
        //Your Load() codes
    }

    public override void Update(float deltaTime) {
        //Your Update() codes
    }
}

Example StateInfo

public sealed class ExampleStateInfo : StateInfo {

    //Your variables, events, functions, etc.

    public override string ToString() {
        //Example ToString()
    }
}

Example Usage

FiniteStateMachine.Instance.Initialize();
FiniteStateMachine.Instance.AddState(new Example1State(FiniteStateMachine.Instance, StateType.EXAMPLE1));
FiniteStateMachine.Instance.AddState(new Example2State(FiniteStateMachine.Instance, StateType.EXAMPLE2));
FiniteStateMachine.Instance.AddState(new Example3State(FiniteStateMachine.Instance, StateType.EXAMPLE3));


FiniteStateMachine.Instance.MoveTo(StateType.EXAMPLE1, new FiniteStateChangeEventArgs(StateType.EXAMPLE1, new ExampleStateInfo()));
FiniteStateMachine.Instance.MoveTo(StateType.EXAMPLE2, new FiniteStateChangeEventArgs(StateType.EXAMPLE2, new ExampleStateInfo()));
FiniteStateMachine.Instance.MoveTo(StateType.EXAMPLE3, new FiniteStateChangeEventArgs(StateType.EXAMPLE3, new ExampleStateInfo()));

Example Event Handling

FiniteStateMachine.Instance.OnStateBegan += new Action<FiniteStateBeganEventArgs>(this.OnFiniteStateBegan);
FiniteStateMachine.Instance.OnStateEnded += new Action<FiniteStateEndedEventArgs>(this.OnFiniteStateEnded);
FiniteStateMachine.Instance.OnStateChange += new Action<FiniteStateChangeEventArgs>(this.OnFiniteStateChange);

public void OnFiniteStateBegan(FiniteStateBeganEventArgs e) {
    //Trigger when OnFiniteStateBegan
    //Began: e.Type
}

public void OnFiniteStateChange(FiniteStateChangeEventArgs e) {
    //Trigger when OnFiniteStateChange
    //RequestedType: e.RequestedType
    //StateInfo: e.StateInfo (Returns null, if no parameters are given)
}

public void OnFiniteStateEnded(FiniteStateEndedEventArgs e) {
    //Trigger when OnFiniteStateEnded
    //End: e.Type
}

Requirements

  • You should be familiar with .NET Core family
  • You will need a text editor (like VSCode) or IDE (Visual Studio)
  • You will need a computer on which you have the rights to install .NET Core

About

Finite State Machine was created to serve three purposes:

Finite State Machine is a basically State Machine learning repository which base state-machine library coded in C# language

  1. To act as a guide to learn Finite State Machine with enhanced and rich content using .NET Core.

  2. To act as a guide to exemplary and educational purpose.

  3. To create an advanced State-Machine with few lines.

Collaborators

Project Manager - Furkan Türkal (GitHub: dentrax)

Branches

We publish source for the [Finite-State-Machine] in single rolling branch:

The master branch is extensively tested by our QA team and makes a great starting point for learning the algorithms. Also tracks live changes by our team.

Copyright & Licensing

The base project code is copyrighted by Furkan 'Dentrax' Türkal and is covered by single licence.

All program code (i.e. C#) is licensed under MIT License unless otherwise specified. Please see the LICENSE.md file for more information.

References

While this repository is being prepared, it may have been quoted from some sources. If there is an unspecified source, please contact me.

Contributing

Please check the CONTRIBUTING.md file for contribution instructions and naming guidelines.

Contact

Finite-State-Machine was created by Furkan 'Dentrax' Türkal

You can contact by URL: CONTACT

Best Regards