Godot Version
4.3
Question
Hey everyone, I am new to using godot and I am trying to build good habits by using resources for a hunting game. I seem to be having an issue where my movement script cannot pull the var speed from its referenced resource.
Here is the custom resource:
extends Resource
class_name AnimalStats
@export var health : int = 100
@export var speed : int = 100
@export var fall_acceleration: int = 75
And then the code on the entity I want to control is:
extends CharacterBody3D
@export var stats: AnimalStats
var target_velocity = Vector3.ZERO
func _physics_process(delta: float) -> void:
var direction = Vector3.ZERO
if Input.is_action_pressed("move_left"):
direction.x += 1
if Input.is_action_pressed("move_right"):
direction.x -= 1
if Input.is_action_pressed("move_forward"):
direction.z += 1
if Input.is_action_pressed("move_back"):
direction.z -= 1
if direction != Vector3.ZERO:
direction = direction.normalized()
$Pivot.basis = Basis.looking_at(direction)
#ground velocity
target_velocity.x = direction.x * stats.speed
target_velocity.z = direction.z * stats.speed
#vertical velocity aka Gravity
if not is_on_floor():
target_velocity.y = target_velocity.y - (stats.fall_acceleration * delta)
#moving character
velocity = target_velocity
move_and_slide()
The error I get is Invalid access to property or key āspeedā on a base object of type āNilā and it appears on the line that says:
target_velocity.x = direction.x * stats.speed
The custom resource is being refernced in the inspector, but it does nothing when I try to play, just a grey screen. Anything Iām missing? Not sure if it is needed but on the Main for the scene I load the resource too:
extends Node3D
func _ready() -> void:
var loaded_resource = load("res://Animals/Deer/deer_stats.tres")