I am looking through a state machine project example so i have an idea on how to implement it on my own project, however i keep on seeing enter() and exit() everywhere, but i couldnt find anything about them in the documents.
Heres the project im looking ats github repository:
https://github.com/theshaggydev/the-shaggy-dev-projects/tree/main/projects/advanced-state-machine
if anyone knows, can someone explain to me what enter() and exit() to? Thanks in advance!

  • SnapCracklins replied to this.
  • SpicoTheMirrorSpirit it looks like, as kojack said, enter() and exit() are custom functions declared in the base class, here:

    class_name BaseState
    extends Node
    
    export (String) var animation_name
    
    # Pass in a reference to the player's kinematic body so that it can be used by the state
    var player: Player
    
    func enter() -> void:
    	player.animations.play(animation_name)
    
    func exit() -> void:
    	pass
    
    func input(event: InputEvent) -> BaseState:
    	return null
    
    func process(delta: float) -> BaseState:
    	return null
    
    func physics_process(delta: float) -> BaseState:
    	return null

    Then they load whatever is connected to those processes in each state's script. Pretty interesting setup.

    enter() and exit() are functions you'd add to a state yourself.
    enter would be called when you first transition into a state, exit would be called when transitioning out of a state.

    For example, in that project have a look at assets/player/states/idle.gd you'll find this:

    func enter() -> void:
    	.enter()
    	player.velocity.x = 0

    When you change to the idle state, it will call that function which will set the player's x velocity to 0.
    The .enter() part on the inside calls the enter function of the parent BaseState class.

      SpicoTheMirrorSpirit it looks like, as kojack said, enter() and exit() are custom functions declared in the base class, here:

      class_name BaseState
      extends Node
      
      export (String) var animation_name
      
      # Pass in a reference to the player's kinematic body so that it can be used by the state
      var player: Player
      
      func enter() -> void:
      	player.animations.play(animation_name)
      
      func exit() -> void:
      	pass
      
      func input(event: InputEvent) -> BaseState:
      	return null
      
      func process(delta: float) -> BaseState:
      	return null
      
      func physics_process(delta: float) -> BaseState:
      	return null

      Then they load whatever is connected to those processes in each state's script. Pretty interesting setup.

        SnapCracklins Kojack
        Im still sort of confused on how we only execute the given script in the state machine, i thought enter and exit handled that, but that does open alot of posibilities. Thank you!

        The state nodes don't actually have any functions called automatically by Godot (like _physics_process), so adding each state as a child of the player won't result in their scripts being run.
        Instead they have physics_process (note the missing underscore from the start) which only the state manager knows about.

        The state manager has a variable called current_state. Whichever state is in here is the only script that runs each update.

        The player object tells the state manager to do a physics_process. The state manager then calls the physics_process of only the state in current_state.

        If the state's physics_process can return a state, the state manager will make that returned state the new current_state (calling exit on the previous state and enter on the new state).
        For example the jump state returns the idle state if the player touches the floor without moving. That will in turn cause jump state exit and idle state enter to be called.

        4 days later

        So is enter() kind of like _ready() for each state?

        It's similar.
        _ready() is called once when a node is added to the scene tree (such as when a scene starts).
        enter() is called every time you change into a state.
        In both cases it does a set up needed for the thing to work.

        Let's say you have an idle state and a running state and you are currently in the idle state...

        • idle's process() is called every frame
        • the player pressed the right arrow
        • idle's exit() is called (because idle is ending)
        • current state is changed to running
        • running's enter() is called (because running has just started)
        • running's process() is now called every frame