problem with app reading a table....loadsave

Hi,

[lua]if ta.musicOn == false then
music_btnF = display.newImageRect(group,“musicOff.png”, 35, 33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
else
music_btnF = display.newImageRect(group,“musicOn.png”, 35, 33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
end

music_btn = display.newRect(group,music_btnF.x, music_btnF.y, 38,38)
music_btn.alpha = 0.1

function music_btn:touch(e)
if e.phase == “began” then
music_btnF:removeSelf()
music_btnF = nil
if ta.musicOn == true then
ta.musicOn = false
–loadsave.saveTable(t, “myTable.json”, system.DocumentsDirectory)
elseif ta.musicOn == false then
ta.musicOn = true
–loadsave.saveTable(t, “myTable.json”, system.DocumentsDirectory)
end
elseif e.phase == “ended” then
if ta.musicOn == false then
music_btnF = display.newImageRect(group,“musicOff.png”,35,33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
else
music_btnF = display.newImageRect(group,“musicOn.png”, 35,33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
end
loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)
newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)
end
return true
end
music_btn:addEventListener(“touch”, music_btn)[/lua]

as you see in these lines :

  1. if ta.musicOn == false then
  2. music_btnF = display.newImageRect(group,“musicOff.png”, 35, 33)
  3. music_btnF.x = centerX-30; music_btnF.y = centerY+99;
  4. else
  5. music_btnF = display.newImageRect(group,“musicOn.png”, 35, 33)
  6. music_btnF.x = centerX-30; music_btnF.y = centerY+99;
  7. end

if ta.musicOn == false, I want to load the specific image.

but every time I get the “else” image.

could it be caused from this…? :

[lua]ta = {}
ta.soundOn = true
ta.musicOn = true

function scene:createScene(e)
local group = self.view

newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)

    loadsave.printTable(newTable)

[/lua]

the “loadtable is fine” i mean that when I see in the console, every thing is alright, but when ta.musicOn == false, it fails to use “musicOff.png” and uses “musicOn.png”.

It looks like you’re not setting the values in the ta table with the saved values that you are retrieving from “myTable.json”.

After this line:

newTable = loadsave.loadTable("myTable.json", system.DocumentsDirectory)

you should do something like:

if newTable then --make sure that newTable is not nil --override with what's in myTable.json or leave the default values ta.soundOn = newTable.soundOn ~= nil and newTable.soundOn or ta.soundOn tal.musicOn = newTable.musicOn ~= nil and newTable.musicOn or ta.musicOn end

@Vince

I tried the things you suggested, but i didn’t work, I added the line at the “ended” of music_btn:touch function.

hammond,

You need to post a larger chunk of your code.

It’s not clear where or when you are having problems.

For instance, when your code first runs, I don’t see where you even load the ta table from loadsave or create the ta table.

Nail

Hi @xnailbender

here’s the full code of the scene :

[lua]local loadsave = require(“loadsave”)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

ta = {}
ta.soundOn = true
ta.musicOn = true

function scene:createScene(e)
local group = self.view


–loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)

newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)


mbg = display.newImageRect(group,“mbg.png”, 515, 320)
mbg.x = centerX; mbg.y = centerY; mbg.xScale = 1.11; mbg.yScale = 1.13;

title = display.newImageRect(group,“title.png”, 198, 55)
title.x = centerX; title.y = screenTop+60;

play_btn = display.newImageRect(group,“play_btn.png”, 192, 72)
play_btn.x = centerX; play_btn.y = centerY-10;

function playtouch(e)
if e.phase ==“began” then
storyboard.gotoScene(“game”,“fade”, 400)
end
end
play_btn:addEventListener(“touch”, playtouch)

 

if ta.musicOn == false then
music_btnF = display.newImageRect(group,“musicOff.png”, 35, 33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
else
music_btnF = display.newImageRect(group,“musicOn.png”, 35, 33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
end

music_btn = display.newRect(group,music_btnF.x, music_btnF.y, 38,38)
music_btn.alpha = 0.1

function music_btn:touch(e)
if e.phase == “began” then
music_btnF:removeSelf()
music_btnF = nil
if ta.musicOn == true then
ta.musicOn = false
elseif ta.musicOn == false then
ta.musicOn = true
end
elseif e.phase == “ended” then
if ta.musicOn == false then
music_btnF = display.newImageRect(group,“musicOff.png”,35,33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
else
music_btnF = display.newImageRect(group,“musicOn.png”, 35,33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
end
loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)
newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)
ta.musicOn = newTable.musicOn ~= nil and newTable.musicOn or ta.musicOn

end
return true
end
music_btn:addEventListener(“touch”, music_btn)
 

sound_btn = display.newImageRect(group,“soundOn.png”, 35, 33)
sound_btn.x = centerX+30; sound_btn.y = centerY+100;

function sound_btn:touch(e)
if e.phase == “began” then
if ta.soundOn == true then
ta.soundOn = false
else
ta.soundOn = true
end
elseif e.phase == “ended” then
loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)
newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)
end
return true
end

