• Godot HelpProgramming
  • A resource when preloaded into another script only produces null variables when calling them

I have this script:


extends Resource
class_name Monster

export(Texture) var front_sprite
export(Texture) var back_sprite
export(String) var name
export(int) var level
# Gets the types and gives the monster a type
export(TypeData.types) var type_1 = TypeData.types.none
export(TypeData.types) var type_2 = TypeData.types.none
# Base stats the limit the amount of points a monster can get
export(int) var Base_hp
export(int) var Base_attack
export(int) var Base_special_attack
export(int) var Base_defence
export(int) var Base_special_defence
export(int) var Base_speed
# The avaliable moves the monster can use to attack or other actions
export(Array,Resource) var move_slot = [null,null,null,null]
# The monsters stats based on the level
onready var hp  = ((float(Base_hp * level) / 100) + 5.0)
onready var attack = ((float(Base_attack * level) / 100.0) + 5.0)
onready var special_attack = ((float(Base_special_attack * level) / 100.0) + 5.0)
onready var defence = ((float(Base_defence * level) / 100.0) + 5.0)
onready var special_defence = ((float(Base_special_defence * level) / 100.0) + 5.0)
onready var speed = ((float(Base_speed * level) / 100.0) + 5.0)
# So when the monster takes damage we can takeaway the hp
onready var current_hp = hp

This script is what values I want to get, I have another script that preloads this resource into the other script but when I try to access its variables it produces nulls, I am not sure why it does this, the funny thing is If I remove the onready it produces a value but not a correct value, it seems to ignore the export when onready isnt available

onready var player = preload("res://Monsters/Gobulk.tres")

func _ready():
	print(player.hp)

This is the other script as you can see I preloaded the resource I want to access and in the ready function I am asking it to print the hp of the resource but instead of actually getting a value I get a null, any ideas on getting this to work, thanks

I'm not sure how your scene setup is. But the export variables only work when you set them in the inspector on an attached script. If you are loading from code, then I don't think the values set will care over. But maybe I am missing something, I haven't tried this before.

I actually figured out from someone on Reddit mentioned since it's a resource onready never gets called so the variable is null

2 years later