Just wanted to show off my janky inventory system :)

What it can do: Store items and allow for selected items.
What it can’t do: click items to select, move items, drop items, or anything like that.
Yeah it sucks, but I’m still proud of it :slight_smile:

function init(self)
	msg.post(".","acquire_input_focus")
	inventory = {} -- Stores Item IDs per slot
	inventorycheck = {} -- Used for later, but generally does the same as inventory
	isFull = {} -- stores if an item is in a certain slot
	itemCount = {} -- stores how many of an item is in x slot
	countSlot = {} -- used for an easier time programming
	slot = {} -- used for an easier time programming
	hotbarSlot = {} -- same as the last 2
	heldSlot = 1 -- stores the held slot number
	heldItem = 0 -- stores the held item id
	for i=1,9,1 do -- creates the item slots
		inventory[i] = 0
		inventorycheck[i] = 0
		itemCount[i] = 0
		isFull[i] = false
		slot[i] = gui.get_node("slot" .. i)
		countSlot[i] = gui.get_node("count" .. i)
		hotbarSlot[i] = gui.get_node("inv" .. i)
	end
	onPickup(self, 3, 1) -- gives player pickaxe
	onPickup(self, 4, 1) -- gives player sword (not of use yet)
end

function onPickup(self,item, count)
	for i=1,9,1 do
		if isFull[i] == false then -- checks if x slot has an item
			isFull[i] = true
			inventory[i] = item
			itemCount[i] = itemCount[i] + count
			break
		elseif inventory[i] == item and itemCount[i] < 50 then -- if it does have an item, checks if that item is the same as pickup
			itemCount[i] = itemCount[i] + count 
			break
		end
	end
end

function update(self, dt)
	for i=1,9,1 do
		gui.set_text(countSlot[i], itemCount[i])
		if inventory[i] == inventorycheck[i] then -- inventorycheck[] is used to prevent every item image being updated per frame
		else
			inventorycheck[i] = inventory[i]
			if inventory[i] == 4 then
				gui.play_flipbook(slot[i], "startersword")
			elseif inventory[i] == 3 then
				gui.play_flipbook(slot[i], "pickaxe")
			elseif inventory[i] == 2 then
				gui.play_flipbook(slot[i], "LogItem")
			elseif inventory[i] == 1 then
				gui.play_flipbook(slot[i], "RockCollectable")
			else
				gui.play_flipbook(slot[i], "nothing")
			end
		end
	end
end

function on_message(self, message_id, message, sender) -- handles the item gaining
	if message_id == hash("r") then
		onPickup(self, 1, 1) -- i used functions instead of writing the same code over and over :D
	elseif message_id == hash("log") then
		onPickup(self, 2, 1)
	end	
end

function on_input(self, action_id, action) -- handles increasing the selected slots size, took a long time
	for i=1,9,1 do
		if action_id == hash(i) then
			gui.play_flipbook(gui.get_node("inv" .. heldSlot), "inventory icon")
			self.guiPos = gui.get_position(gui.get_node("inv" .. heldSlot))
			self.guiPos.z = 0
			gui.set_position(gui.get_node("inv" .. heldSlot), self.guiPos)
			heldItem = inventory[i]
			heldSlot = i
			self.guiPos = gui.get_position(hotbarSlot[i])
			self.guiPos.z = 1
			gui.set_position(hotbarSlot[i], self.guiPos)
			gui.play_flipbook(hotbarSlot[i], "inventory selected")
			break
		end
	end
end

9 Likes