Saving/Loadind Data - Easier way?

Hey, Mo, glad to be of help. And about how you missed the box:clear() function – it happens to me too. I try my best, but sometimes things I read don’t quite stick around. As long as we keep at it, though, I’m sure we’ll retain more goodies in our brain cells (and we’ll become good at retrieving them too).

Cheers,
Naomi

P.S., about the question #2, I might even add the version data. This way, it’s super easy to tell what release version the user might have installed and is playing. [import]uid: 67217 topic_id: 16634 reply_id: 67182[/import]

I SOOOOOO needed all this ‘Ice’ info.

I’m having a hard time because of the lack of official documentation.
I love this thread, I’m gonna chomp down hard on it later! [import]uid: 91798 topic_id: 16634 reply_id: 67187[/import]

Hey, thehivetyrant, I’d say you will get all this ICE information (plus more nuggets) if you read through the threads posted on http://developer.anscamobile.com/code/ice

I also added this quick-start post for newbies like me:
http://developer.anscamobile.com/code/ice#comment-62153

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

Hey, glad to see Ice is getting more use. I do intend to put up some proper documentation on my site soon it’s just finding the time is tricky. [import]uid: 5833 topic_id: 16634 reply_id: 67211[/import]

Hey, Graham, thanks for the note. I’m super grateful that you made ICE available for us, and if you get around to add documentation, that would be awesome too (but I do understand if you don’t have time.)

Naomi [import]uid: 67217 topic_id: 16634 reply_id: 67216[/import]

Hello again.

Yes THANK YOU so much for this library. Like I said it rocks!

Ok, I think I talked too fast when I said I fixed my problem above…

This code

[lua]if box3 ~= nil then
       
        local box3 = IceBox:new( “player” )
        box3:store( “score”, 0 )
        box3:store( “hiScore”,100 )
        box3:store( “medals”, {0,50,0} )
        box3:store( “promotion”,0 )
        box3:store( “rank”, 1 )
        box3:store( “bonus”,0 )
        box3:store( “bullet”,1000 )    
        box3:save()
end[/lua]

(in my main.lua) does not work on the iPhone? It was fine on the simulator. On the device, the app simply quit when switch to my game screen from the menu screen).

BUT I if comment the if box3 ~= nil line like

[lua]-- if box3 ~= nil then
       
        local box3 = IceBox:new( “player” )
        box3:store( “score”, 0 )
        box3:store( “hiScore”,100 )
        box3:store( “medals”, {0,50,0} )
        box3:store( “promotion”,0 )
        box3:store( “rank”, 1 )
        box3:store( “bonus”,0 )
        box3:store( “bullet”,1000 )    
        box3:save()
– end[/lua]

Then the app run normally but of course always re-start with the default values from the main.lua

So I am back one step!

Any idea why checking if a box is nil would crash the app at runtime ( but not the simulator). I am at lost! Maybe I should not check for box3 being nil but in this case how do I keep the app for reloading the default values at each app re-start ( which of course defeat the reason to save the game status in the first place)

By the way also, does anybody what the name of the file(s) saved in the device? Would it be box3 in my case?

Thanks for any pointers.

Mo
EDIT: I read some more and I can see that the file as .ice so it all good on that. BUT

Unfortunately I still having problem with the saving part and the crash on the device. It is hard to diagnose because it works on the simulator. Maybe some more info are needed.

I have the above code in my main.lua ( box3…). In the other modules where I need save/load I use something like:

Local hiScore
Local my Player = ice:loadBox( “player”). – at the top my module
Then in the iniVars ( I am using director) I have something like this:

hiScore = myPlayer:retrieve(“hiScore”)

Before the module quit back to the main menu I saved the new hiScore like this:

myPlayer:save()

[import]uid: 49236 topic_id: 16634 reply_id: 67226[/import]

Hey, Mo, I don’t use ICE the way you do, so I’m sorry but I can’t help you with your latest problem. I only save one .ice file, and therefore, I don’t have multiple of .ice files that I need to access. I use for-loop statements to go through data, and so long as I understand where I put things, it’s really easy for me to access them. I guess I just don’t do anything too fancy (meaning, I try keeping my data table straight forward, rather than nesting them, which I think is what you are trying to do?)

Anyhow, I hope you’ll figure this one out. (And maybe Graham, or someone who understands ICE in depth, will come by and help you out…)

Good luck.

Naomi

EDIT: Just in case it could help you, here’s how I retrieve my high score table:
[lua] local highScore = {};
for numLoop=1,totalNumberOfLevels do
local dataName = “highScore[” … numLoop … “]”
highScore[numLoop] = data:retrieve( dataName );
end
for numLoop = 1,totalNumberOfLevels do
if (highScore[numLoop] == nil) then
highScore[numLoop] = 0;
end
end[/lua] [import]uid: 67217 topic_id: 16634 reply_id: 67247[/import]

