Calling a json table in another scene. (Rob Miracle load save)

Hi guys,

I’ve been trying to figure out how I can load json tables with Rob Miracle load save library, but I can’t seem to figure it out. I’ve got my main file setup, this checks if there already is a json file at the directory and works fine.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- hide the status bar display.setStatusBar( display.HiddenStatusBar ) -- include the Corona "composer" module local composer = require "composer" -- load menu screen composer.gotoScene("menu") -- Create save file local loadsave = require("loadsave") local beginSettings = {} beginSettings.level = 1 beginSettings.bonus = 0 if loadsave.loadTable("settingsGame.json", system.DocumentsDirectory) == nil then local settings = beginSettings loadsave.saveTable(settings, "settingsGame.json", system.DocumentsDirectory) end local testvar = loadsave.loadTable("settingsGame.json", system.DocumentsDirectory) print(testvar.level) ------------------

How could I use the data in this tables in other scenes? I keep getting a nil value when I try to call: load save.loadTable

Any help would be great!

Kind regards

What is the specific error you’re getting and what’s the code around where the error is happening?

Rob

Hi,

I’m just getting an error that I’m trying to do something with a nil value. So my guess is that my table isn’t importing like it should. Correct me if I’m wrong, but this is the code to import the table, change the table, and save it again, or am I making a mistake?

So first of all in my menu scene, I’m declaring a variable at the top of my code to which I’ll save my table to.

local settings

After that I’m assigning the table to the variable in the show phase of my scene.

settings = loadsave.loadTable("settingsGame.json", system.DocumentsDirectory)

After that I’m checking what the highest level of the player is in this table and I’m updating the value if needed.

if settings.level \< 2 then settings.level = 2 loadsave.saveTable(settings, "settingsGame.json", system.DocumentsDirectory) end

And that is kind of it. But it just isn’t working for me.

Did I do something wrong?

Kind regards

Bram

Also, something really strange.

My app doesn’t compile and spits the errors mentioned above on my Imac and Macbook pro, BUT I’m able to build my app on windows. But when I open the app when I compiled it to an apk with my windows pc, my phone just presents the background color to me. Nothing more.

Can you please use Copy and Paste and copy the the exact error in it’s entirety and like the 10 lines before it, then use Paste to paste the error here. I need to see the error message, it’s stack trace including line numbers and the exact language of the error to be able to help you.

Rob

Hi Rob

This is the log that I get when I’m running the app in the iOS emulator.

mei 04 08:54:57.604 [iOS Simulator] ERROR: Runtime error

mei 04 08:54:57.604 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: attempt to index upvalue ‘settings’ (a nil value)

mei 04 08:54:57.605 [iOS Simulator] stack traceback:

mei 04 08:54:57.605 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: in function </Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:104>

mei 04 08:54:57.605 [iOS Simulator] ?: in function ‘dispatchEvent’

mei 04 08:54:57.605 [iOS Simulator] ?: in function ‘_nextTransition’

mei 04 08:54:57.605 [iOS Simulator] ?: in function <?:1492>

mei 04 08:54:57.605 [iOS Simulator] ?: in function ‘gotoScene’

mei 04 08:54:57.626 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/main.lua:15: in main chunk

mei 04 08:54:57.626 [iOS Simulator] Runtime Error: Runtime error: /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: attempt to index upvalue ‘settings’ (a nil value)

mei 04 08:54:57.626 [iOS Simulator]

mei 04 08:54:57.626 [iOS Simulator] stack traceback:

mei 04 08:54:57.626 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: in function </Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:104>

mei 04 08:54:57.626 [iOS Simulator] ?: in function ‘dispatchEvent’

mei 04 08:54:57.626 [iOS Simulator] ?: in function ‘_nextTransition’

mei 04 08:54:57.626 [iOS Simulator] ?: in function <?:1492>

mei 04 08:54:57.627 [iOS Simulator] ?: in function ‘gotoScene’

