Otter
Snippets
Otter
Upstream Otter 1.0.1 Tested
Inventory Bag with Capacity Limits
Manage player inventory slots with strict maximum capacity checks.
Architecture & Runtime Preview
Otter 1.0.1Zero-dependency Otter 1.0.1 snippet (Game Systems). Copy the parameterized template or run the self-contained example.
- 1Category: Game Systems
- 2Attribution: Otter Reusable Library (Official)
- 3Zero external pack dependencies — paste directly into any .ot file
Source Code (2 files)
a InventoryBag has
maxSlots
currentCount
items
.
to createInventoryBag slots
bag is a InventoryBag
maxSlots of bag is slots
currentCount of bag is 0
bagItems are empty
items of bag is bagItems
return bag
.
to addItemToBag bag item
cur is currentCount of bag
slots is maxSlots of bag
if cur is at least slots
return false
.
currentCount of bag is cur plus 1
itemList is items of bag
add item to itemList
return true
.
a InventoryBag has
maxSlots
currentCount
items
.
to createInventoryBag slots
bag is a InventoryBag
maxSlots of bag is slots
currentCount of bag is 0
bagItems are empty
items of bag is bagItems
return bag
.
to addItemToBag bag item
cur is currentCount of bag
slots is maxSlots of bag
if cur is at least slots
return false
.
currentCount of bag is cur plus 1
itemList is items of bag
add item to itemList
return true
.
playerBag is createInventoryBag 2
added1 is addItemToBag playerBag "Wooden Bow"
added2 is addItemToBag playerBag "Quiver"
added3 is addItemToBag playerBag "Iron Shield"
if added1 is not true
fail with "addItemToBag failed to add item 1"
.
if added2 is not true
fail with "addItemToBag failed to add item 2"
.
if added3 is not false
fail with "addItemToBag permitted adding item exceeding capacity"
.
if length of items of playerBag is not 2
fail with "inventory bag item count mismatch: expected 2"
.
say "Inventory capacity checks passed."
Installation & Usage
otter snippet inspect game-inventory-capacity
Copy template.ot directly into your .ot source file, or run example.ot with 'otter run example.ot'.