0

I have done lots of research on invetory system for point and click game in Lua and corona. I have come across this example,I am doing something similar to this,but I need a dynamic inventory system. I mean if I have 4 slots,and all them are full the fifth object go to next slot,so there will be an arrow to the right so I can click on ;and go to the next page. And imagine there are 5 items,and I have 4 slots,the fifth slot would be on the next page. I use the third item,and third slot would then be empty,so I want the fourth and fifth item automatically move back to third and fourth slot. I have hard time figuring this out. Thanks for advance.

local myInventoryBag={}
local maxItems = 10 -- change this to how many you want

myInventoryBag[5]=3 -- Hammer for instance
myInventoryBag[4]=7 -- A metal Pipe for instance

local function getImageForItem(thisItem)
    local itemNumber = tonumber(thisItem)
    local theImage=""

    if itemNumber==3 then
        theImage="hammer.png"
    elseif itemNumber == 7 then
        theImage="metalpipe.png"
    elseif ... -- for other options
        ...
    else
        return nil
    end

    local image = display.newImage(theImage)
    return image
end

local function displayItems()
    local i
    for i=1,#myInventoryBag do
        local x = 0  -- calculate based on the i
        local y = 0 --  calculate based on the i

        local image = getImageForItem(myInventoryBag[i])

        if image==nil then return end

        image.setReferencePoint(display.TopLeftReferencePoint)
        image.x = x
        image.y = y
    end
end
mtsvetkov
  • 885
  • 10
  • 21
Bo Dash
  • 113
  • 1
  • 3
  • 10

2 Answers2

3
local itemImages =
{
    [0] = display.newImage('MISSING_ITEM_IMAGE.PNG'),
    [3] = display.newImage('hammer.png'),
    [7] = display.newImage('metalpipe.png'),
}

function getImageForItem(itemId)
    return itemImages[itemId] or itemImages[0]
end

local myInventoryBag={}
local maxItems = 10 -- change this to how many you want
local visibleItems = 4 -- show this many items at a time (with arrows to scroll to others)

-- show inventory items at index [first,last]
local function displayInventoryItems(first,last)
    local x = 0 -- first item goes here
    local y = 0 -- top of inventory row
    for i=first,last do
        image = getImageForItem(myInventoryBag[i])
        image.x = x
        image.y = y
        x = x + image.width
    end
end

-- show inventory items on a given "page"
local function displayInventoryPage(page)
    page = page or 1 -- default to showing the first page
    if page > maxItems then
        -- error! handle me!
    end
    local first = (page - 1) * visibleItems + 1
    local last = first + visibleItems - 1
    displayInventoryItems(first, last)
end

myInventoryBag[5] = 3 -- Hammer for instance
myInventoryBag[4] = 7 -- A metal Pipe for instance

displayInventoryPage(1)
displayInventoryPage(2)
Mud
  • 28,277
  • 11
  • 59
  • 92
  • Thanks for the help,After looking around and playing around a lot,it seems to me that there are many ways to create a dynamic inventory system.since I have been into programming for 7 month,and still learning;sometimes I tend to think ,there must be only one way,or solution to the problem. – Bo Dash Sep 28 '12 at 03:33
2

Basically what you would do is loop through all the inventory slots and check if the slot is empty. If it's empty, place the item in that slot and stop the loop. If it's not, go to the next one.

If you want to remove an item from the inventory, you can simply call table.delete(myInventoryBag, slotToEmpty).

For pages, you'd simply have a page variable. When drawing the inventory slots, just loop from slots (page-1) * 4 + 1 to page * 4.

(Edit: I'd highly recommend using proper indentation, as it will make the code much much more readable.)

Darkwater
  • 1,358
  • 2
  • 12
  • 25
  • I am a bit confused on page vars,and the numbers that you have used. – Bo Dash Sep 27 '12 at 11:27
  • I think he means to have a variable to keep track of the page that the player is on, and then use it to check all of the slots. – Jutanium Sep 28 '12 at 01:44