mei 04 08:54:57.627 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/main.lua:15: in main chunk

So I’m only getting this error on physical devices and the ios emulator, not on the standard corona emulator.

Is this the exact thing you needed?

Kind regards

Bram

Now we need to see the first 120 lines of menu.lua. Your error is on line 112 of menu.lua. When you copy/paste the code please click the blue <> button in the edit bar with bold and italics. Paste the code in the window that opens up.

----------------------------------------------------------------------------------------- -- -- menu.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" -- include save and load library and assign vars local loadsave = require("loadsave") -- telling corona to recycle the scene when we change it composer.recycleOnSceneChange = true -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 local centerX, centerY = display.contentCenterX, display.contentCenterY local centerX, centerY = display.contentCenterX, display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local originX = display.screenOriginX local originY = display.screenOriginY local maxWidth = display.screenOriginX + actualW local maxHeight = display.screenOriginY + actualH local titleTxt local scoreBox local leftBorder local rightBorder local highestLevelText local highestLevelNumber local playBtn local playTxt local storeBtn local storeTxt local settings -- "scene:create()" function scene:create( event ) local sceneGroup = self.view display.setDefault("background", 45/255, 63/255, 81/255) titleTxt = display.newText("Money Stack", centerX, centerY \* 0.25, "BebasNeue Bold", centerY \* 0.15) rightBorder = display.newImage("Img/envr/rightBorder.png") rightBorder.anchorX = 1 rightBorder.anchorY = 0 rightBorder.x = actualW rightBorder.y = originY rightBorder.xScale = 0.3 rightBorder.yScale = 0.3 leftBorder = display.newImage("Img/envr/leftBorder.png") leftBorder.anchorX = 0 leftBorder.anchorY = 0 leftBorder.x = originX leftBorder.y = originY leftBorder.xScale = 0.3 leftBorder.yScale = 0.3 highestLevelText = display.newText("Highest Level", centerX, centerY \* 0.75, "BebasNeue Regular", centerY \* 0.09) scoreBox = display.newImage("Img/envr/highScoreBox.png") scoreBox.x = centerX scoreBox.y = centerY scoreBox.xScale = 0.3 scoreBox.yScale = 0.3 playBtn = display.newRect(actualW, actualH \* 0.85, actualW \* 0.4, actualH \* 0.08) playBtn.anchorX = 1 playTxt = display.newText("Play", actualW/1.25, actualH \* 0.85, "BebasNeue Regular", centerY \* 0.09) playTxt:setFillColor(0,0,0) storeBtn = display.newRect(originX, actualH \* 0.85, actualW \* 0.4, actualH \* 0.08) storeBtn.anchorX = 0 storeTxt = display.newText("Store", actualW \* 0.2, actualH \* 0.85, "BebasNeue Regular", centerY \* 0.09) storeTxt:setFillColor(0,0,0) -- local grid = display.newImage("Background-Grid-graph-paper-HD-Wallpaper-iPhone-6-plus.jpg") -- grid.x = centerX + 20.5 -- grid.y = centerY -- grid.xScale = 0.296 -- grid.yScale = 0.278 -- grid.alpha = 0.5 end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then settings = loadsave.loadTable("settingsGame.json", system.DocumentsDirectory) print(settings.level) elseif ( phase == "did" ) then highestLevelNumber = display.newText(settings.level, centerX, centerY \* 1.04, "BebasNeue Bold", (actualW/actualH)\*70) end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen) -- Insert code here to "pause" the scene -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view -- Insert code here to clean up the scene -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

This is the code for the menu scene :slight_smile:

I think you have one of two things happening. Either a) you’re not getting to your code in main.lua where you are creating the table if it doesn’t exist or b) you’re trying to load the settings in your scene before you actually create the default version in main.lua. Luckily the cure is the same. You’re calling composer.gotoScene() before you create your save file. Move the composer.gotoScene() line to the very bottom of your main.lua.

