Using The Storyboard.savevalue

Hello! In order to save a value and load it on another screen should I use 

 

storyboard.savevalue = variable  

 

 inside 

 

function scene:exitScene( event )  

 

then reload it in the new scene? If yes, where? Inside the enterScene?

 

Thank you.

 

Sure, enterScene would be a fine place to read that value.  Once it’s been added as a table member to the storyboard object, you can access it anywhere almost like a global variable.

Hello Rob, and thanks for the answer. I have created two storyboard scenes and I still can’t find a way to save the variable and load it.

 

Here are my files:

 

main.lua

 

local storyboard = require "storyboard" storyboard.gotoScene( "scene1" )

 

scene1.lua

 

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() ---------------------------------------------------------------------------------- local variable local text local funct function funct( self, event ) if event.phase == "began" then storyboard.gotoScene( "scene2", "crossFade", 75 ) return true end end function scene:createScene( event ) local group = self.view variable = math.random(1,10) text = display.newText(variable, display.contentWidth / 2 ,display.contentHeight / 2, "Verdana", 40) text:setTextColor( 255,255,255 ) screenGroup:insert( text ) text.touch = funct text:addEventListener( "touch", text ) end function scene:enterScene( event ) local group = self.view storyboard.purgeAll() end function scene:exitScene( event ) local group = self.view storyboard.testVar = { [1] = variable } end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

 

and scene2.lua

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() ---------------------------------------------------------------------------------- local variable local text function scene:createScene( event ) local group = self.view text = display.newText(variable, display.contentWidth / 2 ,display.contentHeight / 2, "Verdana", 90) text:setTextColor( 255,255,255 ) screenGroup:insert( text ) end function scene:enterScene( event ) local group = self.view variable = storyboard.testVar[1] storyboard.purgeAll() end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

 

I still need some help! Thank you!

It seems like this behaviour has changed. I couldn’t use storyboard variables this weekend either.

try:

[lua]

storyboard.testVar = {}
storyboard.testVar[1] = variable    
 

[/lua]

I used

 

storyboard.testVar = {} storyboard.testVar[1] = variable

in scene1.lua  to save the variable.

 

However, when I try to go to scene2 i get the following error in console:

 

 

Runtime error path\scene2.lua:11: bad argument #1 to 'newText' ... string expected, got nil

 

 

Thank you.

I just made my own test. And it seems to work actually.

 

main.lua

 

local storyboard = require "storyboard" storyboard.testVar = {} storyboard.testVar[1] = "test" storyboard.gotoScene("scripts.puzzle")

 

puzzle.lua

 

function scene:enterScene(event) print (storyboard.testVar[1]) end

 

prints test

Part of learning to program is learning how to read error messages.  The error you are getting is very clearly explained. 

 

Runtime error
path\scene2.lua:11: bad argument #1 to ‘newText’ … string expected, got nil

 

 

It’s in scene.2.lua

Line 11

It involves the newText function (display.newText)

and argument #1 being passed in is nil when it should be a string.

 

Edit scene2.lua, go to line 11 and figure out why the string you are trying to display is nil.

Sure, enterScene would be a fine place to read that value.  Once it’s been added as a table member to the storyboard object, you can access it anywhere almost like a global variable.

Hello Rob, and thanks for the answer. I have created two storyboard scenes and I still can’t find a way to save the variable and load it.

 

Here are my files:

 

main.lua

 

local storyboard = require "storyboard" storyboard.gotoScene( "scene1" )

 

scene1.lua

 

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() ---------------------------------------------------------------------------------- local variable local text local funct function funct( self, event ) if event.phase == "began" then storyboard.gotoScene( "scene2", "crossFade", 75 ) return true end end function scene:createScene( event ) local group = self.view variable = math.random(1,10) text = display.newText(variable, display.contentWidth / 2 ,display.contentHeight / 2, "Verdana", 40) text:setTextColor( 255,255,255 ) screenGroup:insert( text ) text.touch = funct text:addEventListener( "touch", text ) end function scene:enterScene( event ) local group = self.view storyboard.purgeAll() end function scene:exitScene( event ) local group = self.view storyboard.testVar = { [1] = variable } end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

 

and scene2.lua

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() ---------------------------------------------------------------------------------- local variable local text function scene:createScene( event ) local group = self.view text = display.newText(variable, display.contentWidth / 2 ,display.contentHeight / 2, "Verdana", 90) text:setTextColor( 255,255,255 ) screenGroup:insert( text ) end function scene:enterScene( event ) local group = self.view variable = storyboard.testVar[1] storyboard.purgeAll() end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

 

I still need some help! Thank you!

It seems like this behaviour has changed. I couldn’t use storyboard variables this weekend either.

try:

[lua]

storyboard.testVar = {}
storyboard.testVar[1] = variable    
 

[/lua]

I used

 

storyboard.testVar = {} storyboard.testVar[1] = variable

in scene1.lua  to save the variable.

 

However, when I try to go to scene2 i get the following error in console:

 

 

Runtime error path\scene2.lua:11: bad argument #1 to 'newText' ... string expected, got nil

 

 

Thank you.

I just made my own test. And it seems to work actually.

 

main.lua

 

local storyboard = require "storyboard" storyboard.testVar = {} storyboard.testVar[1] = "test" storyboard.gotoScene("scripts.puzzle")

 

puzzle.lua

 

function scene:enterScene(event) print (storyboard.testVar[1]) end

 

prints test

Part of learning to program is learning how to read error messages.  The error you are getting is very clearly explained. 

 

Runtime error
path\scene2.lua:11: bad argument #1 to ‘newText’ … string expected, got nil

 

 

It’s in scene.2.lua

Line 11

It involves the newText function (display.newText)

and argument #1 being passed in is nil when it should be a string.

 

Edit scene2.lua, go to line 11 and figure out why the string you are trying to display is nil.