Function not being called

Godot Version

4.5.1

Question

https://shaggydev.com/2023/10/08/godot-4-state-machines/

I am following the turoial above on state machines

extends Node


@export
var startingstate: State



var current_state : State



var currentstate: State



func _init(parent : Player) -> void:
    for child in get_children():
        child.parent = parent


    change_state(startingstate)


func change_state(new_state : State) -> void:
    if currentstate:
        currentstate.exit()
    
    currentstate = new_state
    currentstate.enter(
    )


func process_input(event: InputEvent) -> void:
    var new_state = current_state.process_input(event)
    if new_state:
        change_state(new_state)


func process_frame(delta: float) -> void:
    var new_state = current_state.process_frame(delta)
    if new_state:
        change_state(new_state)

This is my state machine script

This is player script

extends CharacterBody2D
class_name Player


@export
 var animation : AnimatedSprite2D

@onready
 var statemachine = $StateMachine


func _ready() -> void:
    statemachine.init(self)


func _unhandled_input(event: InputEvent) -> void:
    statemachine.process_input(event)


func _process(delta: float) -> void:
    statemachine.process_frame(delta)

Node hierarchy

r/godot - Trying to make a state machine thats not working|750xauto

These are the errors displayed in the stack trace

TLDR : There is a function in my script which for some unknown reason is not being recognised or called.

Can you check if you have the state_machine.gd script attached to your StateMachine node?

here

Change it to

@onready var statemachine = $StateMachine

And try if it works.

sorry but I posted this to reddit and they deleted the @onready because they thought I was mentioning another user and I replaced it with @export here my apologies but It was originally meant as @onready

Try changing this to

func init(parent : Player) -> void:

_init() is a built-in callback, that may break the node when you change it like that.

Note: If _init() is defined with required parameters, the Object with script may only be created directly. If any other means (such as PackedScene.instantiate() or Node.duplicate()) are used, the script’s initialization will fail.

It was one problem that was not related to the others but it helped me realise the other problem

___

extends Node

@export
var startingstate: State

var current_state : State

var currentstate: State

func _init(parent : Player) → void:
for child in get_children():
child.parent = parent

change_state(startingstate)

func change_state(new_state : State) → void:
if currentstate:
currentstate.exit()

currentstate = new_state
currentstate.enter(
)

func process_input(event: InputEvent) → void:
var new_state = current_state.process_input(event)
if new_state:
change_state(new_state)

func process_frame(delta: float) → void:
var new_state = current_state.process_frame(delta)
if new_state:
change_state(new_state)

___

There was meant to be only one current_state this was a stupid mistake on my part thank you for your help stranger

1 Like