Stack Frame errors and I don't know how to fix theme

Godot Version

4.3

Question

Hello

Recently I’ve been trying to make a fighting game on godot, and have been following https://www.youtube.com/watch?v=gtrOIQtnJmI&t=493s to make the game.

Unfortunately, I have encountered many issues mainly in the stack frames.
I am very new to Godot and I don’t know how to fix these issues.

Player Code:

extends CharacterBody2D
class_name LachiePlayer

@onready var state_machine: StateMachine = $FSM
@onready var anim: AnimatedSprite2D = $Animation

func _ready(): state_machine.init() #<— theres an issue here

func _process(delta): state_machine.process_frame(delta)

func _physics_process(delta): state_machine.process_physics(delta)

func _input(event): state_machine.process_input(event)

Player Idle Code:

extends PlayerState
class_name LachieIdle

func enter() → void:
Lachie.anim.play(idle_anim) #<-- there is an issue here

State Machine Code:

extends Node
class_name StateMachine

var current_state: State
@export var initial_state : State

func init() → void: change_state(initial_state) #<---- theres an issue here

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

func process_input(event: InputEvent) → void:
var _new_state: State = current_state.process_input(event)

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

func change_state (new_state: State) → void:
if current_state: current_state.exit()
current_state = new_state
current_state.enter() #<-- there is an issue here aswell

States:

extends Node
class_name State

func enter() → void:
pass

func exit() → void:
pass

func proccess_frame(_delta) → State:
return null

func process_input(_event) → State:
return null

func process_physics(_delta) → State:
return null

Player State Class:

class_name PlayerState
extends State

@onready var Lachie: LachiePlayer = get_tree().get_first_node_in_group(“LACHIE”)

var idle_anim: String = “Idle”

What is the error? Make sure to format your pasted code so we may read it easier.

I will re-paste the code in the format

Player Code:

extends CharacterBody2D
class_name LachiePlayer

@onready var state_machine: StateMachine = $FSM
@onready var anim: AnimatedSprite2D = $Animation

func _ready(): state_machine.init() #<--- theres an issue here too

func _process(delta): state_machine.process_frame(delta)

func _physics_process(delta): state_machine.process_physics(delta)

func _input(event): state_machine.process_input(event)

Player Idle:

extends PlayerState
class_name LachieIdle

func enter() -> void:
	Lachie.anim.play(idle_anim) #<-- there is an issue here 

State Machine:

extends Node
class_name StateMachine

var current_state: State
@export var initial_state : State

func init() -> void: change_state(initial_state) #<---- theres an issue here

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

func process_input(event: InputEvent) -> void:
	var _new_state: State = current_state.process_input(event)

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

func change_state (new_state: State) -> void:
	if current_state: current_state.exit()
	current_state = new_state
	current_state.enter() #<-- there is an issue here aswell


States:

extends Node
class_name State

func enter() -> void:
	pass

func exit() -> void:
	pass

func proccess_frame(_delta) -> State:
	return null

func process_input(_event) -> State:
	return null

func process_physics(_delta) -> State:
	return null 


Player State Class

class_name PlayerState
extends State

@onready var Lachie: LachiePlayer = get_tree().get_first_node_in_group("LACHIE")

var idle_anim: String = "Idle"

The errors:

Idle:
invalid access to property or key ‘anim’ on a base object of type “null instance”.

Since this state is a child of the player the player may not be in the group yet. Keep in mind that for a node to be “ready” all of it’s children must be ready first. Try using @export to get the player reference instead.

Now I´m getting an error saying "invalid call. nonexistent function ‘enter’ in base ‘Nil’.

It is under the “state machine” script in the final line.

Sounds like this export variable wasn’t set, did you assign it in the inspector?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.