Saving User's Settings in game

Hi its me again…

I have a question which is how am I going save the user’s settings such as the user can turn on vibration button, currently in my settings menu I have a vibration button and once I checked it and exit the menu to the main screen and enter the settings again the button is not checked.

In short:

I enter settings->checked vibration button->exit setting page->go back to settings->Vibration button not checked.

What am I suppose to do so that it saves what the user’s preferences in the settings menu?

Thanks for the help!

Hi @w0shi21,

Are you saving these settings locally to the device? And also storing them in variables and then reading those values when the user returns to the settings menu?

Brent

I suppose that what you said is true, but if that’s the case, am i suppose to save it in a table or?
Here my settings file

--------------------------------------------------------------------------------- -- -- scene1.lua -- --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require( "widget" ) local xMultiplier = display.contentWidth/480 local yMultiplier = display.contentHeight/320 --local gameScene = display.newGroup() local screenWidth = display.contentWidth local screenHeight = display.contentHeight local button1,button2,button3 local slider = {} --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- local button = {} local backBtn local image, text1, text2, text3, memTimer --Handles the slide event for the volume slider local function sliderListener( event ) local slider = event.target local value = event.value print( "Slider at " .. value .. "%" ) end --Handles the press event for the checkbox local function onSwitchPress( event ) -- body local switch = event.target print("Switch with ID '"..switch.id.."' is on:" ..tostring(switch.isOn)) --Enable vibration when the check box for Vibrates is checked. if (switch.isOn) then -- If the checkbox is checked, then run this code. print( "Switch is on, Vibrate the device" ) system.vibrate() -- Vibrate the device end end local function onButtonEvent(event) local btn = event.target if btn.id == "start\_btn" then local function nextScene ( event ) storyboard.gotoScene( "LevelSelect","slideLeft",600) end timer.performWithDelay( 100, nextScene) elseif btn.id == "gallery\_btn" then local function nextScene ( event ) storyboard.gotoScene( "Gallery","slideLeft",600) end timer.performWithDelay( 100, nextScene) elseif btn.id == "close\_btn" then local function nextScene ( event ) storyboard.gotoScene( "MainMenu","fade",200) end timer.performWithDelay( 100, nextScene) elseif btn.id == "button3" then print( "Green" ) print (collectgarbage("count")/1000) collectgarbage( "collect" ) return true end end -- Called when the scene's view does not exist: function scene:createScene( event ) local screenGroup = self.view collectgarbage( "collect" ) bg.x = screenWidth / 2 bg.y = screenHeight / 2 screenGroup:insert(bg) --Sliders for volume controls slider[1] = widget.newSlider { id = "bgm", top = screenHeight \* 0.46, left = screenWidth \* 0.283, width = screenWidth \* 0.49, listener = sliderListener } slider[2] = widget.newSlider { id = "sfx", top = screenHeight \* 0.61, left = screenWidth \* 0.283, width = screenWidth \* 0.49, listener = sliderListener } --Checkbox for vibration checkboxButton = widget.newSwitch { top = screenHeight \* 0.665,--0.74, left = screenWidth \* 0.49, initialSwitchState = true, -- Set the vibration mode to "On" at the start. style = "checkbox", id = "Checkbox button", onPress = onSwitchPress, } local backBtnOptions = { -- FRAME 1: width = 48, height = 48, numFrames = 2, sheetContentWidth = 96, sheetContentHeight = 48 } --for i = 1,4 do backBtn = widget.newButton { sheet = myBackButtonSheet, id = "close\_btn", defaultFrame = 1, overFrame = 2, onRelease = onButtonEvent, width = 64, height = 64 } --button[i].alpha = 0 --end backBtn.x = math.floor(screenWidth \* 0.92) backBtn.y = math.floor(screenHeight \* 0.12) myBackButtonSheet = nil slider[2].alpha = 0 screenGroup:insert(slider[1]) screenGroup:insert(slider[2]) screenGroup:insert(checkboxButton) screenGroup:insert(backBtn) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local screenGroup = self.view print( "1: enterScene event" ) -- remove previous scene's view --storyboard.purgeScene( "scene4" ) prior\_scene = storyboard.getPrevious() storyboard.removeScene( prior\_scene ) storyboard.removeScene( "MainMenu" ) storyboard.purgeAll() end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local screenGroup = self.view button1 = nil button2 = nil button3 = nil display.remove(bg) bg = nil display.remove(backBtn) backBtn = nil for i=1,2 do display.remove(slider[i]) slider[i] = nil end display.remove(checkboxButton) checkboxButton = nil --display.remove(bg) screenGroup:removeSelf() screenGroup = nil end function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName --print( event.phase, event.keyName ) -- Print which key was pressed down/up to the log. local message = "Key '" .. event.keyName .. "' was pressed " .. event.phase print( message ) if ( "back" == keyName and phase == "up" ) then storyboard.showOverlay( "ExitScreen" ) end --Volume Control in Settings Menu if ( keyName == "up" and phase == "down" ) then --Key that is used/pressed to increase volume. local masterVolume = audio.getVolume() print( "Volume:", masterVolume ) if ( masterVolume \< 1.0 ) then masterVolume = masterVolume + 0.1 -- Increase the volume by 0.1 whenever up key is pressed. audio.setVolume( masterVolume ) -- Set the new mastervolume affter increasing the volume. end elseif ( keyName == "down" and phase == "down" ) then -- Key that is used/pressed to decrease the volume. local masterVolume = audio.getVolume() print( "Volume:", masterVolume ) if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 -- Decrease the volume by 0.1 whenever down key is pressed. audio.setVolume( masterVolume ) -- Set the new mastervolume affter decreasing the volume. end end return true --SEE NOTE BELOW end --add the key callback Runtime:addEventListener( "key", onKeyEvent ) -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) print( "((destroying scene 1's view))" ) end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene

