Git Product home page Git Product logo

statekit's Introduction

StateKit

Lightweight and simple to use state machine implementing the "states as objects" system. The idea is to create a state machine for some target object (could be a player, NPC, overall game, menu system, etc) that allows you to separate all the states into separate classes while retaining access to the original target object. Setup is made as simple as possible. Assuming the target object is of class SomeClass any states made should inherit from SKState<SomeClass>. Changing state is just a matter of calling the changeState method and providing the class name of the state to change to. All states must implement 3 methods: begin, update and end. That's all there is to it. The rest is left up to the implementor.

Simple usage example:

// create a state machine that will work with an object of type SomeClass as the focus with an initial state of PatrollingState
var machine = new SKStateMachine<SomeClass>( someClass, new PatrollingState() );

// we can now add any additional states
_machine.addState( new AttackState() );
_machine.addState( new ChaseState() );

// this method would typically be called in an Update/FixedUpdate of an object
machine.update( Time.deltaTime );

// change states. the state machine will automatically create and cache an instance of the class (in this case ChasingState)
machine.changeState<ChasingState>();

StateKit now has big brother: SKMecanimStateKit. This is a StateKit state machine that is tailored to work with Mecanim. See the demo scene and comments for more info.

StateKitLite

StateKitLite is an even simpler, single class FSM. To use StateKitLite, you just subclass the StateKitLite class and provide an enum to satifsy the generic constraint (for example class YourClass : StateKitLite<SomeEnum>). The enum is then used to control the state machine. The naming conventions for the methods are best shown with an example. See below:

enum SomeEnum
{
	Walking,
	Idle
}

public class YourClass : StateKitLite<SomeEnum>()
{
	void Start()
	{
		initialState = SomeEnum.Idle;
	}

	void Walking_Enter() {}
	void Walking_Tick() {}
	void Walking_Exit() {}

	void Idle_Enter() {}
	void Idle_Tick() {}
	void Idle_Exit() {}
}

All state methods are optional. StateKitLite will cache the methods that you implemented at startup. You can change states at any time by setting the currentState property (for example currentState = SomeEnum.Walking). There are a few simple rules that you must follow in your subclass to make sure StateKitLite can function:

  • if you implement Awake in your subclass you must call base.Awake()
  • in either Awake or Start the initialState must be set
  • if you implement Update in your subclass you must call base.Update()

License

Attribution-NonCommercial-ShareAlike 3.0 Unported with simple explanation with the attribution clause waived. You are free to use StateKit in any and all games that you make. You cannot sell StateKit directly or as part of a larger game asset.

statekit's People

Contributors

keyworq avatar prime31 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

statekit's Issues

Update readme: machine.reason() not reachable

First - thanks for an excellent and simple open source FSM!

machine.reason() does not seem to be a valid call anymore, it seems that reason() is called in update() in the current version. If this is intentionally the readme on github should be updated.

Is there any particular reason why reason() is now automatically called in the update() function ?
I have a case where I need to call reason() after update(), and I was just wondering why these are bundled together as it seems they were originally separated.

On unity 4.6.5, on import i face this error

Assets/StateKit/SKMecanimStateMachine.cs(60,85): error CS1061: Type UnityEngine.AnimatorStateInfo' does not contain a definition forfullPathHash' and no extension method fullPathHash' of typeUnityEngine.AnimatorStateInfo' could be found (are you missing a using directive or an assembly reference?)

Suggestion : Make SKState descend from ScriptableObject?

Just wondering is there a reason that the base SKState class does not descend from scriptable object? I tend to make this change anytime I implement this repo (so that I can serialize and drag and drop states inside unity). Has this ever been considered? I just wanted to bring it up somewhere before sending a pull request for it.

Coroutine

Hi prime31,

Is there any way to make the update function (and maybe the other) coroutines ?

I mostly see coroutines for state machines, and I can see the benefit for it.

Thanks a lot guys

Separate StateMachine Initialization and start point

Recently I had a small problem. Lets say, that one of my states is just initialization state that just do stuff in begin() than move SM to next state. If this kind of state is a initializationState, machine will fail because other states are added later.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.