How to get a variable from a parent node into a state machine

Godot Version 4.7.1

Question

I want an entities movement speed to be affected by their Speed stat.

I’ve been following a tutorial on using a state machine for character movement and I want each movement state to call the same speed stat since if I want to apply buffs and debuffs, different entities have different speed stats, etc. I will need them all to call the same thing

For example

extends CharacterBody3D

@export var speed_stat: float = 10

So the stat is stored in the character body for now might make a node with it in separate as I also want some other stats. This is the root of the tree.

extends State
class_name Motion

const BASE_SPEED: float = 5

Motion stores various constants and the programming for the velocity and other things. I’m not sure about having this call the speed stat as I would ideally keep this dedicated to just that.

State just has functions in

extends Motion

func _update(_delta: float) -> void:
	_set_direction()
	_calculate_velocity(BASE_SPEED*(1+((speed_stat-10)/100)),direction,_delta)

I just want to get that variable speed stat into each of the movement states so that when in game they’re adjusted (character creation, equipment, levels, buffs/debuffs) it can do the calculations appropriately.

I’ve looked at tutorials for stat systems but they have it so that stats are resources and most focus on health and for some reason when they use a different word I can’t wrap my head around it. I thought it would be as simple as just having the export variable in the character 3D body but when doing it as I’ve done above “Identifier “speed_stat” not declared in the current scope.”

I have asked a similar question before but I still can’t wrap my head around the specific thing I need to do to get this variable to be seen further down the tree

To get speed_stat owned by an entity from your State script, you need a reference to that entity in your state. Then you can do something like entity.speed_stat

I don’t know how to do that if you could break it down a little I would really appreciate it.

If you are not sure how to pass an argument from one node to another it will probably be better for you to either find a simpler setup or wait a bit with implementing this.

There are many different ways. If you don’t want to pass the stats directly or access them through the state nodes, you could make your stats a global and access them that way.

If your state nodes are children to the player: just use something like get_parent().speed_stat

If you instead want to set it from the parent, just use a reference to the state nodes and set the speed directly.

Global may be simplest depending on how your nodes are set up.

I would practice more basics if i were you, and if you really struggle with a node based state machine it makes no sense to use one. State machines are meant to make life simpler, not being incomprehensible or difficult to work with. Give it a go but don’t feel like its wrong to go for a simpler option.