Jump to content

[Question] Inventory grid size


manif14102

Recommended Posts

20 hours ago, Patrick said:

Hi! Please first search what you want to ask.

 

Thank you, but I still do not understand how to use this. Will it be normal to create a table for the slots, and when adding an item to the inventory, check with a loop whether the next slot is free if the item is for example 3x3, or is this a bad idea? I tried a couple of algorithms, but something fails.

Link to comment
4 minutes ago, manif14102 said:

Thank you, but I still do not understand how to use this. Will it be normal to create a table for the slots, and when adding an item to the inventory, check with a loop whether the next slot is free if the item is for example 3x3, or is this a bad idea? I tried a couple of algorithms, but something fails.

Hello manif14102,

since you have trouble implementing the algorithms described in that topic, how about posting your code so that we can assist you? I think that you may have personally overlooked something that we can improve as a community ?

- Martin

  • Like 1
Link to comment
3 hours ago, The_GTA said:

Hello manif14102,

since you have trouble implementing the algorithms described in that topic, how about posting your code so that we can assist you? I think that you may have personally overlooked something that we can improve as a community ?

- Martin

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:

Spoiler

PoC73Zq.png

 

Edited by manif14102
Link to comment

Dear manif14102,

I unfortunately cannot understand what you are trying to do with the getFreeSlot function. But here is my take on how it could be done:

local function createInventoryBox(width, height)
  local box = {};
  local rows = {};
  
  for n=1,height,1 do
    rows[n] = {};
  end
  
  function box.getItem(x, y)
    if (y < 1) or (y > height) then return false; end;
    if (x < 1) or (x > width) then return false; end;
    
    local row = rows[y];
    
    return row[x];
  end
  
  function box.setItem(x, y, item)
    if (y < 1) or (y > height) then return false; end;
    if (x < 1) or (x > width) then return false; end;
    
    local row = rows[y];
    
    if not (row) then
      row = {};
      rows[y] = row;
    end
    
    row[x] = item;
    return true;
  end
  
  function box.tryPlaceItem(item, x, y, iwidth, iheight)
    if (iwidth == 0) or (iheight == 0) then return false; end;
    
    -- Only if we do not cross the border.
    if (x < 1) or (x + iwidth - 1 > width) then return false; end;
    if (y < 1) or (y + iheight - 1 > height) then return false; end;
    
    -- Check if free space.
   	for witer=0,iwidth-1,1 do
      for hiter=0,iheight-1,1 do
        local exist_item = box.getItem(x + witer, y + hiter);
        
        if (exist_item) then
          return false;
        end
      end
    end
    
    -- Place the item there.
    for witer=0,iwidth-1,1 do
      for hiter=0,iheight-1,1 do
        box.setItem(x + witer, y + hiter, item);
      end
    end
    
    return true;
  end
  
  function box.clearItem(x, y, iwidth, iheight)
    for witer=0,iwidth-1,1 do
      for hiter=0,iheight,1 do
        box.setItem(x + witer, y + hiter, nil);
      end
    end
  end
  
  return box;
end

(code has not been tested)

The above code implements a two-dimensional array with helper functions for placement. Thus one could do...

local box = createInventoryBox(3, 3);

assert( box.tryPlaceItem(1, 1, 2, 2, "Knife") == true );
assert( box.tryPlaceItem(1, 2, 2, 2, "Key") == false );

Hopefully this does give you an idea ;) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...