Can't change children properties from parent script/node

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.

This button </> on the menu over the textbox adds code tags to the selected text.
And the pencil icon at the bottom of your post lets you edit your post.

1 Like

Thanks, the post looks a lot more readable now

This tells you that you’re trying to set a variable called parent on an object (a Node) that doesn’t exist on said object, thus is considered invalid.

My best guess would be, that your state machine has at least one child node which isn’t an actual state (i.e. doesn’t have your State script attached).

Yeah, this just occurred to me a few hours after I implemented my workaround. I have no idea why I didn’t think of it earlier, but at least I know to double check now.

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