Godot Version
v4.6.2.stable.mono.official [71f334935]
Question
For an automation game, I need a Resource (Scriptable Object) to create recipes. Amongst other things, it needs to associate a list of input items and their count to a list of output items and their count (i.e. 1 coal + 4 iron ore makes 2 iron bars), however I cannot find a clean way of doing this. I have found a couple solutions, but none of them seem the best. Please help, thanks.
Solution 1) Dictionary (or array[ItemData, int])
This seems to be the most ideal, but the inspector does not like these more complex data types. An actual solution would ideally be as close to this as possible
public partial class RecipeData : Resource
{
[Export] public Godot.Collections.Dictionary<ItemData, int> ingredients;
[Export] public Godot.Collections.Dictionary<ItemData, int> results;
public RecipeData() { }
}
Solution 2) Separate Arrays
This solutions works the best, but feels like bad design, having the items and quantities only implicitly linked.
public partial class RecipeData : Resource
{
[Export] public ItemData[] ingredients;
[Export] public int[] ingredientQuantities;
[Export] public ItemData[] results;
[Export] public int[] resultQuantities;
public RecipeData() { }
}
Solution 3) Custom Struct
This solutions feels the most intended, but it is bad to work with. To create a new recipe, I need to create a new itemstack resource for each ingredient and result, so I will end up with just a bunch of files that have {iron ore, 2} {iron ore, 4} etc. which just seems silly and annoying and a lot of extra stuff for something so trivial.
[GlobalClass]
[Tool]
public partial class ItemStack : Resource
{
public ItemData Item;
public int Amount = 1;
public ItemStack() { }
}
[GlobalClass]
[Tool]
public partial class RecipeData : Resource
{
[Export] public ItemStack[] ingredients;
[Export] public ItemStack[] results;
public RecipeData() { }
}
