And, boys and girls, this is why you shouldn’t mix dictionary and array type tables. Simply nesting the individual levels under the dictionary key world[i]. level kept the dictionary keys in the same level and the array table in it’s own nest area world[i].level[k].
This has perplexed me for the better part of a week. below is code i am running in my main.lua to set up a file to hold sound settings and world/level scores/times/etc. Currently I am utilizing GGData.lua to load/save the file, but am also running into the same error with Rob’s LoadSave.lua.
The Problem
1st time I run the simulator, the main code runs fine, but the nested table world[x][y] are throwing errors that they don’t exist throughout my game. When I rerun the code, i get that the
[line 96]
[lua] print("levelInfo ====== "…world[1][1].id) [/lua]
error: …test code/main.lua:96: attempt to index field ‘?’ (a nil value)
is nil and freezes the simulator. So I’ve deduced that (1) the saved file isn’t starting fresh when I close and rerun the simulator and (2) there is some type of corruption in how I nest my tables. Please let me know if there are any obvious errors in my code that anyone sees.
thanks.
[lua]
local storyboard = require “storyboard”
local GGData = require “GGData”
display.setStatusBar(display.HiddenStatusBar)
– Load/Saving
local worldLocked --boolean check if world is able to be played
local world – table that hold all info (levels, scores, etc) within each world
local totalTimePlayed, totalTags, totalStars
local isMusicOn
– temp numbers for test instead of linking to the lua file
local levels = { {1,2,3,4}, {1,2,3}, {1,2} }
–load gameInfo file and put into gameInfo table
local gameInfo = GGData:new(“gameInfo”)
–sets the game up for 1st time use or loads existing scores
local function resetGame()
totalTimePlayed = 0
totalTags = 0
totalStars = 0
worldLocked = {}
world = {}
for i= 1, #levels do
--world stats
worldLocked[i] = true
world[i] = {}
world[i].id = “world”…i
world[i].totalWorldTags = 0
world[i].totalWorldStars = 0
--level stats
– local numLevels = #levels[i]
– print("numLevels: "…numLevels)
for k=1, #levels[i] do
world[i][k] = {}
world[i][k].id = “world”…i…“level”…k
world[i][k].isLocked = true --set level to locked, array-like
world[i][k].bestTime = 1 – dictionary of level
world[i][k].bestTagNum = 0 --dictionary of level
world[i][k].bestStarsNum = 0
print(“world[”…i…"]["…k…"].id ===== "… world[i][k].id)
end
world[i][1].isLocked = false – 1st level of every world is unlocked
end
– unlock 1st world and 1st level
worldLocked[1] = false
--settings
isMusicOn = false
end
resetGame()
print("levelInfo ====== "…world[1][1].id)
local function loadInfo()
print("world[1][1].id: "…world[1][1].id)
– settings
isMusicOn = gameInfo.isMusicOn or musicOn – boolean
--game stats
totalTimePlayed = gameInfo.totalTimePlayed or totalTimePlayed
print("totalTimePlaye=== "…totalTimePlayed)
totalTags = gameInfo.totalTags or totalTags
print("totalTags=== "… totalTags)
totalStars = gameInfo.totalStars or totalStars
print("totalStars=== "… totalStars)
– world stats
worldLocked = gameInfo.worldLocked or worldLocked
print("worldLocked[1]=== ", worldLocked[1])
world = gameInfo.world or world
print("world[1].id=== "… world[1].id)
print("world[1][1].id=== "… world[1][1].id)
gameInfo.isMusicOn = isMusicOn
gameInfo.isSfxOn = isSfxOn
gameInfo.totalTimePlayed = totalTimePlayed
gameInfo.totalTags = totalTags
gameInfo.totalStars = totalStars
gameInfo.worldLocked = worldLocked
gameInfo.world = world
–enable entire file to accessed by rest of game via storyboard
storyboard.gameInfo = gameInfo
storyboard.gameMode = “goToMenu”
storyboard.tempData = {}
gameInfo:save()
end
loadInfo()
print("gameInfo ======= "…#storyboard.gameInfo.world)
print("gameInfo ======= ",worldLocked[2])
print("levelInfo ====== "…world[1][1].id)
[/lua]