Custom Resource Enum value pair

Godot Version
4.3

Question

I am working on a project with a custom resource made to handle all building information. This can include things like the spite to be used. The production time of the building, amount produced, etc.

I have a global variable that is being used to have one place that keeps a list of all building materials as an enum.

enum BuildingResourceType {
GOLD,
IRON,
WOOD,
STONE
}

I want to be able to use this as a drop down list to select my building resource type. Then assign an integer with it. For example. I would like to select gold make it cost 500 gold to build a building. Then the next building might cost 500 gold and 200 wood etc. I want to be able to assign this in the custom resource for my buildings. I am surprised how difficult this seems to be in this engine.

I can access the global variable and get a drop down but I cannot create a value with it.

The easiest solution is to specify it as a typed dictionary with the enum as key and the cost as value:

@export var cost: Dictionary[BuildingResourceType, int]

Type dictionaries were only added in 4.4 which does not have a stable release yet. But you can switch now if you want to use it, the latest release is rc-2 which is quite stable and shortly before the main release.

If you don’t want to do that or you need more data, you can create a custom resource that contains all the information you need (ResourceType, cost, …) and then set an array of this resource-type as an exported var.

Edit: Added that typed dictionaries are only available in 4.4

2 Likes

Okay, thank you that is what I was tying to do and just couldn’t figure it out for the life of me. Updating made it work as expected. Thank you.