I'm running into some hurdles with my crafting system that I thought I would have to deal with later on, but the problems are cropping up now. I have types of recipes, ingredient, simple, complex, and custom.
Ingredient recipes are almost like substitution for real life recipes, sometimes an ingredient can change its state and act as a substitution for a simple, complex, or custom recipe. Here is a rudimentary implementation for how this works:
func _on_ChangeIngredient_pressed() -> void:
var old_stack: ItemStack = n_hotbar_inventory.get_item_stack(0)
var item: Item = old_stack.item
if not item:
return
if not item.type == Recipe.Type.INGREDIENT:
return
if not recipe_data.has(item.name):
return
var recipe = recipe_data[item.name]
# This is a temporary way to circumvent making a menu so that players can
# decide how many stacks of this item they want to make.
# We likely need to keep the math around.
var req_quantity = recipe.requirements[item.name]
var new_quantity: int = 1
var remainder = old_stack.quantity % req_quantity
if req_quantity != old_stack.quantity:
new_quantity = (old_stack.quantity - remainder) / req_quantity
if new_quantity == 0:
return
var dupe_stack = old_stack._duplicate(true)
dupe_stack.quantity = new_quantity
dupe_stack.item = recipe.product
old_stack.quantity = remainder
if remainder == 0:
old_stack.item = null
emit_signal("craft", old_stack, dupe_stack, Recipe.Type.INGREDIENT)```
This system works exactly as intended. There is a recipe dictionary, this one represents the "ingredient" type recipes. The name of the ingredient is the key, the pair/value is the recipe itself. Etc etc, the rest is pretty boilerplate edge case detection and data management.