Please tell me if you need any other file
Thanks

Bumps 

Anyone can help?

Perhaps this might help.

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Does it works with save the user preferences in the setting menu?

Such as the volume etc… 

No, this doesn’t save things in the system’s settings area.  It saves data in your apps documents director where you can set up your own settings screen in your app.  I feel customers like managing their settings while in the app rather than having to go to another app and dig for the settings.

Now OS related settings like permissions to access the Camera roll, push notifications and such are handled for you.

Rob

So…l am currently using The load save.lua to save and load y levels.
Do you have any tutorials thay can allows the user to save its settings in the game settings menu page? Because now every time I go into the settings menu and change the volume to 0 and exit the volume changes back to default value

Or shoild I say “How to link the setting.lua to my gamescene.lua”? Because from what I see the gamescene.lua and settings.lua looks like an individual.lua which has no connection between them

Perhaps this tutorial would help you bridge how to share data between modules.

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Hi @w0shi21,

Are you saving these settings locally to the device? And also storing them in variables and then reading those values when the user returns to the settings menu?

Brent

I suppose that what you said is true, but if that’s the case, am i suppose to save it in a table or?
Here my settings file

--------------------------------------------------------------------------------- -- -- scene1.lua -- --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require( "widget" ) local xMultiplier = display.contentWidth/480 local yMultiplier = display.contentHeight/320 --local gameScene = display.newGroup() local screenWidth = display.contentWidth local screenHeight = display.contentHeight local button1,button2,button3 local slider = {} --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- local button = {} local backBtn local image, text1, text2, text3, memTimer --Handles the slide event for the volume slider local function sliderListener( event ) local slider = event.target local value = event.value print( "Slider at " .. value .. "%" ) end --Handles the press event for the checkbox local function onSwitchPress( event ) -- body local switch = event.target print("Switch with ID '"..switch.id.."' is on:" ..tostring(switch.isOn)) --Enable vibration when the check box for Vibrates is checked. if (switch.isOn) then -- If the checkbox is checked, then run this code. print( "Switch is on, Vibrate the device" ) system.vibrate() -- Vibrate the device end end local function onButtonEvent(event) local btn = event.target if btn.id == "start\_btn" then local function nextScene ( event ) storyboard.gotoScene( "LevelSelect","slideLeft",600) end timer.performWithDelay( 100, nextScene) elseif btn.id == "gallery\_btn" then local function nextScene ( event ) storyboard.gotoScene( "Gallery","slideLeft",600) end timer.performWithDelay( 100, nextScene) elseif btn.id == "close\_btn" then local function nextScene ( event ) storyboard.gotoScene( "MainMenu","fade",200) end timer.performWithDelay( 100, nextScene) elseif btn.id == "button3" then print( "Green" ) print (collectgarbage("count")/1000) collectgarbage( "collect" ) return true end end -- Called when the scene's view does not exist: function scene:createScene( event ) local screenGroup = self.view collectgarbage( "collect" ) bg.x = screenWidth / 2 bg.y = screenHeight / 2 screenGroup:insert(bg) --Sliders for volume controls slider[1] = widget.newSlider { id = "bgm", top = screenHeight \* 0.46, left = screenWidth \* 0.283, width = screenWidth \* 0.49, listener = sliderListener } slider[2] = widget.newSlider { id = "sfx", top = screenHeight \* 0.61, left = screenWidth \* 0.283, width = screenWidth \* 0.49, listener = sliderListener } --Checkbox for vibration checkboxButton = widget.newSwitch { top = screenHeight \* 0.665,--0.74, left = screenWidth \* 0.49, initialSwitchState = true, -- Set the vibration mode to "On" at the start. style = "checkbox", id = "Checkbox button", onPress = onSwitchPress, } local backBtnOptions = { -- FRAME 1: width = 48, height = 48, numFrames = 2, sheetContentWidth = 96, sheetContentHeight = 48 } --for i = 1,4 do backBtn = widget.newButton { sheet = myBackButtonSheet, id = "close\_btn", defaultFrame = 1, overFrame = 2, onRelease = onButtonEvent, width = 64, height = 64 } --button[i].alpha = 0 --end backBtn.x = math.floor(screenWidth \* 0.92) backBtn.y = math.floor(screenHeight \* 0.12) myBackButtonSheet = nil slider[2].alpha = 0 screenGroup:insert(slider[1]) screenGroup:insert(slider[2]) screenGroup:insert(checkboxButton) screenGroup:insert(backBtn) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local screenGroup = self.view print( "1: enterScene event" ) -- remove previous scene's view --storyboard.purgeScene( "scene4" ) prior\_scene = storyboard.getPrevious() storyboard.removeScene( prior\_scene ) storyboard.removeScene( "MainMenu" ) storyboard.purgeAll() end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local screenGroup = self.view button1 = nil button2 = nil button3 = nil display.remove(bg) bg = nil display.remove(backBtn) backBtn = nil for i=1,2 do display.remove(slider[i]) slider[i] = nil end display.remove(checkboxButton) checkboxButton = nil --display.remove(bg) screenGroup:removeSelf() screenGroup = nil end function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName --print( event.phase, event.keyName ) -- Print which key was pressed down/up to the log. local message = "Key '" .. event.keyName .. "' was pressed " .. event.phase print( message ) if ( "back" == keyName and phase == "up" ) then storyboard.showOverlay( "ExitScreen" ) end --Volume Control in Settings Menu if ( keyName == "up" and phase == "down" ) then --Key that is used/pressed to increase volume. local masterVolume = audio.getVolume() print( "Volume:", masterVolume ) if ( masterVolume \< 1.0 ) then masterVolume = masterVolume + 0.1 -- Increase the volume by 0.1 whenever up key is pressed. audio.setVolume( masterVolume ) -- Set the new mastervolume affter increasing the volume. end elseif ( keyName == "down" and phase == "down" ) then -- Key that is used/pressed to decrease the volume. local masterVolume = audio.getVolume() print( "Volume:", masterVolume ) if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 -- Decrease the volume by 0.1 whenever down key is pressed. audio.setVolume( masterVolume ) -- Set the new mastervolume affter decreasing the volume. end end return true --SEE NOTE BELOW end --add the key callback Runtime:addEventListener( "key", onKeyEvent ) -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) print( "((destroying scene 1's view))" ) end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene

Please tell me if you need any other file
Thanks

Bumps 

Anyone can help?

Perhaps this might help.

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Does it works with save the user preferences in the setting menu?

Such as the volume etc… 

No, this doesn’t save things in the system’s settings area.  It saves data in your apps documents director where you can set up your own settings screen in your app.  I feel customers like managing their settings while in the app rather than having to go to another app and dig for the settings.

Now OS related settings like permissions to access the Camera roll, push notifications and such are handled for you.

Rob

So…l am currently using The load save.lua to save and load y levels.
Do you have any tutorials thay can allows the user to save its settings in the game settings menu page? Because now every time I go into the settings menu and change the volume to 0 and exit the volume changes back to default value

Or shoild I say “How to link the setting.lua to my gamescene.lua”? Because from what I see the gamescene.lua and settings.lua looks like an individual.lua which has no connection between them

Perhaps this tutorial would help you bridge how to share data between modules.

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/