Saving/Loadind Data - Easier way?

Hey, good to see everyone is getting on so well :slight_smile:

Big huge thanks goes out to Naomi for doing all the support that I should have been doing :slight_smile: Thanks!

If you want to avoid globals you should be able to load your boxes locally on each screen you want to access them from, just make sure to call save() before you change scenes or things will get lost.

For multiplayer, if you wanted to be extra fancy you can negate having lots of different player boxes by doing something like this:

local function createPlayer( index )  
  
 local playerBox = ice:loadBox( "player" )  
  
 local player = {}  
 player.name = "player" .. index  
 player.score = 0  
 player.hiScore = 0  
 player.medals = { 0, 0, 0 }  
 player.promotion = 0  
 player.rank = 1  
 player.bonus = 0  
 player.bullet = 1000  
  
 playerBox:store( player.name, player )  
 playerBox:save()  
  
end  

All this function does is initiate all the variables for the player and then store the player table in the singular box ( loaded locally ) under the property name of whatever the player name is.

Then have a second function for getting a player:

  
local function getPlayer( index )  
  
 local playerBox = ice:loadBox( "player" )  
  
 local playerName = "player" .. index  
  
 local player = playerBox:retrieve( playerName )  
  
 if player then  
 return player  
 else  
 -- error, no player found with this index  
 end  
  
end  
  

And a third for saving a player:

  
local function savePlayer( player )  
  
 local playerBox = ice:loadBox( "player" )  
  
 local playerName = player.name  
  
 playerBox:store( playerName, player )  
  
 playerBox:save()  
  
end  

You can then use it like so:

  
-- Create a couple of players  
createPlayer( 1 )  
createPlayer( 2 )  
  
-- Retrieve one of them  
local player1 = getPlayer( 1 )  
  
-- Change the score ( or anything else )  
player1.score = 40  
  
-- Save it back out again  
savePlayer( player1 )  
  

[import]uid: 5833 topic_id: 16634 reply_id: 67334[/import]

Yes I second that, Naomi was instrumental to my understand of ice. Thank you Graham with this new info. That’s great stuff.

Next problem, here I come:)

Mo [import]uid: 49236 topic_id: 16634 reply_id: 67343[/import]

Hey, guys, it’s so nice of you to mention me in such a nice way. Mo, thanks for your code (posted on #39). Together with what Graham posted above (#41), we can really learn to use ice to its full potential. Graham, ice is truly awesome library! Thank you for contributing it to the community.

@kilocinema, I suggest start using it in a small way first in a test project. Fooling around with it (armed with what information is already available to you on this thread as well as on ice library thread), I’m sure you’ll get a real good grip on this library quickly. (And honestly, I think a bit of pain and some struggle can actually help us leapfrog in the world of coding – so long as we don’t give up.)

Cheers,
Naomi [import]uid: 67217 topic_id: 16634 reply_id: 67352[/import]

Hey, thehivetyrant, try using storeIfHigher function (assuming you want the biggest number to be saved/stored). It’s probably the easiest route, I think. You’ll find how it is used in ice library thread.

Cheers,
Naomi

[import]uid: 67217 topic_id: 16634 reply_id: 67353[/import]

Seems if i just use myData:storeIfHigher(“Lv1Stars”, starsCollected)
It works just fine. ^-^ Sorry to bother :stuck_out_tongue:

Hi peeps.

I’ve hit a little hitch, only minor i think.
I’m trying to only store the variable into my iced value if it’s > than.
(in case they replay the level)

i have a local starsCollected = 0 at the top of the level.

And when they complete the level it saves via
myData:store(“Lv1Stars”, starsCollected)

if they played and got 3 stars and replayed and got 2 at the moment it saves the 2.

That should be clear :wink:

I tried:

if (starsCollected \> Lv1Stars) then  
 myData:store("Lv1Stars", starsCollected)  
end  

But i get an error that it’s trying to compare a number with nil.

So i added:

if (Lv1Stars ~= nil) then  
 if (starsCollected \> Lv1Stars) then  
 myData:store("Lv1Stars", starsCollected)  
 end  
end  

And made sure that Lv1Stars is 0 by adding a if nil then store 0 on the previous screen.
And print(Lv1Stars) and it confirms it’s 0.

But it just seems glide over the function and not store it.

Any ideas how to only store the data if it’s > previous score? [import]uid: 91798 topic_id: 16634 reply_id: 67351[/import]

That’s the least I could do. Thanks again Naomi for the wonderful help you provided me.

Cheers.

Mo [import]uid: 49236 topic_id: 16634 reply_id: 67828[/import]

Hey, Mo, glad to hear things are progressing nicely for you.

About your question #1, I’m sure there are many different ways of doing things, but in my game, I have an options screen (which is accessed from menu screen), and it provides the users an option to reset the game. When users decide to reset the game, the saved data is reverted to the default value. For example, 0 would be saved over the high score, and other default values are saved over other data.

EDIT: About your question #2, if I were you, I’d add another data to upgraded version (a data that doesn’t exist in older version of the game), and try retrieving it. In other words, I’d first retrieve this data (with an assumption no one has this data yet), and when it returns nil, I’d know that this user DOES NOT have the newer version installed on the device. If this person has an older version (but not the newer version), I’d try retrieving all data from the older version, and then decide if I want to save them in a specific ways for upgraded version. For example, if player1 needs additional data, I’dd add this additional data to player1 (but would not change the existing data). If there’s no need for changing the data structure, then I’d simply add the new data for player2. (Also, as noted in post #21 below, adding the version data as one of the saved data might be the best way to go. This way, it’s super easy to tell what release version the user might have installed and is playing.)

I’m as new as you are, so I may not be giving you the best advice. You may want to ask these questions on ICE forum where Graham would most likely provide you an answer he deems best suited for what you seek. (And all future users of ICE would read it too.)

Cheers, Naomi [import]uid: 67217 topic_id: 16634 reply_id: 67161[/import]