Thank you so much Naomi for the code and the info. I could use only one ice box but I have the feeling it is not my problem (it maybe worth only having one ice box just for testing).

No I think my problem is the way I initialize my ice boxes. I just saw on the other ice post that if a box do not exist then an empty box is created and the ice return that. This is mean it is not returning nil as I thought. So my test

" if box3 ~= nil then…end " does not make much sense (even so it seems to work on the simulator). Of course I am not sure what an empty box look like? Maybe I can try to retrieve one element of the box3 like hiScore and if it nil then will setup the default box info and then save it. That may work since an empty box will of course would not have an element called hiScore.

Would you mind sharing what you have on your main.lua related to ice and your data box? That could help me see how the code you show above works in coordination with your main.lua.

In any event I cannot thank you enough for your patience with me. Thank you.

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

Good morning, Mo (I’m in California). Here’s what I have on my main.lua that relates to ICE:

[lua]require(“ice”)
data = nil;
data = ice:loadBox( “data” )[/lua]

That is all. This generates data.ice and saves it in the document folder. I don’t do any initializing the way you do. Only initializing that I do is equivalent to how I initialize high score in the post #27 above. It is used in my levels menu where I display high score of each level. Same is true with isLocked (true or false) for each level, and other data displayed/needed on that screen.

I thought about your problem as I went to bed, and I agree that having more than one ice box shouldn’t cause the crash. Maybe it crashes because you might be doing things that conflicts with how ICE library is meant to be used.

About the code working on simulator but not on device, it can happen. I actually dealt with a case last night where my game worked perfectly fine on simulator but when I installed it on device, one aspect of my new game level became super slow for no apparent reason. After attempting the process of elimination, I finally fixed it by specifying a set of hard numbers for x, y coordinate for a set of objects (rather than positioning the objects relative to the positions of other set of objects.) It was a strange one for me.

Anyhow, I hope you’ll sort out your case very soon.

Naomi [import]uid: 67217 topic_id: 16634 reply_id: 67297[/import]

Hey, a function that people may wish to use for initiating things in main.lua ( or anywhere else ) is “storeIfNew()”

So you can do things like:

require( "ice" )  
  
data = ice:loadBox( "data" )  
data:storeIfNew( "volume", 0.5 )  

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

Good morning Naomi!

Ok, I am starting to understand how you use ICE in your code. Please let me know if you see any wrong:

1- you have a global variable called data and you load an ice box also data in it.

2- in your module, you setup a local variable say highscore (in your case a table ) and retrieve the ice box data into it.

3- then you check if the highscore (local) is nil then you set to zero. If not you do nothing and so keep whatever was in the ice box data (highscore element) is retrieve.

You’re right, I must be doing something that ice do not like and I am confused about how to set the default values.

I will think about what I am doing given your GREAT info and report here.

Thank you Naomi!

Mo.

Ps: would it not be better to make your variable data in your main.lua local? And then in your modules retrieve the ice box into another local (highscore in your case) that way you are using the ice file system as way to access game settings across modules without using a global variable…wait maybe that is my problem trying to be fancy and all :). [import]uid: 49236 topic_id: 16634 reply_id: 67310[/import]

Cool, thanks, Graham! That’s a good function to know. You might want to add this storeIfNew function on the top of the ICE page where you show a couple of sample codes. It would really help newbies. Although I don’t feel I need this function to fully utilize ice, I think understanding how ice initializes data is what every newbie will need to grasp properly before feeling more comfortable with this library.

Thanks again!

Naomi

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

Good call! Adding now, thanks! [import]uid: 5833 topic_id: 16634 reply_id: 67312[/import]

WOW! I just saw the storeIfNew feature. I agree with Naomi, this need to be added to the ICE post when you get a chance) This maybe a solution to my current problem. I will look into it and report here. Another one that I use (and Noami do not seem to use as far as I can see) is “IceBox:new( “data” )”. Which I assume that it would create a new ice box if none exist. Maybe that also part of my problem since I use that in my main.lua?

Anyway I will try to experiment with all these info (thanks guys) and see what I come up with. My feeling (as Naomi says) is that once I understand the settings of the ice boxes, ICE will turn out to be a child play to use! I am almost there…

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

Hey, Mo, I read through your 1, 2 and 3, and yes, that’s how I handle data. And I would save over retrieved data when I want to change saved data (like when user reach higher score than already saved high score) – but I’d save over only a single data, not all data in the table, by the way.

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

There’s a party going on in here, i want in ^-^.

