Items, inventories and equipment for browser games. Item definitions as data, slot-based bags with stacking and stack limits, weight limits, adding with leftovers when full, removing across stacks, moving, merging, splitting and sorting, equipment slots with stat bonuses and swapping, change events for UI, save and load, and inventory UI for mouse, touch and gamepad. Includes a tested inventory...
Download skill (.zip) Files on GitHubInventory system
Drop-in: assets/inventory.js (tests: stacking, weight limit with leftovers, full bag, removing across stacks, split, merge,
sort and compact, equip and swap, save and load).
import { Inventory, Equipment } from "./core/inventory.js";
const ITEMS = { // data, in config or data/items.json
potion: { name: "Potion", icon: "potion", stack: 10, weight: 0.5, use: "heal", value: 10 },
sword: { name: "Iron sword", stack: 1, slot: "hand", stats: { atk: 5 }, weight: 3, value: 40 },
};
const bag = new Inventory(ITEMS, { slots: 24, maxWeight: 60 });
const left = bag.add("potion", 3); // 0 = all fit; otherwise drop the rest on the ground / "Bag full"
bag.onChange(() => ui.refresh());
const gear = new Equipment(ITEMS, ["head", "body", "hand", "ring"]);
gear.equip(bag, slotIndex); // old item goes back into the bag
const stats = gear.stats({ atk: 1, def: 0 }); // base + gear (+ power-ups, + skill tree mods)
save.set("bag", bag.toJSON()); bag.load(save.get("bag"));
Design choices
| Choice | Options |
|---|---|
| Limit | Slots (simple, visual), weight (simulation), or both |
| Stacks | Consumables stack (10–99); gear never stacks; unique items carry data (durability, rolls) |
| Full bag | Leftovers stay on the ground with a message; never silently delete items |
| Hotbar | First N slots, or a separate small Inventory bound to keys 1–9 / touch buttons |
UI
- Grid of slots with icon, count and rarity color (loot-system
RARITY). Tooltip on hover and on long-press for touch. - Mouse: drag and drop (
move). Touch: tap to select, tap a slot to move. Gamepad: d-pad focus + A to pick up / place. - Compare equipment stats on hover/selection (green up, red down).
- Pause the game (or slow it) while the full inventory is open in action games.
Pitfalls
Item IDs renamed after release (old saves break: game-save-migration), item data copied into saves (save IDs and counts,
not definitions), and duplicates through race conditions (always go through add / remove).