[ice] Create a simple example to save/load data to different places

Hi people
I am using ice library to create my save/load system, but i need to create some like it:

  • enter on the game and go to settings menu and stop the music, clicking on the music button.
  • and then go to the gameplay and pause the game, and will appear some buttons, including the music button, and it should be with the image of music off.

i tried to create some like that, and create this:

external\_file.lua:  
   
local data = ice:newBox( "database")  
data:store( "music", "off" )  
   
   
gameplay.lua  
   
local data = ice:loadBox( "database")  
local game = data:retrieve( "music")  
data:store( "music", "on" )  
data:save()  
print(game)  

but it dont works great, because he only save the new value of music if i press 2 times on the button, and should be only necessary to click one time.
if anyone can help me with this problem it will be great for me.

Thanks :slight_smile: [import]uid: 26056 topic_id: 20448 reply_id: 320448[/import]

Not sure if I understand the problem correctly, but you’d at least need to add data:save() to line 5.

Otherwise external_file.lua doesn’t ask Ice to save the variable.

You need to have the button code shown here or it’s impossible to say why user needs to press it twice for the data to be saved. [import]uid: 10416 topic_id: 20448 reply_id: 80123[/import]

you are right, and with your simples help i saw other things that i needed to change and now works great :slight_smile:

Thank you very much :slight_smile: [import]uid: 26056 topic_id: 20448 reply_id: 80172[/import]

Good to know that you got it to work :slight_smile: [import]uid: 10416 topic_id: 20448 reply_id: 80181[/import]

Only one last problem, i am trying to do this:
enter on the game for the first time, and the music is running, and then i click on the music button to change the variable to off, and save it, but always that i restart the program it start “on” and the music is running, really need to fix it, because it is very used on the game:

external_data.lua:

local settings = ice:newBox( "settings")  
settings:store( "music", "on" )  

menu.lua

  
local settings = ice:loadBox( "settings" )  
  
settings:store( "music", "on" )  
settings:save()-- i know that here is the problem but if i delete it, the music value is always nil when the user start the game.  
  
local music = settings:retrieve("music")  
print(music)  
-- the result is on  
local touch = function( event )  
 settings:store( "music", "off" )  
 settings:save()  
end   
  
local music\_button = ui.newButton{  
 defaultSrc = "..",  
 defaultX = 55,  
 defaultY = 55,  
 overSrc = "..",  
 overX = 45,  
 overY = 45,  
 onRelease = touch  
}  
  

Thanks [import]uid: 26056 topic_id: 20448 reply_id: 80190[/import]

If I understand the issue correctly then rather than using settings:store( "music", "on" ) when you first load the app, try changing it to this settings:storeIfNew( "music", "on" ) [import]uid: 5833 topic_id: 20448 reply_id: 80194[/import]

Yeh, is really it that i needed, thank you very much for the help, it was great for me :slight_smile: [import]uid: 26056 topic_id: 20448 reply_id: 80223[/import]

Glad I could help! [import]uid: 5833 topic_id: 20448 reply_id: 80237[/import]

Hi, i am haveving a little issues again with the save/load, i have this code:

main_menu.lua:

local settings = ice:loadBox( "settings" )  
  
local va = settings:retrieve("music")  
print(va)  
--only to see the variable when i start the program, if it changed or now when i tap the music settings buttons  

settings.lua:

local music\_off\_touch = function( event )  
local settings = ice:loadBox( "settings" )  
local music = settings:retrieve("music")  
settings:storeIfNew( "music", "on" )  
settings:save()   
  
music\_off\_button = ui.newButton{  
 defaultSrc = "",  
 defaultX = 67,  
 defaultY = 67,  
 overSrc = "",  
 overX = 67,  
 overY = 67,  
 onRelease = music\_off\_touch  
}  
local music\_on\_touch = function( event )  
local settings = ice:loadBox( "settings" )  
local music = settings:retrieve("music")  
settings:storeIfNew( "music", "off" )  
settings:save()  
end   
  
music\_on\_button = ui.newButton{  
 defaultSrc = "",  
 defaultX = 67,  
 defaultY = 67,  
 overSrc = "",  
 overX = 67,  
 overY = 67,  
 onRelease = music\_on\_touch  
}  
  

data.lua

local settings = ice:newBox( "settings")  

i need to, when i tap the music buttons, it change the music variable to off and on and save it on data because when i start again the game it will be saved. with this code i tap on musics buttons and i only have the result “on”, dont change to “off”, maybe i am using the settings:storeIfNew badly

Can you help me with this problem?

Thanks [import]uid: 26056 topic_id: 20448 reply_id: 80801[/import]