Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alecsouthward/state-machine
State Machine for managing different States that an entity can be in.
https://github.com/alecsouthward/state-machine
gdsc gdscript godot godot-4 godot-addon godot-engine godot-module godot-plugin godot-tool godot4 godot4-2 godotengine
Last synced: 4 months ago
JSON representation
State Machine for managing different States that an entity can be in.
- Host: GitHub
- URL: https://github.com/alecsouthward/state-machine
- Owner: AlecSouthward
- License: mit
- Created: 2024-01-12T19:18:14.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-11T10:50:35.000Z (8 months ago)
- Last Synced: 2024-09-27T00:03:33.379Z (4 months ago)
- Topics: gdsc, gdscript, godot, godot-4, godot-addon, godot-engine, godot-module, godot-plugin, godot-tool, godot4, godot4-2, godotengine
- Language: GDScript
- Homepage: https://godotengine.org/asset-library/asset/2505
- Size: 20.5 KB
- Stars: 26
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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...
```gdscript
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)...
```gdscript
# (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.
```gdscript
# 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](https://github.com/AlecSouthward/State-Machine/issues/new) or message me on Discord at `itsmealec`.