Hello,
In my game, a player collects items, stores them in their backpack, which I write as an array to device permanent memory.
I can retrive such items and use them.
HOWEVER, when I “use” them I want to remove them from the players backpack and inventory.
The item no longer exists ANYWHERE.
This is when I am stuck.
ON SCREEN:
The item remains viewable.
CONSOLE:
When I run through the array, the item has been removed.
JSON FILE:
Partial reference to the object remains, which is why the object is still showing on screen.
Line 11 to 18
CODE:
Line 612 is where I am attempting to remove the item found in the array using its index/key reference.
[{ "myName": "Red Scales", "\_proxy": "\<type 'userdata' is not supported by JSON.\>", "\_class": { "removeEventListener": "\<type 'function' is not supported by JSON.\>", "addEventListener": "\<type 'function' is not supported by JSON.\>", "\_\_index": "\<reference cycle\>" } }, { "\_functionListeners": { "tap": [ "\<type 'function' is not supported by JSON.\>"] }, "myName": "Wooden Wheel" }, { "\_proxy": "\<type 'userdata' is not supported by JSON.\>", "\_functionListeners": { "tap": ["\<type 'function' is not supported by JSON.\>"] }, "myName": "Wheelbarrow", "\_class": { "removeEventListener": "\<type 'function' is not supported by JSON.\>", "addEventListener": "\<type 'function' is not supported by JSON.\>", "\_\_index": "\<reference cycle\>" } } ]
-- remove item from inventory function gfRemoveItemFromInventory(removeItemIn) print("item to remove from array: " .. removeItemIn) -- write inventory to permanent memory for use by inventory overlay whenever necessary local pathFileInventoryArray = system.pathForFile( "fileInventoryArray.json", system.DocumentsDirectory ) local fileInvArr = io.open( pathFileInventoryArray, "r" ) if fileInvArr then -- test it variable holds file/path exists local contentFileInvArr = fileInvArr:read( "\*a" ) io.close( fileInvArr ) local arrInv = json.decode( contentFileInvArr ) if arrInv then print("arrInv") for indexA, valueA in pairs(arrInv) do print("indexA: " .. indexA .. " valueA.myName: " .. valueA.myName) if (valueA.myName == removeItemIn) then print("found item to remove: " .. valueA.myName ) print("found item to remove position: " .. indexA ) table.remove( arrInv, indexA) local fileInvArrWrite = io.open( pathFileInventoryArray, "w" ) fileInvArrWrite:write( json.encode( arrInv ) ) io.close( fileInvArrWrite ) fileInvArrWrite = nil -- break end end else print ("no arrInv") end --test check arrInv to see if item still lives in it or not. print("test full arrInv After update") for indexA, valueA in pairs(arrInv) do print("indexA: " .. indexA .. " valueA.myName: " .. valueA.myName) end end end