State design pattern¶
Scripting a game can be difficult when there are many states that need to handled, but only one script can be attached to a node at a time. Instead of creating a state machine within the player's control script, it would make development simpler if the states were separated out into different classes.
There are many ways to implement a state machine with Godot, and some other methods are below:
- The player can have a child node for each state, which are called when utilized.
- Enums can be used in conjunction with a match statement.
- The state scripts themselves could be swapped out from a node dynamically at run-time.
This tutorial will focus only on adding and removing nodes which have a state script attached. Each state script will be an implementation of a different state.
There is a great resource explaining the concept of the state design pattern here: https://gameprogrammingpatterns.com/state.html
The feature of inheritance is useful for getting started with this design principle. A class should be created that describes the base features of the player. For now, a player will be limited to two actions: move left, move right. This means there will be two states: idle and run.
Below is the generic state, from which all other states will inherit.
A few notes on the above script. First, this implementation uses a
setup(change_state, animated_sprite, persistent_state) method to assign
references. These references will be instantiated in the parent of this state. This helps with something
in programming known as cohesion. The state of the player does not want the responsibility of creating
these variables, but does want to be able to use them. However, this does make the state coupled to the
state's parent. This means that the state is highly reliant on whether it has a parent which contains
these variables. So, remember that coupling and cohesion are important concepts when it comes to code management.
See the following page for more details on cohesion and coupling: https://courses.cs.washington.edu/courses/cse403/96sp/coupling-cohesion.html
Second, there are some methods in the script for moving, but no implementation. The state script
pass to show that it will not execute any instructions when the methods are called. This is important.
_physics_process(delta) method is actually implemented here. This allows the states to have a default
_physics_process(delta) implementation where
velocity is used to move the player. The way that the states can modify
the movement of the player is to use the
velocity variable defined in their base class.
Finally, this script is actually being designated as a class named
State. This makes refactoring the code
easier, since the file path from using the
preload() functions in godot will not be needed.
So, now that there is a base state, the two states discussed earlier can be implemented.
The since the
Idle states extend from
State which extends
Node2D, the function
_physics_process(delta) is called from the bottom-up meaning
Idle will call their
State will call its implementation, then
will call its own implementation and so on. This may seem strange, but it is only relevant for predefined functions
_process(delta), etc. Custom functions use the normal inheritance rules of overriding
the base implementation.
There is a round-about method for obtaining a state instance. A state factory can be used.
This will look for states in a dictionary and return the state if found.
Now that all the states are defined with their own scripts, it is time to figure out
how those references that passed to them will be instantiated. Since these references
will not change it makes sense to call this new script
persistent_state.gd script contains code for detecting input. This was to make the tutorial simple, but it is not usually
best practice to do this.
This tutorial made an assumption that the node it would be attached to contained a child node which is an AnimatedSprite. There is also the assumption that this AnimatedSprite has at least two animations, the idle and run animations. Also, the top-level node is assumed to be a KinematicBody2D.
The zip file of the llama used in this tutorial is
The source was from piskel_llama, but
I couldn't find the original creator information on that page though...
There is also a good tutorial for sprite animation already. See 2D Sprite Animation.
So, the only script that must be attached is
persistent_state.gd, which should be attached to the top node of the
player, which is a KinematicBody2D.
Now the player has utilized the state design pattern to implement its two different states. The nice part of this pattern is that if one wanted to add another state, then it would involve creating another class that need only focus on itself and how it changes to another state. Each state is functionally separated and instantiated dynamically.