local box = { } ; -- table with gui elements of inventory slots
-- init draw inventory gui blank slots
function getFreeSlot ( x, y ) --func get placing item dimension and return free slot
for i, j in ipairs ( box ) do
if ( j[4] == false ) and ( x == 1 ) and ( y == 1 ) then return i, j[2], j[3] end --return free id, x, y of slot when placing item has 1x1 dimension
end
end
--show inventory when press key
addCommandHandler("inv",
function ( )
for i = 1, 25 do -- 25 slots by y
for j = 1, 7 do -- 7 slots by x
local slotBoxImg = dgsCreateImage( 64 * j, 64 * i, 64, 64, nil, false, scrollpane, tocolor( 255, 255, 121, 50 ) ); -- ?
table.insert( box, { slotBoxImg, j, i, false } ); -- link to blank box image, pos x, pos y, slot statement(false is slot free)
end
end
end
--[[for example:
add item in inventory when player pickup this]]
slotid, x, y = getFreeSlot( 1, 1 ); --get free slot
box[slotid][4] = true; -- mark slot as used
local item = dgsCreateImage( 64 * x, 64 * y, 64, 64, nil, false, scrollpane, tocolor( 255, 235, 1250, 100 ) ); -- draw item icon for example
But i dont understand how to match slots if item has dimension > 1, like 3x3 or 5x2. And I'm not sure at all that this algorithm is correct
That's what happened so far with two 1x1 items: