Code Monkey home page Code Monkey logo

state-machine's Introduction

Finite State Machine

Creating State Machine

To create the State Machine, simply create a new State Machine Node.

Note

To make a starting State for the State Machine, select the State Machine and change the Initial State property.

Creating a State

To create a new State, create a script similiar to this...

extends State
class_name STATE_NAME # Your state's name

# Runs when the state is entered
func enter():
    pass

# Runs when the state is exited
func exit():
    pass

# Updates every _process() update (When state is active)
func update():
    pass

# Updates every _physics_process() update (When state is active)
func physics_update():
    pass

Warning

If you do not specify a class name for your state, you will be unable to create a node of the State.

To add the State to the State Machine, create a new Node of the State you just made, and add it as a child of the State Machine.

Transitioning to other States

To transition to other States on the State Machine, make sure there are more than one State as a child of the State Machine.

To transition to another State, you can use the Transitioned signal (signal inside State script)...

# (CURRENT_STATE, NEXT_STATE_NAME)
Transitioned.emit(self, "EnemyIdle")

Checking if State is Active

If you need to check if a state is active while inside of the state, for example, inside the EnemyIdle state you are checking the _on_area_entered(): (you might already be getting an 'Cannot change state from a non-active state') you can use the active boolean included inside the State class to check if the state is currently active.

# Instead of doing this...
func area_entered(area):
    # Code here

# Do this...
func area_entered(area):
    if active:
        # Code here

Note

This applies to any functions that could run at anytime (such as area_entered, area_exited, etc).

Support

If you have any issues, create an Issue or message me on Discord at itsmealec.

state-machine's People

Contributors

alecsouthward avatar

Stargazers

Luiz Henrique Harsche avatar  avatar Albert Nasretdinov avatar mammon avatar Stefano Azzolini avatar  avatar chZzZzZz avatar Vand Sales avatar Cat avatar  avatar Otávio Burato avatar  avatar

Watchers

 avatar

state-machine's Issues

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.