State Machine Plugin for 2d games in godot. It comes with the implementation of 4-way movement.
To use this plugin correctly, the following steps must be followed:
1.- You must create the script of the character node to which you want to implement the state machine, like the next example.
2.- Verify that you have the plugin enabled. (Project/Projects Settings/Plugins)
3.- Add StateMachinePlayer
node to your character.
Note: If you want to create a second state machine different from the first one, you must create a different node with its respective script where to define the new states.
4.- Add the states as child nodes to StateMachinePlayer node and for each one there must be a script created. Each of the state node scripts must be extended by state
5.- In the script that is included with the StateMachinePlayer node, you must define the states that your character will have and what the initial state will be.
6.- You only have to edit the scripts of the child nodes with the "enter" and "physics" functions.
extends state
var name_state
func enter(): ----> THIS FUNCTION IS EXECUTED WHEN YOU ENTER THE STATE
name_state = self.get_name()
func physics(delta): ----> THIS FUNCTION IS IN CHARGE OF MAKING THE CORRESPONDING VALIDATIONS TO CHANGE FROM ONE STATE TO ANOTHER
get_direction()
move()
direction_validation(name_state)
Below an example of the state called "Right"
The following functions can be used to construct the states.:
"get_direction()"
: This function gives you the orientation in which the character will move, depending on the key that is pressed."move()"
: This function gives the speed defined in the player script to the character."walk_animation(texture, is_flip_h)"
: This function is used to run an animation, the first argument it receives is the character texture for that state. And the second argument is optional, which is a boolean to flip the sprite, which has a false value by default."stop_walk()"
: It's used to stop the sprite and leave it in its first frame."direction_validation(state_name)"
: This function validates changes in the direction of the character while it is in a state, in this way the changes of state are made. The only parameter that this function receives is the name of the node of the state that needs to be validated.