GGData problem

Im using a tutorial from Jay on mastering corona sdk about using widgets on an option page to toggle sound on and off. Im using GGData and have followed all the steps in the video but am having a issue where the music isn’t toggling. Sound effects are toggling on and off perfectly well but I am guessing that it is because sfx doesn’t play until the game starts playing, where as music plays on menu screen.

I have also noticed that when I toggle music checkbox to off, and restart the simulator, it works fine and doesn’t load any music unless i turn it on. Whats going on? I really am confused. 

Ill do my best to post relevant code up now however, if I miss something then please tell me and i will post it up. 

Ps: storyboard has been required in all scenes, I just haven’t included it in the code below.

Main.lua 

local GGData = require("GGData") local prefs = GGData:new("preferences") musicIsPlaying = true sfxIsPlaying = true local function loadPrefs() prefs:load() musicIsPlaying = prefs.musicIsPlaying sfxIsPlaying = prefs.sfxIsPlaying end function savePrefs() prefs.musicIsPlaying = musicIsPlaying prefs.sfxIsPlaying = sfxIsPlaying prefs:save() end audio.reserveChannels(1) sndChanMusic = 1 sndJump = audio.loadSound("sounds/sfx.wav") sndMusic = audio.loadStream("sounds/POL-air-sharks-short.wav") function playSFX(audioHandle, opt) local options = opt or {} local loopNum = options.loop or 0 local channel = options.channel or 0 local chanUsed = nil if sfxIsPlaying then chanUsed = audio.play( audioHandle, { channel=channel, loops=loopNum } ) end return chanUsed end function playMusic() if musicIsPlaying then audio.play( sndMusic, {channel = sndChanMusic, loops=-1 } ) audio.setVolume ( .15 ,{ channel=sndChanMusic } ) end end loadPrefs()

Menu.lua

function scene:enterScene( event ) playMusic()

Setting.lua

local musicSwitch = widget.newSwitch { top = display.contentCenterY - 70, left = display.contentCenterX + 40, style = "checkbox", initialSwitchState = musicIsPlaying, onRelease = function() musicIsPlaying = not musicIsPlaying end } group:insert(musicSwitch) local sfxSwitch = widget.newSwitch { top = display.contentCenterY - 15, left = display.contentCenterX +40, style = "checkbox", initialSwitchState = sfxIsPlaying, onRelease = function() sfxIsPlaying = not sfxIsPlaying end } group:insert(sfxSwitch) function goBack(event) if event.phase == "began" then savePrefs() storyboard.gotoScene("menu", "fade", 500) end end

Your music won’t turn off when you switch the toggle because you haven’t called audio.stop.

Hi nick_sherman, thanks for the reply, should I add the audio.stop to onRelease function? also Ive noticed that when I print the value of musicIsPlaying it shows Nil in terminal. is that a problem or is it supposed to behave like that?

It will be nil as when you first load the values, you haven’t saved anything yet. You can use this behaviour to check whether the user has run the app before, and if not set up the default values and save them. You don’t need to call prefs:load() either.

[lua]

local prefs = GGData:new(“preferences”)

       

musicIsPlaying = true

sfxIsPlaying = true

       

local function loadPrefs()

      musicIsPlaying = prefs.musicIsPlaying

      sfxIsPlaying = prefs.sfxIsPlaying

end

function savePrefs()

      prefs.musicIsPlaying = musicIsPlaying

      prefs.sfxIsPlaying = sfxIsPlaying

      prefs:save()

end

if prefs.firstRun == nil then

      prefs.firstRun = false

      prefs.musicIsPlaying = true

      prefs.sfxIsPlaying = true

      prefs:save()

end

loadPrefs()

[/lua]

It should not be nil…   you will get nil, if it has no value assigned to it at the time you run the print statement.  So, try running the print statement right after you run loadPrefs()  …   The other reason you are likely to get a nil, is if in the ‘print’ line, you have not spelled the variable correctly…

print( musicisPlaying )  will be nil

print( musicIsPlaying ) should print   true or false

case must be exact and spelling exact!

