Coder Social home page Coder Social logo

tavurth / godot-simple-state Goto Github PK

View Code? Open in Web Editor NEW
62.0 7.0 3.0 49 KB

A simple Finite State Machine for Godot

Home Page: https://godotengine.org/asset-library/asset/1242

License: MIT License

GDScript 100.00%
godot godot-engine finite-state-machine fsm-library

godot-simple-state's Introduction

Godot Simple State

A clean and easy to use Finite State Machine (FSM) for Godot 3.x

img img img img

Usage

  1. Install the plugin

  2. Enable the plugin

  3. Add a SateMachine node to your character

    Screenshot 2022-02-19 at 12 36 45
  4. Add any type of Node to the StateMachine as a child to create a new script

    Screenshot 2022-02-19 at 12 36 30
  5. Attach a script to the node

  6. Now at runtime you can change to a different state using $StateMachine.goto("state")

Example

The example project contains two states, idle and attack. The project will switch between each state automatically every 3 seconds.

State functionality

The state has a few functionalities, here is an example state:

extends Node2D

var States
var Host

func _state_enter(arg or not):
    pass

func _state_exit():
    pass

If you call await in _state_exit the StateMachine will wait for your await to finish before entering the new state. This is also true for _state_enter or other state functions.

func _state_exit():
    await get_tree().create_timer(1).timeout

Note: Exit state

state will change to _exit when the state machine is exiting. Your state will also be queue-freed.

If running a long while loop in your state logic, be sure to check for States.is_current()

Reference

StateMachine

signal state_changed(new_state)

Emitted whenever the StateMachine changes state (but before _state_enter is called)

goto(state_name: String, args = null) change the state

args can be any or undefined

When an arg is passed, the argument will be pushed to the _state_enter function.

StateMachine.goto("attack")
StateMachine.goto("attack", some_character)
StateMachine.goto("attack", [some_character_a, some_character_b])

The last example would call this function in the attack state:

func _state_enter(some_characters: Array):
    ...

call(method: String, args = null) call a function on the current state (if exists)

StateMachine.call("some_method")
StateMachine.call("some_method", my_argument)
StateMachine.call("some_method", [my_arguments])

now(state: String)

Returns true if the current state matches state

if States.now("afraid"):
    # keep running away instead of stopping to look at something

has(state: String)

Returns true if exists in our state tree

if States.has("some-other-state"):
    # Do something

is_current()

Returns true only when called from a function inside the current state

if States.is_current():
    This can only be printed in the current state
    In any other state this will never be printed

restart(arg: any or none)

Restarts the current state This only calls "_state_enter" again it does not reset any variables

State

_state_enter(args or not) will be called when the state is entered (each time) An argument is only passed if one was passed. (StateMachine.goto("state", arg))

_state_exit() will be called when the state is left (each time)

If the following variables exist on your state, they will be injected with dependencies as follows:

Host is the NodePath input into StateMachine i.e. your character controller

States is the StateMachine

If they do not exist on your state, nothing will be injected.

Signals

You can connect signals directly to the StateMachine node using the following style:

Screenshot 2022-02-19 at 13 00 38

They will be then automatically sent to the current active state if that state has the handler function defined.

Buy Me A Coffee

godot-simple-state's People

Contributors

tavurth 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

godot-simple-state's Issues

Migrate to support Godot 4

Godot 4 is fast approaching a stable version and I would love to see this plugin support the new code conventions and features of the lastest Godot version.

Much appreciated.

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.