Godot Version
4.3
Question
Hello there!
I have a Finite State Machine and I’ve been trying to use a Resource to update the Player variables (for saving and loading the game) but I receive this error:
“Invalid access to property or key ‘speed’ on a base object of type ‘Nil’.”
The Player script holds all the variables and it gets referenced by each State to access them (I only copied one variable, speed, to test it):
class_name Player extends CharacterBody2D
@export var _stats: PlayerResource:
set(_new_stats):
_stats = _new_stats
get:
return _stats
var speed = _stats.speed # <--- this is where I receive the ERROR.
PlayerResource script:
class_name PlayerResource extends Resource
@export var speed := 160.0
Then, inside each State, for example RunningState, I access the Player variable inside the physics function:
var player: Player
func physics_update(delta: float) -> void:
var input_direction_x := Input.get_axis("move_left", "move_right")
player.velocity.x = player.speed * input_direction_x
I’ve already imported the PlayerResource script in the Player’s inspector var that I exported.
It seems like the engine accesses the speed too early before it gets assigned from _stats. Correct?
How do I solve by keeping this flow in the FSM?
I was checking those couple of videos but since they are not FSM there might be something I’m missing:
Thanks for any help!
Cheers