Making an Inventory for the Player


so I’m quite new to Defold but have already done a few smaller games here and there, like Snake.
Now I want to make something bigger but ran into a little problem: I’m quite unsure how I should make the player’s inventory.
I know that I want it to be weight-based, like for example in Skyrim. My idea would be to make a table containing every item and its attributes (for example its weight) and adding these items together with the associated attributes to the table which manages the player’s inventory, if the player picks that item up.
But here’s the problem: I have no clue on how to add such “attributes” to the table entries.

So now I’m also questioning if handling everything with tables is even the right idea.

I Hope someone can help me with that!



Something like this maybe:

local BROADSWORD = hash("broadsword")
local DAGGER = hash("dagger")

-- the list of items in the game
-- each item is referred to by an item id
-- this is easy to pass around and it can be used to
-- lookup the item with all properties
local ITEMS = {
		damage = 12,
		weight = 5,
	[DAGGER] = {
		damage = 6,
		weight = 2,

-- the player inventory
-- we track the current weight of the items in the inventory as well as the
-- maximum number of items
local inventory = {
	current_weight = 0,
	max_weight = 35,
	items = {},

-- pick-up an item and add it to the inventory
-- @param item_id Id of the item to pick up
-- @return success true if picked up
-- @return error_message Error message if it wasn't possible to pick up the item
local function pick_up(item_id)
	-- get the item to pick up
	local item = ITEMS[item_id]

	-- check if the item weighs too much
	if inventory.current_weight + item.weight > inventory.max_weight then
		return false, "You can't carry that much!"

	-- add it!
	table.insert(inventory.items, item)
	inventory.current_weight = inventory.current_weight + item.weight
	return true

-- pick up an item
-- show an error if it fails
-- update inventory gui if ok
local ok, err = pick_up(BROADSWORD)
if not ok then


You can try this too :slight_smile: