I have been stuck on this for probably 3 weeks now, i’ve tried everything I can think of and I have edited my code several times trying to figure this out. I’ll first post a video showing you guys the issue i’m having and then I’ll post the code that I currently have.
Video here: https://youtu.be/YFkJmR27_V0
As you may be able to see from the video, the issue i’m having is not necessarily the equipping of items, it is having my inventory recognize what items are in inventory when I leave and come back to the scene.
When I first enter the scene I can click on the items and you can see that they equip/un-equip and it fills the proper slots. When I exit the scene and come back in, it seems to no longer recognize anything.
This is the code that I have…
I have a table “equipInventory” that holds all items that should be in the players inventory
local equipInventory = { [1] = {name = "Sword2", frame = 4, description = "Weapon", class = "Warrior", itemLevel = 1, strength = 5, armor = 0, plusHealth = 0, plusSpeed = 0, }, [2] = {name = "Shield", frame = 2, description = "Armor", class = "Warrior", itemLevel = 1, strength = 0, armor = 5, plusHealth = 0, plusSpeed = 0, }, [3] = {name = "Boots", frame = 3, description = "Trinket", class = "Warrior", itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [4] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [5] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [6] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [7] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [8] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [9] = {name = "Sword3", frame = 3, description = "Weapon", class = "Warrior", itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [10] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [11] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [12] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [13] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [14] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [15] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [16] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [17] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [18] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [19] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, [20] = {name = nil, frame = 1, description = nil, class = nil, itemLevel = 1, strength = 0, armor = 0, plusHealth = 0, plusSpeed = 0, }, } return equipInventory
I also have a table called “myData” this table stores all the information for all of the players characters that he can pick from and play with
local myData = { [1] = {name = "Blair", image = "Images/AR.png", frame = 3, dead = false, class = "Warrior", level = 10, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 15, speed = .12, weaponEquip = nil, shieldEquip = equipInventory[2], trinket1 = equipInventory[3], trinket2 = equipInventory[3], BSK = blairSkills[1], SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [2] = {name = "Lily", image = "Images/CG.png", frame = 4, dead = false, class = "Rogue", level = 5, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 12, speed = .16, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", BSK = lilySkills[1], SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [3] = {name = "Kylianna", image = "Images/AP.png", frame = 2, dead = false, class = "Mage", level = 6, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", BSK = kyliannaSkills[1], SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [4] = {name = "Robyn", image = "Images/CY.png", frame = 6, dead = false, class = "Healer", level = 30, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", BSK = robynSkills[1], SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [5] = {name = "Edward", image = "Images/SG.png", frame = 9, dead = false, class = "Paladin", level = 20, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", BSK = edwardSkills[1], SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [6] = {name = "Blake", image = "Images/SP.png", frame = 10, dead = false, class = "Hunter", level = 18, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", BSK = blakeSkills[1], SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [7] = {name = "noname", image = "Images/SB.png", frame = 8, dead = false, class = "Druid", level = 36, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [8] = {name = "needname", image = "Images/CO.png", frame = 5, dead = false, class = "Shaman", level = 12, curExp = 0, expToNextLvl = 100, maxHealth = 100, health = 100, strength = 10, armor = 10, speed = .12, weaponEquip = "none", shieldEquip = "none", trinket1 = "none", trinket2 = "none", SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, } return myData
I have a third table called “activeParty” this looks exactly the same as the table “myData” except it only holds table items (to help me load the proper characters when a player chooses his party and starts to play
local activeParty = { [1] = {name = nil, image = "Images/GC.png", frame = 7, dead = false, class = nil, level = nil, curExp = 0, expToNextLvl = 100, maxHealth = nil, health = nil, strength = nil, armor = nil, speed = nil, weaponEquip = nil, shieldEquip = nil, trinket1 = "none", trinket2 = "none", BSK = nil, SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [2] = {name = nil, image = "Images/GC.png", frame = 7, dead = false, class = nil, level = nil, curExp = 0, expToNextLvl = 100, maxHealth = nil, health = nil, strength = nil, armor = nil, speed = nil, weaponEquip = nil, shieldEquip = nil, trinket1 = "none", trinket2 = "none", BSK = nil, SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [3] = {name = nil, image = "Images/GC.png", frame = 7, dead = false, class = nil, level = nil, curExp = 0, expToNextLvl = 100, maxHealth = nil, health = nil, strength = nil, armor = nil, speed = nil, weaponEquip = nil, shieldEquip = nil, trinket1 = "none", trinket2 = "none", BSK = nil, SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, [4] = {name = nil, image = "Images/GC.png", frame = 7, dead = false, class = nil, level = nil, curExp = 0, expToNextLvl = 100, maxHealth = nil, health = nil, strength = nil, armor = nil, speed = nil, weaponEquip = nil, shieldEquip = nil, trinket1 = "none", trinket2 = "none", BSK = nil, SK1 = nil, SK2 = nil, SK3 = nil, SK4 = nil, SK5 = nil, SK6 = nil, }, } return activeParty
“myData” and “activeParty” are essentially the same exact table except “activeParty” only holds 4 characters, so as info on the “activeParty” table changes, it also changes on the “myData” table
So those are the tables working behind the scenes now I’ll move onto the code for equipping items…
under the composer scene:show (phase == will) I have this code
for i=1, 20 do --#equipInventory do helps look at table even if there is no item so we can fill with blank item if i \< 5 then local setX = 460 local setY = 220 InventoryDisplay[i] = display.newSprite( itemSheet, itemSequenceData ) InventoryDisplay[i].rep = equipInventory[i] InventoryDisplay[i]:setFrame(equipInventory[i].frame) sceneGroup:insert(InventoryDisplay[i]) InventoryDisplay[i].x = setX + (i \* 120) InventoryDisplay[i].y = setY InventoryDisplay[i].pos = i InventoryDisplay[i]:addEventListener("touch", equipItems ) end if i \> 4 and i \< 9 then local setX = 460 local setY = 340 InventoryDisplay[i] = display.newSprite( itemSheet, itemSequenceData ) InventoryDisplay[i].rep = equipInventory[i] InventoryDisplay[i]:setFrame(equipInventory[i].frame) sceneGroup:insert(InventoryDisplay[i]) InventoryDisplay[i].x = (setX + ( i \* 120 )) - 480 InventoryDisplay[i].y = setY InventoryDisplay[i].pos = i InventoryDisplay[i]:addEventListener("touch", equipItems ) end
(I cut out half of the for loop to try and keep the post as small as possible)
This for loop does a few things… It makes yet another table called InventoryDisplay and makes each element in that table equal to each corresponding equipInventory table item.
So
InventoryDisplay[1].rep = equipInventory[1]
InventoryDisplay[2].rep = equipInventory[2]
InventoryDisplay[3].rep = equipInventory[3]
…
I did this, so that I could make the items on the screen have touch listeners and be inter-actable
When each item on the inventory is clicked I run it through code to check the items specs (class, weapon or shield, etc) I also have it check to see if the character set in the edit window matches so it can be equipped.
that code looks like this…
local function equipItems(event) -- I think this actually works -- --local tempItemHold local charToInventory -- sends equipped item to equip inventory & inventory display local inventoryToChar -- sends item from inventory to character if event.phase == "ended" then print("Touch Recognized") print("Item slot " .. event.target.pos) print(event.target.rep.class) print(event.target) print(event.target.rep) -- should be same as equipInventory print(equipInventory[9]) if equipEdit[1].class == event.target.rep.class then print("class matches") if event.target.rep.description == "Weapon" then -- this works for equiping weapons for a specific class print("this is a weapon for a warrior") if equipEdit[1].weaponEquip == nil then --table.remove(equipInventory, event.target.pos) --table.insert( equipInventory, 1, blankItem ) inventoryToChar = equipInventory[event.target.pos] charToInventory = blankItem equipEdit[1].weaponEquip = inventoryToChar equipInventory[event.target.pos] = charToInventory elseif equipEdit[1].weaponEquip ~= nil then inventoryToChar = equipInventory[event.target.pos] charToInventory = equipEdit[1].weaponEquip equipEdit[1].weaponEquip = inventoryToChar equipInventory[event.target.pos] = charToInventory --InventoryDisplay[event.target.pos] = charToInventory -- tempItemHold = equipInventory[event.target.pos] -- table.remove(equipInventory, event.target.pos) -- table.insert(equipInventory, 1, equipEdit[1].weaponEquip ) end ------------------------------------------------------------------------- for i=1, #activeParty do if equipEdit[1].name == activeParty[i].name then print("Updatting Active Party") activeParty[i].weaponEquip = equipEdit[1].weaponEquip print(activeParty[i].weaponEquip) break end end for i=1, #myData do if equipEdit[1].name == myData[i].name then print("Updatting My Data") myData[i].weaponEquip = equipEdit[1].weaponEquip print(myData[i].weaponEquip) break end end updateInventory() end
(again, I cut out some of the code to try and keep it short, but whats missing is just what you can see here, but repeated for each item class, shiled, trinket, etc…)
Please ignore any of my print() statements and any of the commented out code… those were things I use to try and figure this all out.
The idea of this code is it checks if the item clicked is something that can be equipped by the selected character, if so, it equips it.
it does basically with this code here
"inventoryToChar = equipInventory[event.target.pos]
charToInventory = equipEdit[1].weaponEquip"
you will also notice the 2 loops within the function that go through and update the two tables “myData” and “activeParty”
and at the very end you see a trigger for another function called updateInventory() this is supposed to keep all the background tables for equipInventory and InventoryDisplay in check aligned with each other
that code looks like this
local function updateInventory(event) print("updatting Inventory") for i=1, 20 do --#InventoryDisplay is table we are working with InventoryDisplay[i].rep = equipInventory[i] InventoryDisplay[i]:setFrame(equipInventory[i].frame) end end
this function takes a piece from the initial function that builds the table back in the composer scene (phase == will) section.
I have fiddled with the table.insert and table.remove functions as well, but the issue that I have is still the same as with the current method that I am using and have shown here in this post.
seeing as I can enter the equip screen for the very first time and everything works, that tells me that the composer scene == will code is working (at least the first time). seeing as I can click on the items while in the scene that tells me that the code I use to update the table is working.
My hunch is that something is off with the code I have for the composer scene (phase == will) seeing as once I leave and come back, it all seems to go to hell.
I know this is kind of a lengthy post, but im hoping someone can take a look at the code I have provided and help with a solution.
Thanks