Inventory variation

Hello!

I tried to make character dynamic inventory:

  1. At once our Inventory is empty (Table is empty)

  2. Char find some item

  3. Clicked on item and that one inserts in inventory

And want to ask can i use this variation? Сan there be better variations?

-- Set Items -- item\_1 = display.newRect( 0, 0, 50, 50 ) item\_2 = display.newRect( 0, 0, 50, 50 ) item\_2.x = 200 -- .X coordinates for testing item\_3 = display.newRect( 0, 0, 50, 50 ) item\_3.x = 400 -- .X coordinates for testing local spacingX -- Spacing between items local cellCount = 0 -- Counter local new\_inventory = {} -- New table for inventory local maxAmount -- Maximum ammount of items in table local function input\_in() spacingX = 50 -- set spacing cellCount = 0 -- set counter (every calls of function the counter resets) maxAmount = #new\_inventory -- set all values in table for i = 1 ,#new\_inventory do new\_inventory[i].y = 200 new\_inventory[i].x = spacingX --cellCount = cellCount + 1 if maxAmount \> cellCount then new\_inventory[i].x = new\_inventory[i].x + spacingX new\_inventory[i].y = 200 cellCount = cellCount + 1 spacingX = spacingX+50 end end end local function add\_in (self , event) if( event.phase == "ended" ) then table.insert( new\_inventory, self ) -- input Item in inventory input\_in() self.touch = nil end end item\_1.touch = add\_in item\_1:addEventListener( "touch", item\_1 ) item\_2.touch = add\_in item\_2:addEventListener( "touch", item\_2 ) item\_3.touch = add\_in item\_3:addEventListener( "touch", item\_3 )