I do it all the time, typing to fast, and then I get a nil when it should not, it most of the time for me, it is just that i did a typo when typing that line of code.

Good Luck!

Thats great thanks for that, the sound toggle works properly.

Ill try out the nil problem now. Thanks. 

EDIT: Just checked my print code and noticed that I wrote the variable name incorrectly, Its working good now, really appreciate the help guys. 

Thanks 

Hey,

thanks for this post!  I had the same kinda problem and this helped solving it. Kinda. I mean, I have not reached the step in which the button actually effects the on/off toggling of music and sounds. So far I set up the switch in my option menu and by using ggdata the program saves the state of the button properly. I am checking on how the button operates using “print” to see if musicIsPlaying return true or false consistently with the state of the button.

And it does…up until I ‘force-test’ the button by rapidly clicking on it, when I do that -sometimes- the button gets set on a ‘on’ position but musicIsPlaying returns ‘false’ and viceversa…

Now, is this necessarily an errror that resides in my code (which is quite identical to dip’s) or is it possible that quickly and repeatedly changing the state of the button induce the button library to get ‘confused’??

I am testing this on the simulator, on a mac, a trackpad for clicking… can someone dissolve my doubts before I go on at the risk a potential error gets magnified?? Thank you.

here’s the option screen module, if you notice the ‘print check’ as well as the save preferences function are called on hiding the optionscreen scene:

local widget = require  ("widget") local GGData = require("GGData" ) local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event )     local sceneGroup = self.view     local  function gotoPlay ()    composer.gotoScene ("Play")  end  local function gotoStory ()    composer.gotoScene ("Intros")  end local function Musicwitch ()   Mswitch =  widget.newSwitch({style="checkbox",  initialSwitchState=musicIsPlaying,   onRelease = function () musicIsPlaying = not musicIsPlaying end })   sceneGroup:insert (Mswitch) end local function backButton ()   back = display.newText( "- BACK", 50, 0, "Helvetica", 20 )   back.x=350   back.y=150   back:addEventListener ( "tap", gotoPlay )   sceneGroup:insert(back) end local function storyButton ()   storybut = display.newText( "Story", 50, 0, "Helvetica", 20 )   storybut.x = 250   storybut.y=50   storybut:addEventListener ( "tap", gotoStory )   sceneGroup:insert(storybut) end backButton () Musicwitch () storyButton ()      end function scene:show( event )     local sceneGroup = self.view     local phase = event.phase     if ( phase == "will" ) then     elseif ( phase == "did" ) then     end end function scene:hide( event )     local sceneGroup = self.view     local phase = event.phase     if ( phase == "will" ) then     elseif ( phase == "did" ) then       savePrefs()       print (musicIsPlaying)     end end function scene:destroy( event )     local sceneGroup = self.view end -- ------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

and here’s the main.lua file.  These are the only two modules in which is present all that goes with setting and saving the ‘musicIsPlaying’ boolean value:

display.setStatusBar(display.HiddenStatusBar) local composer = require( "composer" ) local GGData = require("GGData" ) local prefs = GGData:new("AudioPreferences") musicIsPlaying = true local function loadPrefs ()     musicIsPlaying = prefs.musicIsPlaying end function savePrefs()         prefs.musicIsPlaying = musicIsPlaying     prefs:save()     end loadPrefs () composer.gotoScene( "Intros" )

Your music won’t turn off when you switch the toggle because you haven’t called audio.stop.

Hi nick_sherman, thanks for the reply, should I add the audio.stop to onRelease function? also Ive noticed that when I print the value of musicIsPlaying it shows Nil in terminal. is that a problem or is it supposed to behave like that?

It will be nil as when you first load the values, you haven’t saved anything yet. You can use this behaviour to check whether the user has run the app before, and if not set up the default values and save them. You don’t need to call prefs:load() either.

[lua]

local prefs = GGData:new(“preferences”)

       

musicIsPlaying = true

sfxIsPlaying = true

       

local function loadPrefs()

      musicIsPlaying = prefs.musicIsPlaying

      sfxIsPlaying = prefs.sfxIsPlaying

end