The reason it’s working in the simulator is likely due to you having gotten the save file created successfully at some point in your Simulator’s sandbox folder. If you were to go there and delete the settingsGame.json file, the problem would start showing up in your simulator runs as well.

Rob

I’ve spent so many hours trying to fix it and that is the solution :o

I guess it’s the simple bugs that are the hardest to spot :slight_smile:

Thanks Rob,

You are a legend!

What is the specific error you’re getting and what’s the code around where the error is happening?

Rob

Hi,

I’m just getting an error that I’m trying to do something with a nil value. So my guess is that my table isn’t importing like it should. Correct me if I’m wrong, but this is the code to import the table, change the table, and save it again, or am I making a mistake?

So first of all in my menu scene, I’m declaring a variable at the top of my code to which I’ll save my table to.

local settings

After that I’m assigning the table to the variable in the show phase of my scene.

settings = loadsave.loadTable("settingsGame.json", system.DocumentsDirectory)

After that I’m checking what the highest level of the player is in this table and I’m updating the value if needed.

if settings.level \< 2 then settings.level = 2 loadsave.saveTable(settings, "settingsGame.json", system.DocumentsDirectory) end

And that is kind of it. But it just isn’t working for me.

Did I do something wrong?

Kind regards

Bram

Also, something really strange.

My app doesn’t compile and spits the errors mentioned above on my Imac and Macbook pro, BUT I’m able to build my app on windows. But when I open the app when I compiled it to an apk with my windows pc, my phone just presents the background color to me. Nothing more.

Can you please use Copy and Paste and copy the the exact error in it’s entirety and like the 10 lines before it, then use Paste to paste the error here. I need to see the error message, it’s stack trace including line numbers and the exact language of the error to be able to help you.

Rob

Hi Rob

This is the log that I get when I’m running the app in the iOS emulator.

mei 04 08:54:57.604 [iOS Simulator] ERROR: Runtime error

mei 04 08:54:57.604 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: attempt to index upvalue ‘settings’ (a nil value)

mei 04 08:54:57.605 [iOS Simulator] stack traceback:

mei 04 08:54:57.605 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: in function </Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:104>

mei 04 08:54:57.605 [iOS Simulator] ?: in function ‘dispatchEvent’

mei 04 08:54:57.605 [iOS Simulator] ?: in function ‘_nextTransition’

mei 04 08:54:57.605 [iOS Simulator] ?: in function <?:1492>

mei 04 08:54:57.605 [iOS Simulator] ?: in function ‘gotoScene’

mei 04 08:54:57.626 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/main.lua:15: in main chunk

mei 04 08:54:57.626 [iOS Simulator] Runtime Error: Runtime error: /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: attempt to index upvalue ‘settings’ (a nil value)

mei 04 08:54:57.626 [iOS Simulator]

mei 04 08:54:57.626 [iOS Simulator] stack traceback:

mei 04 08:54:57.626 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:112: in function </Volumes/USB Bram Va/Money Stack Test/Test Money Stack/menu.lua:104>

mei 04 08:54:57.626 [iOS Simulator] ?: in function ‘dispatchEvent’

mei 04 08:54:57.626 [iOS Simulator] ?: in function ‘_nextTransition’

mei 04 08:54:57.626 [iOS Simulator] ?: in function <?:1492>

mei 04 08:54:57.627 [iOS Simulator] ?: in function ‘gotoScene’

mei 04 08:54:57.627 [iOS Simulator] /Volumes/USB Bram Va/Money Stack Test/Test Money Stack/main.lua:15: in main chunk

So I’m only getting this error on physical devices and the ios emulator, not on the standard corona emulator.

Is this the exact thing you needed?

Kind regards

Bram

Now we need to see the first 120 lines of menu.lua. Your error is on line 112 of menu.lua. When you copy/paste the code please click the blue <> button in the edit bar with bold and italics. Paste the code in the window that opens up.

