Inventory system for adventure game

Hi, every one! I am new to corona. I am trying to make adventure game using corona storyboard API. I managed to create two scenes and transition beetwen them. But now I face with inventory. I found some tutorials and do all accordingly them, But I got troubles.

I write a functions for display inventory objects, add and remove thne but when I trying to add a handler for drag and drop then I got en error
Runtime error d:\corona\firsttry\inventory.lua:126: attempt to index local ‘itemInSlot’ (a nil value)

Here is my code:

scene.lua

[lua]require(“inventory”)

local storyboard = require(“storyboard”)
local ui = require(“ui”)
local scene = storyboard.newScene()
local back, tap1, tap2, tap3, textMemory

– onCreate

function scene:createScene(event)
local group = self.view

local function GoToMenu(event)
storyboard.gotoScene(true, “menu”, “fade”, 500)
end

back = display.newImageRect(“img/Scene1.png”, 1024, 768);
back.x = display.contentCenterX;
back.y = display.contentCenterY;
back.alpha = 0.9;
group:insert(back);

tap2 = ui.newButton
{
default = “img/tap2.png”,
over = “img/tap2.png”,
onRelease = GoToMenu,
}

tap2.x = display.contentCenterX;
tap2.y = 650;
group:insert(tap2)

textMemory = display.newText(“Memory:”, 0, 0, native.SystemFont, 32)
textMemory:setTextColor(0)
textMemory.x = 200
textMemory.y = 200
group:insert(textMemory)

end

–Draw

function scene:enterScene(event)
local group = self.veiw
– if storyboard.prevScene > 0 then storyboard.removeScene(“scene”…storyboard.prevScene) end-- ??? ?? ??? ??? ???
storyboard.purgeScene( “menu” )
– storyboard.prevScene = 1;
– storyboard.removeScene( “menu” );
local showMem = function()
–text3.isVisible = true
textMemory.text = textMemory.text … collectgarbage(“count”)/1000 … “MB”
textMemory.x = display.contentWidth * 0.5
end
memTimer = timer.performWithDelay( 1000, showMem, 1 )
end

–exit

function scene:exitScene(event)
local group = self.veiw
end

–destroy

function scene:destroyScene(event)
local group = self.view
end

addItemToInvetory(1,1,true)

–displayInventory()

scene:addEventListener(“createScene”,scene)

scene:addEventListener(“enterScene”,scene)

scene:addEventListener(“exitScene”,scene)

scene:addEventListener(“exitScene”,scene)

return scene[/lua]

inventory.lua

–Inventory

[lua]local iconAcross – number of icons to display
local iconWidth – icon width
local iconHeigth – iconHeigth
local offsetX, offsetY – the position offset where the icons would be
local colNum, rowNum – the row and column from the inventory slot
local slotNumber – number of slots

local items =
{
“bag.png”,
“lamp.png”,
“glasses.png”
}

local slots = 8 – number of slots
local inventoryBag = {nil,nil,nil,nil,nil,nil,nil,nil} – this where the items are stored

–The local functions that are used to manage Inventory
local addItemToInventory
local removeItemFromInventory
local saveInventory
local loadInventory
local displayInventory

local function printTouch( event )
if event.target then
local bounds = event.target.contentBounds
– print( “event(” … event.phase … “) (”…event.x…","…event.y…") bounds: “…bounds.xMin…”,"…bounds.yMin…","…bounds.xMax…","…bounds.yMax )
end
end

local function onTouch(event)

local t = event.target – ??? ??? ? ???

local phase = event.phase

if “began” == phase then
– make target top object
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)

– check if player tap on the object
t.isFocus = true

– store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
end

end

return true
end
–for i = 1, 2 do
– local item = display.newImage(items[i], true)
– item:addEventListener(“touch”, onTouch)
– end

function addItemToInvetory(itemIndex, inSlot, redraw)
local itemIndex = itemIndex
local inSlot = inSlot
– local itemInSlot = InventoryBag[inSlot]

local redraw = redraw or false

if inSlot > 8 or inSlot < 1 then return end

– if itemInSlot ~= nil then
– removeFromInventory(itemIndex)
– end

inventoryBag[inSlot] = itemIndex
– inventoryBag[inSlot]:addEventListener(“touch”, onTouch)

if redraw == true then
displayInventory()
end

