problem with resource in my game I can’t assigne value to var from resource in class body

Godot Version

4.6.stable.official [89cea1439]

Question

Hi I have this problem with resource in my game I can’t assigne value to var from resource in class body

this code works and normaly, project plays and var attack_speed prints correct value

extends Node2D
@export var Player_stats_local: PlayerStats 
var atack_speed 

func _ready():
atack_speed = Player_stats_local.Player_Base_Attack_Speed
print(atack_speed)


this also work fine

extends Node2D
@export var Player_stats_local: PlayerStats 
var atack_speed 

func _stats_recalculate():
	atack_speed = Player_stats_local.Player_Base_Attack_Speed
	print(atack_speed)
func _ready():
	_stats_recalculate()

but this code gives me errors:

1Resource file not found: res://SCENES/main.tscn::jsldp (expected type: unknown)
2Can’t use get_node() with absolute paths from outside the active scene tree.

extends Node2D
@export var Player_stats_local: PlayerStats 
var atack_speed = Player_stats_local.Player_Base_Attack_Speed

func _ready():
print(atack_speed)

my resource has name player_stats.gd and has this code

class_name PlayerStats
extends Resource

@export var Player_HP: int = 1
@export var Player_Base_Dmg: int = 20
@export var Player_Base_Attack_Speed: float = 1
@export var Player_Base_Attack_Range: float = 400
@export var Player_Base_Maximum_Targets: int =1
@export var Player_Base_Speed: float = 100.00 
@export var Player_Base_Time: float = 35

I alerady tried:

delete .godot folder

change from forward+ to compatability

opening new project then open this project with problem

making new resource

I have not tested this, but my guess would be you can’t assign from the Player_stats_local as it is not ready yet. I guess this only because you said it works if you assign attack_speed in the _ready() function. So just do that instead.

1 Like

Assignment to variables declared only as var happens when the constructor _init() is called. Assignment to @export variables happens after that. So if you try to initialized a plain var with exported an exported var you’ll only get the default value, which in this case is null.

The only strange thing are your error messages. It normally should be a null reference error.

1 Like

Thank you for expling why this problem occour.

I checked and this errors pop up when i try to assagin to node resource when i have this code in script
var atack_speed = Player_stats_local.Player_Base_Attack_Speed
but when i already have resoursce assigned a type this code a run program i get normall “Null” error.

1 Like

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