----------------------------------------------------------------------------------------- -- -- menu.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" -- include save and load library and assign vars local loadsave = require("loadsave") -- telling corona to recycle the scene when we change it composer.recycleOnSceneChange = true -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 local centerX, centerY = display.contentCenterX, display.contentCenterY local centerX, centerY = display.contentCenterX, display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local originX = display.screenOriginX local originY = display.screenOriginY local maxWidth = display.screenOriginX + actualW local maxHeight = display.screenOriginY + actualH local titleTxt local scoreBox local leftBorder local rightBorder local highestLevelText local highestLevelNumber local playBtn local playTxt local storeBtn local storeTxt local settings -- "scene:create()" function scene:create( event ) local sceneGroup = self.view display.setDefault("background", 45/255, 63/255, 81/255) titleTxt = display.newText("Money Stack", centerX, centerY \* 0.25, "BebasNeue Bold", centerY \* 0.15) rightBorder = display.newImage("Img/envr/rightBorder.png") rightBorder.anchorX = 1 rightBorder.anchorY = 0 rightBorder.x = actualW rightBorder.y = originY rightBorder.xScale = 0.3 rightBorder.yScale = 0.3 leftBorder = display.newImage("Img/envr/leftBorder.png") leftBorder.anchorX = 0 leftBorder.anchorY = 0 leftBorder.x = originX leftBorder.y = originY leftBorder.xScale = 0.3 leftBorder.yScale = 0.3 highestLevelText = display.newText("Highest Level", centerX, centerY \* 0.75, "BebasNeue Regular", centerY \* 0.09) scoreBox = display.newImage("Img/envr/highScoreBox.png") scoreBox.x = centerX scoreBox.y = centerY scoreBox.xScale = 0.3 scoreBox.yScale = 0.3 playBtn = display.newRect(actualW, actualH \* 0.85, actualW \* 0.4, actualH \* 0.08) playBtn.anchorX = 1 playTxt = display.newText("Play", actualW/1.25, actualH \* 0.85, "BebasNeue Regular", centerY \* 0.09) playTxt:setFillColor(0,0,0) storeBtn = display.newRect(originX, actualH \* 0.85, actualW \* 0.4, actualH \* 0.08) storeBtn.anchorX = 0 storeTxt = display.newText("Store", actualW \* 0.2, actualH \* 0.85, "BebasNeue Regular", centerY \* 0.09) storeTxt:setFillColor(0,0,0) -- local grid = display.newImage("Background-Grid-graph-paper-HD-Wallpaper-iPhone-6-plus.jpg") -- grid.x = centerX + 20.5 -- grid.y = centerY -- grid.xScale = 0.296 -- grid.yScale = 0.278 -- grid.alpha = 0.5 end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then settings = loadsave.loadTable("settingsGame.json", system.DocumentsDirectory) print(settings.level) elseif ( phase == "did" ) then highestLevelNumber = display.newText(settings.level, centerX, centerY \* 1.04, "BebasNeue Bold", (actualW/actualH)\*70) end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen) -- Insert code here to "pause" the scene -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view -- Insert code here to clean up the scene -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

This is the code for the menu scene :slight_smile:

I think you have one of two things happening. Either a) you’re not getting to your code in main.lua where you are creating the table if it doesn’t exist or b) you’re trying to load the settings in your scene before you actually create the default version in main.lua. Luckily the cure is the same. You’re calling composer.gotoScene() before you create your save file. Move the composer.gotoScene() line to the very bottom of your main.lua.

The reason it’s working in the simulator is likely due to you having gotten the save file created successfully at some point in your Simulator’s sandbox folder. If you were to go there and delete the settingsGame.json file, the problem would start showing up in your simulator runs as well.

Rob

I’ve spent so many hours trying to fix it and that is the solution :o

I guess it’s the simple bugs that are the hardest to spot :slight_smile:

Thanks Rob,

You are a legend!