Crafting Bench
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 49 of 50.
In a survival game, a crafting bench turns what you gather into things you can use. Two buttons gather wood and stone, and three recipes turn them into more: two wood make a plank, a plank and two stone make an axe, four planks make a raft. A recipe's button can be pressed only while the inventory holds everything the recipe needs, and crafting uses those things up. The recipes are data filled in on the bench in the Inspector, so a new recipe needs no new code. The inventory announces every change to whoever is listening, and it is saved as JSON each time it changes, so quitting the game loses nothing.
Write two scripts: Inventory, on the Backpack, and CraftingBench, on the Bench.
Challenge
HardInventory:
- A
[System.Serializable]classItemStackwithpublic string itemandpublic int count, and a[System.Serializable]classInventoryDatawithpublic List<ItemStack> stacks. The inventory keeps oneInventoryData. public event Action Changed, raised after every change.public int Count(string item): how many of the item it holds, 0 when it has none.public void Add(string item, int amount)adds to the item's stack, or adds a new stack at the end of the list.public void Remove(string item, int amount)takes from it, and a stack that reaches 0 leaves the list.public string Describe()lists the stacks in order, asWood 3, Plank 1, or returnsempty.- Every change is saved at once to
inventory.jsoninApplication.persistentDataPath, asJsonUtility.ToJsonof theInventoryData.Awakeloads that file when it exists.
CraftingBench, which the Inspector gives inventory, recipes, recipeButtons (one per recipe, in the same order), woodButton, stoneButton and inventoryText:
- A
[System.Serializable]classIngredientwithpublic string itemandpublic int amount, and a[System.Serializable]classRecipewithpublic string resultandpublic List<Ingredient> needs. The bench'srecipesis aList<Recipe>, filled in the scene. - The gather buttons add one
Woodor oneStone. Each recipe button callspublic void Craft(int index)with its recipe's index. Craftdoes nothing when the inventory lacks anything the recipe needs. Otherwise it removes every ingredient, adds one of the result and logs it, asCrafted Axe.- At the start and whenever the inventory changes, each recipe button is clickable only while its recipe can be crafted, and
inventoryTextshows the description after the word Inventory, asInventory: Wood 3, Plank 1orInventory: empty.
Press Play, gather and craft a raft, press Stop and Play again, then Check.
Try it yourself
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class CraftingBench : MonoBehaviour
{
[SerializeField] private Inventory inventory;
[SerializeField] private Button[] recipeButtons;
[SerializeField] private Button woodButton;
[SerializeField] private Button stoneButton;
[SerializeField] private TMP_Text inventoryText;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular Turret10Project: Arena Survivor
The HeroEnemy Types