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
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.
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!
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?
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.
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.
-- 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.
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.