function savePrefs()

      prefs.musicIsPlaying = musicIsPlaying

      prefs.sfxIsPlaying = sfxIsPlaying

      prefs:save()

end

if prefs.firstRun == nil then

      prefs.firstRun = false

      prefs.musicIsPlaying = true

      prefs.sfxIsPlaying = true

      prefs:save()

end

loadPrefs()

[/lua]

It should not be nil…   you will get nil, if it has no value assigned to it at the time you run the print statement.  So, try running the print statement right after you run loadPrefs()  …   The other reason you are likely to get a nil, is if in the ‘print’ line, you have not spelled the variable correctly…

print( musicisPlaying )  will be nil

print( musicIsPlaying ) should print   true or false

case must be exact and spelling exact!

I do it all the time, typing to fast, and then I get a nil when it should not, it most of the time for me, it is just that i did a typo when typing that line of code.

Good Luck!

Thats great thanks for that, the sound toggle works properly.

Ill try out the nil problem now. Thanks. 

EDIT: Just checked my print code and noticed that I wrote the variable name incorrectly, Its working good now, really appreciate the help guys. 

Thanks 

Hey,

thanks for this post!  I had the same kinda problem and this helped solving it. Kinda. I mean, I have not reached the step in which the button actually effects the on/off toggling of music and sounds. So far I set up the switch in my option menu and by using ggdata the program saves the state of the button properly. I am checking on how the button operates using “print” to see if musicIsPlaying return true or false consistently with the state of the button.

And it does…up until I ‘force-test’ the button by rapidly clicking on it, when I do that -sometimes- the button gets set on a ‘on’ position but musicIsPlaying returns ‘false’ and viceversa…

Now, is this necessarily an errror that resides in my code (which is quite identical to dip’s) or is it possible that quickly and repeatedly changing the state of the button induce the button library to get ‘confused’??

I am testing this on the simulator, on a mac, a trackpad for clicking… can someone dissolve my doubts before I go on at the risk a potential error gets magnified?? Thank you.

here’s the option screen module, if you notice the ‘print check’ as well as the save preferences function are called on hiding the optionscreen scene:

local widget = require  ("widget") local GGData = require("GGData" ) local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event )     local sceneGroup = self.view     local  function gotoPlay ()    composer.gotoScene ("Play")  end  local function gotoStory ()    composer.gotoScene ("Intros")  end local function Musicwitch ()   Mswitch =  widget.newSwitch({style="checkbox",  initialSwitchState=musicIsPlaying,   onRelease = function () musicIsPlaying = not musicIsPlaying end })   sceneGroup:insert (Mswitch) end local function backButton ()   back = display.newText( "- BACK", 50, 0, "Helvetica", 20 )   back.x=350   back.y=150   back:addEventListener ( "tap", gotoPlay )   sceneGroup:insert(back) end local function storyButton ()   storybut = display.newText( "Story", 50, 0, "Helvetica", 20 )   storybut.x = 250   storybut.y=50   storybut:addEventListener ( "tap", gotoStory )   sceneGroup:insert(storybut) end backButton () Musicwitch () storyButton ()      end function scene:show( event )     local sceneGroup = self.view     local phase = event.phase     if ( phase == "will" ) then     elseif ( phase == "did" ) then     end end function scene:hide( event )     local sceneGroup = self.view     local phase = event.phase     if ( phase == "will" ) then     elseif ( phase == "did" ) then       savePrefs()       print (musicIsPlaying)     end end function scene:destroy( event )     local sceneGroup = self.view end -- ------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

and here’s the main.lua file.  These are the only two modules in which is present all that goes with setting and saving the ‘musicIsPlaying’ boolean value:

display.setStatusBar(display.HiddenStatusBar) local composer = require( "composer" ) local GGData = require("GGData" ) local prefs = GGData:new("AudioPreferences") musicIsPlaying = true local function loadPrefs ()     musicIsPlaying = prefs.musicIsPlaying end function savePrefs()         prefs.musicIsPlaying = musicIsPlaying     prefs:save()     end loadPrefs () composer.gotoScene( "Intros" )