Accessing data in nested resources

Godot Version

Godot 4.2

Question

Hello again! I’m stuck once more in how to access and change data in resources.

The problem is that I’m trying to make a simple crafting system, where all it does it add and subtract an amount in the inventory list of items. So it’s just changing numbers of variables basically.

I have two resource classes that I’m working with. “Item” and “Recipe”. These I’ve tried manipulating by making two scripts: “get_item” and “craft_item”.

Get_item works well! It has an export variable for what Item I need to add, and fetches information fine from it, just changing the “amount” of the resource. No problems there:

Craft_item gives me trouble though, because a Recipe resource contains Item resources within one of its variables, for information on what items to subtract, and what will be the result:

billede

I’ve tried making this slapdash code to make it reference the data in the nested “result” resource, but am failing. Godot keeps giving me a ‘nil’ error for the contents of those variables.

How do access this information nested in the recipe resource? is there something I should be loading in, that I’m missing?

(Don’t mind the commented out line for now, I think I’ve turned that into nonsense)

there might be problems with when-what is defined. Try to print(recipe.result.name) in the _on_pressed()-method. If my suspicions are correct, the recipe is null when result,ingredient1 and ingredient2 are set. To avoid this you can use

@onready var result: Item = recipe.result

Another way to ensure the recipe is loaded correctly, is to use preload(“your_recipe_path”), but then you cant export it since it has to be constant

Gosh, that’s all it took! Thank you so much that’s perfect, it works now!

1 Like

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