Godot Version
Godot 4.2
Question
I am trying to implement a state machine in Godot. I’ve been following this tutorial: https://www.youtube.com/watch?v=oqFbZoA2lnU&list=PLaiU9HSaKMWtmAIR345HGIz_ijQiyr3kH&index=4
However at around 4:42 if I try and implement that bit of code into my state machine, which I’ve pretty much copy pasted at this point, I get this error: Invalid set index ‘parent’ (on base: ‘Node’) with value of type ‘CharacterBody3D (Player)’.
I have no clue why, and I even created a test variable that only holds an interger and tried to update that to some random value and I got a similar error. I’m not sure what code exactly would be helpful, but here’s the most relevant parts.
From the state machine:
func initialize(parent: Player) -> void:
for child in get_children():
child.parent = parent
change_state(current_state)
func change_state(new_state: State):
if current_state: current_state.exit_state()
current_state = new_state
current_state.enter_state()
And from the actual state itself:
extends Node
class_name State
var parent: Player
func enter_state() -> void:
print(parent)
#parent.current_speed = 0
func exit_state() -> void:
pass
func process_inputs(event) -> State:
if event.is_action_pressed("WalkForward"): return $"../WalkState"
else: return null
func process_frame(delta) -> State:
return null
func process_physics(delta) -> State:
return null
Please help, I am honestly about to tear my hair out because I cannot for the life of me figure out what could be causing this issue and I already know it’s gonna be something really stupid.
Edit: Also apologies for the wall of code without any indents, I have no idea how to make it look nice since I’ve never used this site before.
Edit 2: Managed to find a workaround by just passing in the player reference through the states enter function. Don’t know why the tutorial method didn’t work so if anyone has any guesses as to why, I’d love to hear them.