Godot Version
4.2.2 stable
My setup
I have a script “plant.gd” where I can set the plant_resource as exported property:
class_name Plant
extends Node2D
@export var plant_resource: DamagingPlantResource
In one of my plants called bamboo, I set this plant_resource to bamboo.tres:
This bamboo is a DamagingPlant, that sets the current stats when instantiated, by calculating it for a dictionary:
class_name DamagingPlant
extends Plant
var current_stats: Dictionary = {
"range": plant_resource["plant_range"] + fusion_stats["range"] + levelup_stats["range"] + modified_stats["range"],
"damage": plant_resource["damage"] + fusion_stats["damage"] + levelup_stats["damage"] + modified_stats["damage"],
"reload_time": plant_resource["reload_time"] * fusion_stats["reload_time_mult"] * levelup_stats["reload_time_mult"] * modified_stats["reload_time_mult"],
"proj_pen": plant_resource["projectile_penetration"] + fusion_stats["proj_pen"] + levelup_stats["proj_pen"] + modified_stats["proj_pen"],
"proj_speed": plant_resource["projectile_speed"] + fusion_stats["proj_speed"] + levelup_stats["proj_speed"] + modified_stats["proj_speed"]
}
The problem
When I then try to instantiate a bamboo using
var drag_plant = load("res://Scenes/Plants/Bamboo/bamboo.tscn").instantiate()
It fails creating the dictionary because the plant_resource has not loaded:
Invalid get index 'plant_range' (on base: 'Nil').
What I tried so far
- Setting the plant_resource in func _init() instead of an @export variable: Doesnt work
- Setting the plant_resource to a static value in the class Plant: Works (but defies the whole point of the resource)