Finding the first stack of an item and checking its count misses everything in the other stacks. In our Godot 4.7 test the player had 9 iron ore in stacks of 3, 4 and 2, and a recipe that needs 5 said no. Taking the 5 from that first stack left it at -2. Count over every slot first, then take what you need stack by stack.
The code
Call count_of(slots, item) to show how many the player has and to grey out recipes they cannot make. Call take(slots, item, amount) when crafting: it returns false and takes nothing if there is not enough. When a recipe needs several items, check all their counts first, then take them all.
extends Node
## Count and take crafting items across every inventory stack.
## slots is an Array of Dictionaries like {"item": "iron_ore", "count": 4}, or null for an empty slot.
static func count_of(slots: Array, item: String) -> int:
var n := 0
for s in slots:
if s != null and s.item == item:
n += s.count
return n
static func take(slots: Array, item: String, amount: int) -> bool:
if count_of(slots, item) < amount:
return false # check first, so a failed craft takes nothing
for i in slots.size():
var s = slots[i]
if s == null or s.item != item:
continue
var used: int = mini(s.count, amount)
s.count -= used
amount -= used
if s.count == 0:
slots[i] = null # empty the slot
if amount == 0:
break
return true
What we measured
An inventory of iron ore 3, wood 5, iron ore 4, an empty slot and iron ore 2: 9 iron ore in total, and a recipe that needs 5. Two runs with the same results.
- First stack only: can craft false, with 9 in the bag. Taking 5 from it anyway: iron ore -2, wood 5, iron ore 4, empty, iron ore 2.
- The script: count 9. Taking 5: empty, wood 5, iron ore 2, empty, iron ore 2, with 4 left. The first stack was used up and emptied, and the second gave the other 2.
- Taking 20 with the script: false, and the inventory stayed exactly as it was.
Why it happens
Stacks have a size limit, or the player splits them, so the same item often sits in several slots. A check that stops at the first match only sees part of what the player has, and taking more than that stack holds drives it below zero. Checking the total before taking anything also means a failed craft never eats half the ingredients.
What to change
- The slot format: if your slots are Resources or nodes, only the item and count lookups change.
- Stack limits: when you add crafted items back, fill existing stacks up to the limit before using empty slots.
Recipes that work in any order: crafting recipe check. A gathering hit that feels solid: shake an object when hit. Hold a button to craft: a hold bar that takes the same time at any frame rate.
Ready-made gathering effects: Crafting & Gathering VFX for Godot 4 has 28 presets in Stylized and Retro PSX looks, including Mine Stone, Mine Crystal, Mine Gold Ore, Chop Wood and Gather Herb.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Crafting & Gathering VFX for Godot 4 has 28 ready-made crafting and gathering effects in Stylized and Retro PSX looks, including Mine Stone, Mine Gold Ore, Chop Wood and Gather Herb.
More how-tos for Crafting & Gathering VFX for Godot 4: Shake a rock or tree when hit, without it drifting · A hold-to-craft bar that takes the same time at any frame rate and finishes once
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene