In Godot, a Finite State Machine (FSM) is a design pattern used to manage the behavior of characters or entities by dividing their logic into discrete states (such as "Idle", "Run", "Jump") and transitions between them. This avoids the use of large blocks of nested conditional code, making the code more modular, scalable, and easy to maintain. How to use:
- Create an empty node within your scene that you want your state machine
- Add an empty node as a child of the node you just created and give it a state name you like
- Search the following route: addons\state_machine_bot\StateMachine
- With left click drag state.gd to your state.
- With left click drag state_machine.gd to you state machine node
- Select the node that contains your state. For example: "Idle"
- With right click you click extend_node and create the code for your custom state. That's all
Switch between states, call the state_machine variable then its function change_state_to(new_state:String) Example: we are in Idle and in the enter function: state_machine.change_state_to("Walk")