Define amount in Array of resources for different resources

Godot Version

v4.5.1.stable.official [f62fdbde1]

Question

Hi all,

I have a question on how to best handle the following issue:

I have custom resources, like wood and stone. I set up the necessary info in the editor. Example:

class_name Materials extends Resource

@export var material_type: String
@export var material_img: Texture2D

I also have different kinds of housing. The base resource looks like this:

class_name House extends Resource

@export var build_cost: Array[Materials]
@export var build_material_amount: int

Now, I want to add different building costs to place the housing. For example, house1 uses 5 wood and 5 stone to build, house2 uses 10 wood and 10 stone.
With the setup above, I’m not able to define the specific amount for each material resource. If I add some variable to the material resource about the cost, it’ll be the same amount for all houses. I also couldn’t get some exported Dictionary with the Array to work.
Do you have any suggestions?

Thank you very much in advance!

1 Like

this is perfect for dictionaries:

@export build_cost: Dictionary[Materials, int]

the key is the material and the value is the amount

3 Likes

Thanks for the reply! In theory, this should work perfectly.
But for some reason, when I set up two different example house resources, it does not save what I’ve set up. Either e.g. the stone completely deletes from the house resource, or the amount vanishes etc. Even though I saved the resource.

Maybe I’m doing something wrong?
I’ve added this to the House Resource. It shows up in the editor, but does not save any changes

@export var bcost2: Dictionary [Materials, int]

Introduce another resource class:

class_name CostChunk extends Resource

@export var material: Materials
@export var amount: int

And then:

@export var build_cost: Array[CostChunk]
2 Likes

Thank you so much, this worked! :slight_smile: