[Solved] Invalid get index on base: 'Nil' on exported resource

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:
grafik

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

  1. Setting the plant_resource in func _init() instead of an @export variable: Doesnt work
  2. Setting the plant_resource to a static value in the class Plant: Works (but defies the whole point of the resource)

try to do this:

@onready var current_stats: Dictionary = {
    ...
}

I’m feeling a bit dumb right now:

I tested it previously, exactly like you proposed, but then another function wouldnt find the required values either.

Now that I tested it after you proposed it, it worked?!?
Maybe restarting godot AND setting it to @onready after a restart did the trick…

1 Like

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