# 7. Crafting Pitfalls
Part of [[Crafting System]]. Prev: [[6. Worked Example Campfire and Workbench]].
## Logic and data
| Symptom | Cause | Fix |
|---|---|---|
| Items vanish when the bag is full | inputs removed, then `Inventory.Add` returned leftover | simulate on a clone first (`CraftingService`), commit only if the simulation succeeds |
| "Not enough room" although crafting would free a slot | checked `RoomFor(output)` before removing inputs | simulation removes inputs first, as the real craft does |
| "Max" button picks too few | binary search assumed room is monotonic in batch size | scan down from the ingredient limit (part 3 test `MaxCraftable_BiggerBatchCanFitWhenSmallerDoesNot`) |
| Hammer disappears | ingredient left at `consumed = true` | mark tools `consumed = false`; `Needed()` counts tools once per batch |
| Have/need numbers wrong | the same item in two input rows | `RecipeDefinition.Validate` flags duplicates; merge rows |
| Infinite items | a loop that gains items (A -> B -> A) | Recipe Audit cycle check; balance or remove the loop |
| Timed recipe crafts instantly | `craftSeconds > 0` with no station | `Validate`: timed recipes need a station |
| Station queue keeps running with the fire out | requirement not on the station's GameObject | `GetComponents<IStationRequirement>()` only finds components on the same object |
| Finished items lost | outputs added to a full backpack | outputs go to the station's `Output`; a full output stalls the job ("Output full") |
| Cancel creates items | refund for units already delivered | `unitsLeft` only counts undelivered units; refund those |
| Real craft differs from simulation (error log) | `Inventory.Add`/`Remove` changed to be non-deterministic (random slot choice, events that mutate) | keep inventory operations deterministic; never mutate the inventory inside `Changed` handlers |
## UI
| Symptom | Cause | Fix |
|---|---|---|
| UI shows old counts | detail not refreshed on `Inventory.Changed` | `OnInventoryChanged` refreshes detail and `RefreshItems()` |
| Frame hitch when the bag changes | `Rebuild()` and `MaxCraftable` for every recipe on every change | `RefreshItems()` for appearance; `MaxCraftable` only for the selected recipe, only on click |
| Wrong row shows wrong recipe after scrolling | state stored in row elements instead of re-bound in `bindItem` | `bindItem` sets *every* visual property each time (ListView recycles rows) |
| Nothing happens on the gamepad | no focus on open, or the player map still eats input | `_list.Focus()` on open; switch to the UI action map |
| NullReference on open | `CraftingView` subscribed in `OnEnable` before `InventoryHolder.Awake` created the inventory | subscribe in `Start` (as written) |
| Colour-blind players cannot see missing items | red/green only | numbers (`have / need`), a reason line, icons ([[Accessibility]]) |
| Craft button disabled with no explanation | UI checks `Ok` but ignores `Reason` | always show `CraftCheck.Reason` |
UI Toolkit performance in general: [[7.UI Toolkit Pitfalls and Performance]].
## Discovery and saves
| Symptom | Cause | Fix |
|---|---|---|
| Toast spam on load | `Learn` called during load | `RecipeKnowledge.Restore` adds silently; ingredient re-scan is idempotent |
| Recipe learned from starting items, spoiling the surprise | ingredient discovery runs on the starting inventory | set `discoverOnIngredientPickup = false` for that recipe or do not start with the item |
| Blueprint consumed although already known | effect returned true | `LearnRecipeEffect` returns `Learn(...)`, false when already known |
| Old saves break after renaming assets | saved names instead of ids | ids for recipes, items and stations; never change an id after release |
| Placed campfire gone after load | runtime-spawned stations not in the save | save placed objects (prefab id, pose, station data) and respawn them |
| Save file half-written after a crash | direct `File.WriteAllText` to the real path | write `.tmp`, then `File.Replace` / `File.Move` (as `InventorySave` does) |
| Statics leak between Play sessions | domain reload disabled | this series uses no statics for crafting state; keep it that way |
## Design
| Symptom | Cause | Fix |
|---|---|---|
| Players grind for one resource | recipe costs not checked against level placement | economy table (part 1.4) |
| Recipe list is a wall of text | everything known at start | discovery: few starting recipes, new ones per area (Level Up! teach, test, twist) |
| Crafting feels like a menu, not part of the world | everything by hand, instantly | stations, time, requirements (fire, rain), outputs waiting at the station |
| Nobody uses a recipe | its output has no use | Recipe Audit "DEAD END" check; give it a use or cut it |
| Waiting at a progress bar | long timers with nothing else to do | queue and walk away; shorter timers early |
## Sources
- [[Inventory Systems]] (re-entrancy and save notes)
- [[UI Toolkit and Diegetic UI]], [[7.UI Toolkit Pitfalls and Performance]]
- [[Level Up! (Review)]], [[Game Programming Patterns (Review)]]
- `ListView` (row recycling, `bindItem`): https://docs.unity3d.com/6000.5/Documentation/ScriptReference/UIElements.ListView.html