Inventory System

Show off your games, demos and other (playable) creations.
Post Reply
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Inventory System

Post by interested_party »

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.
Last edited by interested_party on Thu Apr 24, 2025 5:39 am, edited 1 time in total.
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Re: Inventory System

Post by interested_party »

I can't figure out the youtube embed... Sorry :(
User avatar
darkfrei
Party member
Posts: 1262
Joined: Sat Feb 08, 2020 11:09 pm

Re: Inventory System

Post by darkfrei »

interested_party wrote: Wed Apr 23, 2025 3:38 am I can't figure out the youtube embed... Sorry :(

Code: Select all

[youtube]vgbTIJr4H0o[/youtube]
But you right, it is not intuitive at all. I had the same issue before the other had explained it to me.

If it possible, add the .love file, not just a video.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 792
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Inventory System

Post by dusoft »

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]
Can you elaborate on the exact structure, e.g. by providing a sample code?
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Re: Inventory System

Post by interested_party »

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:

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
}
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.

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
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!
Attachments
InventoryDemo.love
(1.25 MiB) Downloaded 4 times
User avatar
dusoft
Party member
Posts: 792
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Inventory System

Post by dusoft »

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.
interested_party
Prole
Posts: 13
Joined: Sun Mar 30, 2025 5:00 am

Re: Inventory System

Post by interested_party »

Yes! That was my thought too. Probably not the most efficient thing ever, but makes my own brain hurt less.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests