Super proud of a very basic inventory system I made. I wanted to avoid using 2d arrays, so instead I used a single array of all the items in inventory, and some math to create x,y positions where each item should go. It's a class itself so I can use it for any scenario I might need (it has variable size as well). Super stoked with this one! I'm very new to love and game development, so I'm sure there are much bigger and better ways to go about this but I wanted to share regardless.
Inventory System
-
- Prole
- Posts: 13
- Joined: Sun Mar 30, 2025 5:00 am
Inventory System
Last edited by interested_party on Thu Apr 24, 2025 5:39 am, edited 1 time in total.
-
- Prole
- Posts: 13
- Joined: Sun Mar 30, 2025 5:00 am
Re: Inventory System
I can't figure out the youtube embed... Sorry 

Re: Inventory System
Code: Select all
[youtube]vgbTIJr4H0o[/youtube]
If it possible, add the .love file, not just a video.
Re: Inventory System
Can you elaborate on the exact structure, e.g. by providing a sample code?interested_party wrote: ↑Wed Apr 23, 2025 3:36 am Super proud of a very basic inventory system I made. I wanted to avoid using 2d arrays, so instead I used a single array of all the items in inventory, and some math to create x,y positions where each item should go. It's a class itself so I can use it for any scenario I might need (it has variable size as well). Super stoked with this one! I'm very new to love and game development, so I'm sure there are much bigger and better ways to go about this but I wanted to share regardless.
[youtube]https://www.youtube.com/watch?v=vgbTIJr4H0o[/youtube]

-
- Prole
- Posts: 13
- Joined: Sun Mar 30, 2025 5:00 am
Re: Inventory System
Sorry just got back to this. I attached a .love file, I didn't know if a .love should contain the love engine itself, but I assumed everybody just has love in their system path so I did not. You're welcome to extract and take a look at the code (I warn you it's not the most pleasant looking stuff). The equipment class has a bunch of extra params that are for a game I'm working on. The basic concept for this inventory was this:
This next function fills the self.positions table with all of the positions for items at each spot in the inventory. It also puts placeholder 0's in the self.invent array.
From there I listen to mouse events to indicate object selection and have a swapPos method to index the corresponding positions to where the mouse button is released. I'm still implementing some other methods (such as removing items) but the general stuff is there!
Code: Select all
local inventory = Class{
init = function(self, w, h, slot_size)
self.x = 0
self.y = 0
self.w = w
self.h = h
self.slotSize = slot_size
self.xw = slot_size * w
self.yh = slot_size * h
self.maxItem = w * h
self.positions = {}
self.invent = {}
end
}
Code: Select all
function inventory:initPositions()
for i=1, self.maxItem do
table.insert(self.invent, 0)
end
for i = 1, self.maxItem do
local column = 0
local row = math.ceil(i/self.w) - 1
if i % self.w == 0 then
column = self.w - 1
else
column = (i % self.w) - 1
end
table.insert(self.positions, {self.x + (self.slotSize * column), self.y + (self.slotSize * row)})
end
end
- Attachments
-
InventoryDemo.love
- (1.25 MiB) Downloaded 4 times
Re: Inventory System
Ah, I see, you have implemented a custom class in a table. I was confused when you said you didn't want a 2D array and implemented it differently... So now you basically have a integer indexed table with items. Sounds easier to handle to me than a 2D matrix.

-
- Prole
- Posts: 13
- Joined: Sun Mar 30, 2025 5:00 am
Re: Inventory System
Yes! That was my thought too. Probably not the most efficient thing ever, but makes my own brain hurt less.
Who is online
Users browsing this forum: No registered users and 4 guests