Skip to main content

Inventory System

This example shows a simple inventory system using Reheaven.

The inventory stores items, selected item, and equipped item.

Key Point

Inventory state keeps track of what the player owns, selects, and equips.

Inventory State

InventoryState.lua
local computed = Reheaven.computed

local inventory, setInventory = Reheaven.signal({
items = {};
selectedItem = nil;
equippedItem = nil;
})

local itemCount = computed(function()
return #inventory().items
end)

local hasEquippedItem = computed(function()
return inventory().equippedItem ~= nil
end)

Inventory Actions

InventoryActions.lua
local function addItem(itemName)
local current = inventory()
local nextState = table.clone(current)

nextState.items = table.clone(current.items)
-- double clone because items is a nested table.
-- this is where a deepClone helper can be useful :3
table.insert(nextState.items, itemName)

setInventory(nextState)
end

local function selectItem(itemName)
local current = inventory()
local nextState = table.clone(current)

nextState.selectedItem = itemName

setInventory(nextState)
end

local function equipSelectedItem()
local current = inventory()

if not current.selectedItem then
return
end

local nextState = table.clone(current)

nextState.equippedItem = current.selectedItem

setInventory(nextState)
end

Inventory UI

InventoryUI.lua
local effect = Reheaven.effect

effect(function()
itemCountLabel.Text = `Items: {itemCount()}`
end)

effect(function()
local state = inventory()

selectedLabel.Text = state.selectedItem or "None"
equippedLabel.Text = state.equippedItem or "None"
end)

Button Input

InventoryButtons.lua
swordButton.MouseButton1Click:Connect(function()
selectItem("Sword")
end)

equipButton.MouseButton1Click:Connect(function()
equipSelectedItem()
end)

Inventory Flow

Item added

Inventory state changes

Selected or equipped item changes

UI reacts
Table State

Because inventory is stored as a table signal, the examples clone the table before making changes.

Nested tables like items should also be cloned before changing them.