← Pixel Dreamer Studio

An inventory that never loses an item and never overfills a stack

Top up the stacks you have, open new slots for the rest, and hand back whatever still does not fit.

Adding items to the first matching stack and then clamping it to the stack limit silently throws the extra away, and adding without the clamp makes a stack bigger than the limit. In our Godot 4.7 test, 250 herbs added to a stack of 90 with a limit of 99 left 99 herbs and lost 241 with the clamp, and made one stack of 340 without it. Topping up the stack and putting the rest into new slots gave 99, 99, 99 and 43, all 340 herbs, and when the bag was full the script handed back the 44 that did not fit.

The code

Save it as stack_add.gd, load it with const StackAdd := preload("res://stack_add.gd") and add items with var left := StackAdd.add(slots, "spirit_herb", 250, 99, 20): the item, how many, the stack limit and how many slots the bag has. The slots are an Array of Dictionaries with an item and a count. If left is more than 0, drop that many on the ground, or refuse the pickup.

extends RefCounted
## Adds items to an inventory with a stack limit: tops up the stacks of that
## item first, opens new slots for the rest, and gives back what did not fit,
## so nothing is lost and no stack goes over the limit.
##   var left := StackAdd.add(slots, "spirit_herb", 250, 99, 20)
##   if left > 0:
##       drop_on_ground("spirit_herb", left)     # or refuse the pickup
## slots is an Array of Dictionaries {"item": String, "count": int}.


static func add(slots: Array, item: String, count: int, max_stack: int, max_slots: int) -> int:
	for s in slots:  # 1) top up stacks that are not full yet
		if count <= 0:
			break
		if s["item"] == item and s["count"] < max_stack:
			var put := mini(count, max_stack - int(s["count"]))
			s["count"] += put
			count -= put
	while count > 0 and slots.size() < max_slots:  # 2) new stacks in empty slots
		var put := mini(count, max_stack)
		slots.append({"item": item, "count": put})
		count -= put
	return count  # 3) what did not fit


static func total(slots: Array, item: String) -> int:
	var n := 0
	for s in slots:
		if s["item"] == item:
			n += int(s["count"])
	return n

What we measured

A bag of 5 slots holding herbs 90 and ore 10, with a stack limit of 99, then 250 herbs added. Two runs gave the same results.

Why it happens

mini(count + n, 99) answers how many fit in this stack, but the rest of n has to go somewhere, and nothing in that line sends it anywhere. Without the clamp the stack just keeps growing. The script works in three steps: top up every stack of that item that is not full, open new slots for whole stacks while there is room, then return the count that is left so the caller decides what happens to it.

What to change

A whole pill refining system to fill the bag from: the Xianxia Alchemy & Pill Refining System for Godot 4 has herbs, recipes, a furnace heat minigame, experimenting to discover recipes, levels and save/load.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Xianxia Alchemy & Pill Refining System for Godot 4 has herbs, recipes, a furnace heat minigame, experimenting to discover recipes, levels and save/load.

More how-tos: Game systems