I just recently got Ice to save the amount of stars a user collects within a level to save within memory.

(And even when system is turned off, but thats not good for in-progress testing, so i commented it out :wink: )

I was struggling a day or two ago, Using Naomi’s ‘newbie quick guide’ given in the comments section of your Ice page is what got me understanding.

Questions i had when i was trying to figure out were:

-What needed to go into main.lua.
-What needed to be global.
-How to Create a icebox variable (‘highscore’)
(since it seemed you didn’t have to pre-create it like you do with variables)

Naomi’s guide really helped me.
Not to say other’s inputs didn’t but that guide just cleared up questions i had.

Thats my perspective ^-^ [import]uid: 91798 topic_id: 16634 reply_id: 67315[/import]

Hey, thehivetyrant, good to hear my little newbie quick guide helped! I’ve been helped before (and I was sooo super duper happy when I was helped), and I’m just giving back what I can.

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

WELCOME to the party thehivetyrant! Yes I agree, Naomi has been tremendous help. @Naomi, thank you for the confirmation it helps me put my around the issues.

I will get back here and report what I am able to figure out.

Thanks guys.

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

GOT IT! The pieces of code below work both on the simulator and the iphone.

Here it is and maybe someone will benefit from it. I tried to comment it so to make it easier:

[lua]------ in main.lua-------------
require( “ice” )

settings = ice:loadBox ( “settings” )

settings:storeIfNew(“version”, 1.0)
settings:storeIfNew(“musicOn”, true)
settings:storeIfNew(“fxOn”, true)
settings:storeIfNew(“gameMode”, 1)
settings:storeIfNew(“gameLevel”, 3)
settings:storeIfNew(“isGameSaved”, false )
settings:storeIfNew(“gameCounter”, 30000)
settings:store(“gameTimer”, {10000,20000,30000})
player1 = ice:loadBox ( “player1” )

player1:storeIfNew(“name”, “player1”)
player1:storeIfNew(“score”, 0)
player1:storeIfNew(“hiScore”,0 )
player1:storeIfNew(“medals”, {0,0,0} )
player1:storeIfNew(“promotion”,0)
player1:storeIfNew(“rank”, 1)
player1:storeIfNew(“bonus”,0)
player1:storeIfNew(“bullet”,1000)



---- in module X (on top of module X)…here module X is my main game screen (but I used the ----- same thing in options screen for instance)

local hiScore
local musicOn
local fxOn
local gameLevel
local gameMode
local gameCounter = { }
— then in the initVars section (at the bottom of the module X if using director…) I retrieve all the ----- variables from memory (remenber that load from disk in main.lua)

hiScore = player1:retrieve( “hiScore”)
hiScoreText.text =string.format ( “%08d”, hiScore )

musicOn = settings:retrieve( “musicOn”)
fxOn = settings:retrieve( “fxOn”)
gameLevel = settings:retrieve( “gameLevel”)
gameMode = settings:retrieve( “gameMode”)
gameCounter = settings:retrieve( “gameCounter”)

– reload the gameTimer that depends on the game level the user selected in the Options Screen ---- (another module)

local temp = settings:retrieve( “gameTimer”)
gameTimer[gameLevel] = temp[gameLevel]
print("game timing = "…gameTimer[gameLevel])
----------- Every time something change while in module X like a new high score then I do this to ----save the new high score (for instance) to memory.

player1:store(“hiScore”,highScore )

– Finally when I am about to exit module X (and go back to my main menu module) then I simply save to disk like this:

player1:save()
settings:save()

---------------------------------THAT’S IT !!!-------------------------------------[/lua]

The only thing I do not like is that fact is that the ice boxes settings and player1 are global variables (not sure if that really cause less performance but when you play with lua, everybody “hate” globals and so now I “hate” globals too:)

In any event I want to specially thank you Naomi for sticking with me all these last few days. I REALLY appreciated it. Of course thanks to Graham for coming up with ICE in the first place (AND for storeIfNew!) for When you understand it, it really child play to add a powerful load/save feature to your game.

In any event, I will see if I can remove the use of globals in the code above since ice box contains the data to accessed between modules as well as between app re-start. OR maybe i will just leave it alone if it does not interfer with the game performance/memory size…

THANKS to all.

Mo

ps: You noticed I used player1 as an ice box. The reason is that i am hoping to add local multiplayer mode so I will think I will just need to add a player2 ice box to the mix…
[import]uid: 49236 topic_id: 16634 reply_id: 67332[/import]

I see the amazing things you can do with Ice, but I still don’t really get how to use it. It would be nice if someone could write up a whole tutorial or something so that I know the difference between methods, and what I need to update them and for them to work. [import]uid: 103624 topic_id: 16634 reply_id: 67333[/import]