sound_btn:addEventListener(“touch”, sound_btn)

end
[/lua]

@hammond,

I’m thinking you want the app to load with the previously stored “musicOn” values.

I think I see the problem, when the app loads you are setting ta.musicON = true and ta.soundOn = true.

This sets both to true when the app loads.

If you want to use stored values try this at the top:

newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)

loadsave.printTable(newTable)

 

if newTable ~= nil then

    ta = newTable

else

   ta = {}

   ta.musicOn = true

   ta.soundOn = true

end

 

 Hope this helps,

Nail

@xnailbender

yes, that worked, thanks alot, this was the only piece I’m missing for my project.

It looks like you’re not setting the values in the ta table with the saved values that you are retrieving from “myTable.json”.

After this line:

newTable = loadsave.loadTable("myTable.json", system.DocumentsDirectory)

you should do something like:

if newTable then --make sure that newTable is not nil --override with what's in myTable.json or leave the default values ta.soundOn = newTable.soundOn ~= nil and newTable.soundOn or ta.soundOn tal.musicOn = newTable.musicOn ~= nil and newTable.musicOn or ta.musicOn end

@Vince

I tried the things you suggested, but i didn’t work, I added the line at the “ended” of music_btn:touch function.

hammond,

You need to post a larger chunk of your code.

It’s not clear where or when you are having problems.

For instance, when your code first runs, I don’t see where you even load the ta table from loadsave or create the ta table.

Nail

Hi @xnailbender

here’s the full code of the scene :

[lua]local loadsave = require(“loadsave”)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

ta = {}
ta.soundOn = true
ta.musicOn = true

function scene:createScene(e)
local group = self.view


–loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)

newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)


mbg = display.newImageRect(group,“mbg.png”, 515, 320)
mbg.x = centerX; mbg.y = centerY; mbg.xScale = 1.11; mbg.yScale = 1.13;

title = display.newImageRect(group,“title.png”, 198, 55)
title.x = centerX; title.y = screenTop+60;

play_btn = display.newImageRect(group,“play_btn.png”, 192, 72)
play_btn.x = centerX; play_btn.y = centerY-10;

function playtouch(e)
if e.phase ==“began” then
storyboard.gotoScene(“game”,“fade”, 400)
end
end
play_btn:addEventListener(“touch”, playtouch)

 

if ta.musicOn == false then
music_btnF = display.newImageRect(group,“musicOff.png”, 35, 33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
else
music_btnF = display.newImageRect(group,“musicOn.png”, 35, 33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
end

music_btn = display.newRect(group,music_btnF.x, music_btnF.y, 38,38)
music_btn.alpha = 0.1

function music_btn:touch(e)
if e.phase == “began” then
music_btnF:removeSelf()
music_btnF = nil
if ta.musicOn == true then
ta.musicOn = false
elseif ta.musicOn == false then
ta.musicOn = true
end
elseif e.phase == “ended” then
if ta.musicOn == false then
music_btnF = display.newImageRect(group,“musicOff.png”,35,33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
else
music_btnF = display.newImageRect(group,“musicOn.png”, 35,33)
music_btnF.x = centerX-30; music_btnF.y = centerY+99;
end
loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)
newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)
ta.musicOn = newTable.musicOn ~= nil and newTable.musicOn or ta.musicOn

end
return true
end
music_btn:addEventListener(“touch”, music_btn)
 

sound_btn = display.newImageRect(group,“soundOn.png”, 35, 33)
sound_btn.x = centerX+30; sound_btn.y = centerY+100;

function sound_btn:touch(e)
if e.phase == “began” then
if ta.soundOn == true then
ta.soundOn = false
else
ta.soundOn = true
end
elseif e.phase == “ended” then
loadsave.saveTable(ta, “myTable.json”, system.DocumentsDirectory)
newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)
loadsave.printTable(newTable)
end
return true
end

sound_btn:addEventListener(“touch”, sound_btn)

end
[/lua]

@hammond,

I’m thinking you want the app to load with the previously stored “musicOn” values.

I think I see the problem, when the app loads you are setting ta.musicON = true and ta.soundOn = true.

This sets both to true when the app loads.

If you want to use stored values try this at the top:

newTable = loadsave.loadTable(“myTable.json”, system.DocumentsDirectory)

loadsave.printTable(newTable)

 

if newTable ~= nil then

    ta = newTable

else

   ta = {}

   ta.musicOn = true

   ta.soundOn = true

end

 

 Hope this helps,

Nail

@xnailbender

yes, that worked, thanks alot, this was the only piece I’m missing for my project.