end

function removeItemFromInventory(itemIndex, redraw)

local i
local redraw = redraw or false

for i = 0, slots do
if inventoryBag[i] == itemIndex then inventoryBag[i] = 0 end
end

if redraw == true then
displayInventory()
end
end

function displayInventory()

– local group = self.view

inventory = display.newImageRect(“img/inventory/inventory.png”, 1024, 768)
inventory.x = 511
inventory.y = 391
inventory.alpha = 0.9
– group:insert(inventory)

local i
for i=1, slots do
local itemInSlot = inventoryBag[i]
if itemInSlot ~= nil or itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch) end
end
end [/lua]

Does anyone know how to fix it? Or maybe another way to make inventory managment using corona?
Thank you! [import]uid: 216233 topic_id: 35558 reply_id: 335558[/import]

Should this bit be:

[lua]if itemInSlot ~= nil and itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch) end[/lua]

…otherwise even if itemInSlot is nil this code will still fire because itemInSlot isn’t 0 and it will attempt to load an image that doesn’t exist. [import]uid: 93133 topic_id: 35558 reply_id: 141324[/import]

That error typically means that display.newImage() failed to load the image. Was there an warning above it about not being able to find a particular image?

[import]uid: 199310 topic_id: 35558 reply_id: 141325[/import]

Solved!) I remove temInSlot ~= 0 and all works properly. But can you give me advice how to make than when player tap the object on the scene - it will fly to nearst empty inventory slot, and then player can drag and drop it. And also how to save inventory state? I think I should use file, but how I can load/save to file in lua? [import]uid: 216233 topic_id: 35558 reply_id: 141339[/import]

Should this bit be:

[lua]if itemInSlot ~= nil and itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch) end[/lua]

…otherwise even if itemInSlot is nil this code will still fire because itemInSlot isn’t 0 and it will attempt to load an image that doesn’t exist. [import]uid: 93133 topic_id: 35558 reply_id: 141324[/import]

That error typically means that display.newImage() failed to load the image. Was there an warning above it about not being able to find a particular image?

[import]uid: 199310 topic_id: 35558 reply_id: 141325[/import]

Solved!) I remove temInSlot ~= 0 and all works properly. But can you give me advice how to make than when player tap the object on the scene - it will fly to nearst empty inventory slot, and then player can drag and drop it. And also how to save inventory state? I think I should use file, but how I can load/save to file in lua? [import]uid: 216233 topic_id: 35558 reply_id: 141339[/import]

@Roman, I’m going to snipe Rob’s own tutorial series. It’s helped me immensely with figuring out how to save/load items to a separate table:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

For your issue with which inventory slots should be utilized, I’d suggest a bunch of if/elseif statements. Meaning, for example:

[lua]
if slot1 ~= “full”, then
–do something
elseif slot1 == “full” then
if slot2 ~= "full then
– do something else
end
end
end
[/lua]

It’ll take a bit of trial and error to get it to work for you, but that should start you out. Good luck! [import]uid: 135394 topic_id: 35558 reply_id: 142091[/import]

Thank you very much! I’ll try it!
[import]uid: 216233 topic_id: 35558 reply_id: 142541[/import]

@Roman, I’m going to snipe Rob’s own tutorial series. It’s helped me immensely with figuring out how to save/load items to a separate table:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

For your issue with which inventory slots should be utilized, I’d suggest a bunch of if/elseif statements. Meaning, for example:

[lua]
if slot1 ~= “full”, then
–do something
elseif slot1 == “full” then
if slot2 ~= "full then
– do something else
end
end
end
[/lua]

It’ll take a bit of trial and error to get it to work for you, but that should start you out. Good luck! [import]uid: 135394 topic_id: 35558 reply_id: 142091[/import]

Hello again! I can’t figure out how to make logic when user use item in wrong place it will placed automaticly in slot where it was.
I made something like this in event tap function…

elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
if t.x < 100 then – ??? ??? ?? ??? ???
t.x = 180
elseif t.x > 900 then – ??? ??? ?? ??? ???
t.x = 850
elseif t.x ~= t.x0 then
t.x = t.x0
end
end
t.y = _H - iconWidth + 40
end
t.x - is original position that I calculated in that way t.x0 = event.x - t.x

but item appered in left bottom of the screen.

