Skip to content

Wokarol/StateMachine

Repository files navigation

StateMachine

It allows you to create generic State Machines using code

Warning

I no longer mantain this repo and no longer use this approach to state machines as I moved to switch..case based ones due to their simplicity

State Machine like this (Image taken from http://www.gameprogrammingpatterns.com/state.html, I recommend you to read it) State Machine from gameprogrammingpatterns.com can be converted to code like that

// States
State standing = new Standing(this.characterController);
State jumping = new Jumping(this.characterController);
State ducking = new Ducking(this.characterController);
State diving = new Diving(this.characterController);

// Transitions
standing.AddTransition(ducking, s => downPressed);
standing.AddTransition(jumping, s => bPressed);

jumping.AddTransition(diving, s => downPressed);

ducking.AddTransition(standing, s => !downPressed);

// Initialization
stateMachine = new StateMachine(standing);

you can also move transitions to states themselves, then you just need to pass states to other states like that for example

// States
State standing = new Standing(this.characterController);
State jumping = new Jumping(this.characterController);
State ducking = new Ducking(this.characterController);
State diving = new Diving(this.characterController);

// State Injections
standing.SetStates(ducking, jumping);
jumping.SetStates(diving);
ducking.SetStates(standing);

// Initialization
stateMachine = new StateMachine(standing);

Rememeber, you will have to call

stateMachine.Tick(Time.deltaTime);

each frame

About

State Machine package for Unity

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages