How to write a global state machine in godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hsjaaa

I want to write a global state machine to change the state of the whole game. For example, start state for start menu and when player hit new game button then change to gameplay state and generate a new game.
Any idea or good practice how can I implement such a state machine in godot?
Thanks!

:bust_in_silhouette: Reply From: nayala

Hi !

I’m really new to the godot engine but I think the scenes are already a FSM.
Each scene represents a state (scene menu, scene game) and you can go from one scene to an other (one state to an other) with get_tree().change_scene()

In your case in the menu screen just connect the new game button signal pressed to a method that will load the game scene.

If you really want a FSM just go with an autoload script (Singleton) keeping the current state of your game with a basic state pattern.

Thanks, it’s really helpful info!

hsjaaa | 2019-01-29 08:31