I think that’s because in display function I set coordinates to item in this way
for i=1, slots do
local itemInSlot = inventoryBag[i]
if itemInSlot ~= nil then–or itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch)

local x = ((index *(iconWidth))-5)+85
local y = _H - iconWidth + 45

itemInSlot.x = x
itemInSlot.y = y

index = index + 1
end
end
but I try to remember this coordinates in varuable and all items appered in one slot. Maybe I should use some loop here
elseif t.x ~= t.x0 then
t.x = t.x0
?

Please, help! [import]uid: 216233 topic_id: 35558 reply_id: 143234[/import]

Thank you very much! I’ll try it!
[import]uid: 216233 topic_id: 35558 reply_id: 142541[/import]

Hello again! I can’t figure out how to make logic when user use item in wrong place it will placed automaticly in slot where it was.
I made something like this in event tap function…

elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
if t.x < 100 then – ??? ??? ?? ??? ???
t.x = 180
elseif t.x > 900 then – ??? ??? ?? ??? ???
t.x = 850
elseif t.x ~= t.x0 then
t.x = t.x0
end
end
t.y = _H - iconWidth + 40
end
t.x - is original position that I calculated in that way t.x0 = event.x - t.x

but item appered in left bottom of the screen.

I think that’s because in display function I set coordinates to item in this way
for i=1, slots do
local itemInSlot = inventoryBag[i]
if itemInSlot ~= nil then–or itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch)

local x = ((index *(iconWidth))-5)+85
local y = _H - iconWidth + 45

itemInSlot.x = x
itemInSlot.y = y

index = index + 1
end
end
but I try to remember this coordinates in varuable and all items appered in one slot. Maybe I should use some loop here
elseif t.x ~= t.x0 then
t.x = t.x0
?

Please, help! [import]uid: 216233 topic_id: 35558 reply_id: 143234[/import]

Hello again! I can’t figure out how to make logic when user use item in wrong place it will placed automaticly in slot where it was.
I made something like this in event tap function…

elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
if t.x < 100 then – ??? ??? ?? ??? ???
t.x = 180
elseif t.x > 900 then – ??? ??? ?? ??? ???
t.x = 850
elseif t.x ~= t.x0 then
t.x = t.x0
end
end
t.y = _H - iconWidth + 40
end
t.x - is original position that I calculated in that way t.x0 = event.x - t.x

but item appered in left bottom of the screen.

I think that’s because in display function I set coordinates to item in this way
for i=1, slots do
local itemInSlot = inventoryBag[i]
if itemInSlot ~= nil then–or itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch)

local x = ((index *(iconWidth))-5)+85
local y = _H - iconWidth + 45

itemInSlot.x = x
itemInSlot.y = y

index = index + 1
end
end
but I try to remember this coordinates in varuable and all items appered in one slot. Maybe I should use some loop here
elseif t.x ~= t.x0 then
t.x = t.x0
?

Please, help! [import]uid: 216233 topic_id: 35558 reply_id: 143234[/import]

Hello again! I can’t figure out how to make logic when user use item in wrong place it will placed automaticly in slot where it was.
I made something like this in event tap function…

elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
if t.x < 100 then – ??? ??? ?? ??? ???
t.x = 180
elseif t.x > 900 then – ??? ??? ?? ??? ???
t.x = 850
elseif t.x ~= t.x0 then
t.x = t.x0
end
end
t.y = _H - iconWidth + 40
end
t.x - is original position that I calculated in that way t.x0 = event.x - t.x

but item appered in left bottom of the screen.

I think that’s because in display function I set coordinates to item in this way
for i=1, slots do
local itemInSlot = inventoryBag[i]
if itemInSlot ~= nil then–or itemInSlot ~= 0 then
itemInSlot = display.newImage(items[itemInSlot],true)
itemInSlot:addEventListener(“touch”, onTouch)

local x = ((index *(iconWidth))-5)+85
local y = _H - iconWidth + 45

itemInSlot.x = x
itemInSlot.y = y

index = index + 1
end
end
but I try to remember this coordinates in varuable and all items appered in one slot. Maybe I should use some loop here
elseif t.x ~= t.x0 then
t.x = t.x0
?

Please, help! [import]uid: 216233 topic_id: 35558 reply_id: 143234[/import]