Skip to content

State Machine

Jake Cattrall edited this page Sep 11, 2023 · 4 revisions

The Nodot framework introduces the StateMachine to manage and transition between different game states. Transitions are set up using specific functions. It's also possible to monitor state changes by connecting to a signal that notifies of transitions between old and new states.

Furthermore, the Nodot framework offers the StateHandler node, designed to control specific states stemming from a StateMachine.

Basic Setup

image

The StateHandler node provides several functions you can override to implement your logic. These will only be executed if the StateHandler is responsible for the current state:

  • ready(): Logic when the node becomes ready.
  • physics(delta: float): Logic for every physics frame.
  • process(delta: float): Logic for every process frame.
  • input(event: InputEvent): Logic for input events.
  • state_updated(old_state: int, new_state: int): Logic when the state is updated.

Override these methods in your script to add custom behavior. For example:

func state_updated(old_state: int, new_state: int) -> void:
    if new_state == "State1":
        # Do something for State1
  • For each statehandler, right click and Extend Script.
  • In the new script, create a func ready() function

Registering and Handling States:

Inside the new statehandler scripts ready function, use register_handled_states function to specify which states this StateHandler should manage:

register_handled_states(["walk", "jump"])

Allowing transitions

Inside the new statehandler scripts ready function, use the StateMachines add_valid_transition function to add allowed transition states.

sm.add_valid_transition("idle", ["walk", "jump"])
sm.add_valid_transition("jump", ["land"])
sm.add_valid_transition("land", ["idle"])

Additional Configurations:

  • ignore_handled_states: If set to true, the StateHandler will execute for all states. By default, this is false.

Use Case

Walking State:

StateMachine defines the walking state, typically triggered when arrow keys or gamepad direction controls are pressed.

StateHandler for Walking: The StateHandler node dedicated to the walking state would manage the character's walking animations, sounds, and speed.

  • If the walking state is entered, this handler would play the walking animation, loop footstep sounds, and modify the character's horizontal velocity.
  • If the walking state is exited, it would stop the walking animation and footstep sounds, and possibly blend into another animation, such as idle or jump.

Jumping State:

StateMachine triggers the jumping state when the jump button is pressed.

StateHandler for Jumping: This StateHandler would manage everything related to the character's jump.

  • On entering the jumping state, it would play the jump animation, play a jump sound, and add an upward force or velocity to the character for the jump.
  • On exiting the jump state, it would manage the transition, either blending back into a falling animation until the character lands or directly into the walking or idle states.

In conclusion, while the StateMachine efficiently defines the broad state changes based on user input and game environment, the StateHandler nodes for each state handle the granular details, ensuring that animations, sounds, physics interactions, and other gameplay mechanics are managed appropriately for the current state.

Clone this wiki locally