Would you use a matrix to make a grid based inventory system?

Not sure why you would use a matrix when you could just use 2d tables
Something like this would probably work well.

local inventory = {
    {}, -- row 1
    {}, -- row 2
    {}, -- row 3
}

-- to access stuff
print(inventory[1][1]) -- first item in row one
print(inventory[1][2]) -- second item in row one

print(inventory[2][3]) -- third item in row two

-- to set stuff
inventory[1][1] = "Some item name"
inventory[2][1] = "Some item again"

-- to remove stuff
inventory[1][1] = nil
inventory[2][1] = nil

-- to check if there is something occupying the inventory slot
if inventory[1][1] == nil then
    print("Nothing here!")
else
    print(inventory[1][1], "is there!")
end
1 Like

Well that explains how you’d store the items; but how would you detect what the item you’re dragging, is over which slots and such?

this would be a matrix

{
    {1, 0, 1, 0},
    {0, 1, 0, 1},
    {1, 0, 1, 0},
    {0, 1, 0, 1}
}

so what you are doing in the code you sent is creating a matrix

4 Likes

Well obviously the 2d array would be the backend.

In order to drag items and actually see the items, you would need to delve into some UI API. And probably associate each slot to a 2d array position. There’s actually a special UI Table Layout which could be used really well in this situation.

Here’s something for dragging UI.

Dragging the UI would be a simple task. How would you detect if the gui is over which slots is honestly whats puzzling me.

Check out this post here

So the idea here is that everytime the player moves the mouse, check all the frames in the inventory gui to see if they overlap?

Hey! That would work but it would be highly ineffective. You just need one table, loop through the table using for i, v in pairs(table) do. Then generate the slots inside that loop.

To make it automatically sort into rows, UIGridLayout is great for that. Hope that help!

How would generating slots aide in detecting which slots the object is being hovered over?

Hey! To check if a slot is being hovered over, using :MouseEnter:Connect() and :MouseLeave:Connect() are the way to go! Inside the parentheses, insert the function that you would like to fire.

I don’t think you’re understanding. Detecting which frame the mouse is over is done just like you said, yes. However; when you are dragging the object around over the slots. The object is lets say 3 by 4 slots wide. How would you know which slots its over to check if it can be placed in those slots?

I get out of class soon, I can write a little example once I get back.

1 Like

Would appreciate that. Not asking for someone to write the whole script for me, I’m just not understanding how that would help you tell which slots the Object is over.

Why do you need to check multiple slots? Wouldn’t a single draggable equip only occupy a single slot?

Uh that’s not what this is.

For reference this is what I’m trying to recreate essentially.

https://www.youtube.com/watch?v=W-HRdTpOXe4&t=25s

Okay so single draggable equips which occupy multiple slots. Does the size of the draggable equip correspond to the number of slots it occupies?

Something like that i guess? I’m still not sure what you’re meaning.

Uh yes? I’m just a little confused what “single draggable equips” are. But the answer is yes, the size does correspond the the number of slots it occupies.

You could absolutely use a matrix. The method you use should be heavily dependent on what functionality you want to see.

What you’ll see if you look at the Roblox Backpack source code is this:

-- 140
local SlotsByTool = {} 
-- 929
				local slot = LowestEmptySlot or MakeSlot(UIGridFrame)
				for i = slot.Index, 1, -1 do
					local curr = Slots[i] -- An empty slot, because above
					local pIndex = i - 1
					if pIndex > 0 then
						local prev = Slots[pIndex] -- Guaranteed to be full, because above
						prev:Swap(curr)
					else
						curr:Fill(tool)
					end
				end
-- 941

Diving deeper into this source code with ctrlF+SlotsByTool will show you this variable’s main functionality. It would be very similar to what you’re searching for.

The truth is that you don’t actually need to keep track of rows unless you want every slot to be accessible for organization (backpack does not have this functionality, rather it decides what slot is open and places new objects there).


That said, another option is to write each inventory slot with OOP, and attach a metatable to it that indexes functions which will handle most of your work for you. This method would provide additional functionality, depending on if you need it.

When you have created all slots, write them all to the same metatable that indexes your functions. That metatable will read your other slots and their information, etc…


If most of what you want is a readable table to keep track of what a player’s inventory is currently occupied of – this can be done with a matrix as you said above.

1 Like

Ah this is very helpful! I’m still lost on the main problem I’ve stated; detecting which slots the object is hovering over when you’re dragging it that way you can actually check if its occupied using that matrix.