Ice

Hey,

I am trying use Ice (http://developer.anscamobile.com/code/ice) in my game. I’ve seen the score being saved demo but that is not the only thing i am trying to accomplish. I’m trying to use Ice to display the score on different director screens. On the demo it shows how to save the score but not how to display it on different screens. Can any help in making something this work? I just don’t know how to start, i have seen the storing and retrieving part but i am really confused. Can any one demonstrate how I can display the score on other screens using Ice?

Thanks so much. [import]uid: 23689 topic_id: 18792 reply_id: 318792[/import]

Hi Michale, I wonder if this could help. It’s a quick start note I put together when I first started using Ice:

http://developer.anscamobile.com/code/ice#comment-62153

If you are storing and saving the highscore data properly, you should be able to retrieve the same highscore data from any module you are in (i.e., from any screen you are in).

If you’ve figured out how to store and retrieve, but if you never used save() function, you probably want to do that before you leave the module where you store and retrieve the data. Until you call the save function, I don’t think the data is actually saved/written to the device (and unless it is saved, when you exit your game, I believe all data you stored will simply disappear.)

It might be worth your while to read through all the posts here: http://developer.anscamobile.com/code/ice

Lots of questions are answered there, and I imagine you’ll find most of your questions addressed there. Also, Graham is super nice and responsive. You can always ask him the questions there too. And, perhaps, if it seems like a recurring stumbling block for some, Graham might even consider adding a section on the top that is dedicated to newbies so that newbies like us can sort out how-to right away.

Good luck.

Naomi

[import]uid: 67217 topic_id: 18792 reply_id: 72341[/import]

Thanks a lot Naomi!
But I’ve been playing around with it for about an hour now and still can make it work!

Im a fairly new to corona and am constantly getting confused, especially the idea of accessing different information on different screens. I first started with globals then i heard globals are bad, I then found Ice. I’ve been looking for a solid way of doing this, if it is not too much to ask… Do you mind giving me an example of what to do if you were in my situation? What I have is a main screen and a menu screen. On my main screen I have my score which is a set score, then on menu screen I want to display it. If it does not give you any trouble can you please demonstrate what code I should have on my main screen and what I should have on my menu screen?

Thanks a lot, I am trying to find a solution to this and this looks like the best one.

  • Michael [import]uid: 23689 topic_id: 18792 reply_id: 72357[/import]

Sorry for not getting back to you earlier. I tried exactly what you have gave me, not sure why it does not work. [import]uid: 23689 topic_id: 18792 reply_id: 73142[/import]

Hi Michael, here’s a quickie. When you are on menu screen, this code should print “setScore = 12345” on terminal. Also, after you run this, click on Colona Simulator > File > Show Project Sandbox. It will show you where the myData is saved. And if you open myData.ice using some simple text editor, you can see how your data is being saved. From there, you just play around with ice (and remember to read the ice thread carefully) and in no time, you’ll know enough to do whole loads of wonderful thing with it.

Naomi

[lua]---------------------------
– in main.lua

require(“ice”)
myData = nil

– load previously saved data
– EDIT: if no data has been saved, it will create the myData.ice where data will be saved
myData = ice:loadBox( “myData” )

– try and retrieve the fixed score
local setScore = myData:retrieve( “mySetScore” )
if (setScore == nil) then
setScore = 12345 – this is the fixed score you want to use
myData:store( “mySetScore”, setScore )
end
myData:save() – this will save the data to device


– in menu.lua

local setScore = myData:retrieve( “mySetScore” )
print("setScore = " … setScore)[/lua]

Edit: I placed myData:save() function outside the if-statement on purpose, just in case you want to save more than one data. I think it’s best to use save function once after all required data are stored properly (rather than calling save function over and over every time you store some new data.)

Edit #2: Instead of (setScore == nil), you can also use storeIfNew function – but I started using ice before I learned of storeIfNew function, and I feel I don’t need it. But it’s probably cleaner to use storeIfNew function (see ice thread under code exchange for further info.) [import]uid: 67217 topic_id: 18792 reply_id: 72365[/import]

Hi Michael, maybe you need to post your code. I don’t know where it isn’t working, and what you might be really trying to do…

What does it say on terminal when you print("setScore = " … setScore) in menu.lua??

Naomi [import]uid: 67217 topic_id: 18792 reply_id: 73169[/import]

What you gave me to add for main.lua is in Ice2.lua, menu.lua is in Ice3.lua. I’ve been changing some of it but every time I tap on “Press” in Ice2.lua it says failed to execute new (params) function on ‘Ice3’. I guess something is wrong in Ice3.lua, I gave you exactly what I have below.

Thanks Again!

In Terminal:

Director ERROR: Failed to execute new( params ) function on ‘Ice3’.

…michaela/Desktop/GrahamRanson-Ice-fe1d297 4/Ice3.lua:8: attempt to index global ‘myData’ (a nil value)

[code]
–Ice2.lua :
module(…, package.seeall)

function new()

local iceGroup = display.newGroup();

require(“ice”)
myData = nil

– load previously saved data
– EDIT: if no data has been saved, it will create the myData.ice where data will be saved
myData = ice:loadBox( “myData” )

– try and retrieve the fixed score
local setScore = myData:retrieve( “mySetScore” )
if (setScore == nil) then
setScore = 12345 – this is the fixed score you want to use
myData:store( “mySetScore”, setScore )
end
myData:save() – this will save the data to device

Press = display.newText( “Press”, 0, 0, “Helvetica”, 30 )
Press.x = 100
Press.y = 100
iceGroup:insert(Press);

function ther4(event)
director:changeScene( “Ice3”, “fade” )
end
Press:addEventListener(“tap”, ther4);
return iceGroup;

end

–Ice3.lua:

module(…, package.seeall)

function new()

local ice2Group = display.newGroup();

local setScore = myData:retrieve( “mySetScore” )
print("setScore = " … setScore)

return ice2Group;

end

[import]uid: 23689 topic_id: 18792 reply_id: 73437[/import]

What you gave me to add for main.lua is in Ice2.lua, menu.lua is in Ice3.lua. I’ve been changing some of it but every time I tap on “Press” in Ice2.lua it says failed to execute new (params) function on ‘Ice3’. I guess something is wrong in Ice3.lua, I gave you exactly what I have below.

Thanks Again!

In Terminal:

Director ERROR: Failed to execute new( params ) function on ‘Ice3’.

…michaela/Desktop/GrahamRanson-Ice-fe1d297 4/Ice3.lua:8: attempt to index global ‘myData’ (a nil value)

[code]
–Ice2.lua :
module(…, package.seeall)

function new()

local iceGroup = display.newGroup();

require(“ice”)
myData = nil

– load previously saved data
– EDIT: if no data has been saved, it will create the myData.ice where data will be saved
myData = ice:loadBox( “myData” )

– try and retrieve the fixed score
local setScore = myData:retrieve( “mySetScore” )
if (setScore == nil) then
setScore = 12345 – this is the fixed score you want to use
myData:store( “mySetScore”, setScore )
end
myData:save() – this will save the data to device

Press = display.newText( “Press”, 0, 0, “Helvetica”, 30 )
Press.x = 100
Press.y = 100
iceGroup:insert(Press);

function ther4(event)
director:changeScene( “Ice3”, “fade” )
end
Press:addEventListener(“tap”, ther4);
return iceGroup;

end

–Ice3.lua:

module(…, package.seeall)

function new()

local ice2Group = display.newGroup();

local setScore = myData:retrieve( “mySetScore” )
print("setScore = " … setScore)

return ice2Group;

end

[import]uid: 23689 topic_id: 18792 reply_id: 73438[/import]

Hi Michael, you need to put some of the key elements back to main.lua – if you absolutely want/need to keep them outside of main.lua, I am unable to help further. I’d say, in that case, you’d be better off seeking help from Graham (who created this library). Anyhow, see below how I would modify your code. (Not tested but it should work.)

Naomi

[lua]-- main.lua
require(“ice”)
myData = nil

– load previously saved data
– EDIT: if no data has been saved, it will create the myData.ice where data will be saved
myData = ice:loadBox( “myData” )
–Ice.lua :

module(…, package.seeall)

function new()

local iceGroup = display.newGroup();

– try and retrieve the fixed score
local setScore = myData:retrieve( “mySetScore” )
if (setScore == nil) then
setScore = 12345 – this is the fixed score you want to use
myData:store( “mySetScore”, setScore )
end
myData:save() – this will save the data to device

Press = display.newText( “Press”, 0, 0, “Helvetica”, 30 )
Press.x = 100
Press.y = 100
iceGroup:insert(Press);

function ther4(event)
director:changeScene( “Ice2”, “fade” )
end
Press:addEventListener(“tap”, ther4);

return iceGroup;

end

–Ice2.lua:

module(…, package.seeall)

function new()

local ice2Group = display.newGroup();

local setScore = myData:retrieve( “mySetScore” )
print("setScore = " … setScore) – this should print “12345” to terminal

return ice2Group;

end[/lua] [import]uid: 67217 topic_id: 18792 reply_id: 73441[/import]

I LOVE YOU! Thanks so much Naomi! It worked!

One more question. So when I change “setScore” numbers and reload the simulator it stays the same number. I saw something about clearing the data, do you know what I could do to do that? So what i mean is that instead of 12345 for setScore i put 54321 and it stayed 12345. So I was wondering what I could do to clear it every time so the score can change.

THANKS AGAIN. YOUR AWESOME. [import]uid: 23689 topic_id: 18792 reply_id: 73448[/import]

Also one thing that i noticed is that when I erased “myData:save()” Ice.lua it does not change anything. I’m trying to use “myData:clear()”, I’m adding it in difference positions but it doesn’t seem to do anything. [import]uid: 23689 topic_id: 18792 reply_id: 73449[/import]

Hi Michael, I’m glad I could help.

About myData:save() function, you need to store the data first and then use the save function while in the same module.

So, if you are in Ice1.lua file, and if you store some data, you need to call save function before you leave Ice1.lua to ensure the data is saved to myData – storing data in Ice1.lua and moving on to Ice2.lua and then calling the save function won’t save the data stored while in Ice1.lua.

About myData:clear() function, it will clear out all data from myData. Try using myData:clear() function and check the myData.ice file in the sandbox. You’ll see all stored data are gone.

So… don’t use clear function unless you really want to wipe clean. Instead, when you want to change the value of stored/saved data, use store function to store a value and then call save function before leaving the module.

Naomi [import]uid: 67217 topic_id: 18792 reply_id: 73452[/import]

I’m not sure you know what I mean. What I meant was that when you tap on Press and it displays 12345, in the sand box the number is stored. So when I change the number to let’s say 54321 the number does not change unless I erase the data in the sandbox. So I was wondering If i could erase the data in the sandbox using some code so every time the score can update it self. Let’s say i have a game and the score is constantly changing, i need the sandbox to erase itself so that i can add the new score. Because at the moment it is just storing the number 12345 and I can’t change the number, because it is stored in the sandbox. Do you know what I can do to fix this? That is why i brought of the myData:clear(), but it does not work so what can I do?

Besides the point I recently have made a timer counting up, but the score is not the number the counter is at once I tap on Press and go to the Ice2.lua, it is the number the score starts at which is 0. What I did is make a make a counter timer, i set it at 0 increasing by one each second. The problem is that when I tap on Press and go to Ice.lua it does not show the current number, it shows 0. I want it to show the current number it is once I tap on Press. Do you know what I mean? Let’s say the timer has gone up to 8 and I tap on press, the score that it shows me is O, the starting number not the current number.

What I did differently was I made the timer, and once you tap on press it brings you to ice2.lua. On Ice2.lua it is suppose to display the current score, but it shows 0. I added text so now it is not using terminal.

Thanks a lot for your help, I really appreciate it. I’m sorry for all this, i’m really a newbie.

[code]

– main.lua
require(“ice”)
myData = nil

– load previously saved data
– EDIT: if no data has been saved, it will create the myData.ice where data will be saved
myData = ice:loadBox( “myData” )

–Ice.lua :

module(…, package.seeall)

function new()

local iceGroup = display.newGroup();

– try and retrieve the fixed score
local setScore = myData:retrieve( “mySetScore” )
if (setScore == nil) then
setScore = 0-- this is the fixed score you want to use
myData:store( “mySetScore”, setScore )
end
myData:save() – this will save the data to device

local scoreText = display.newText( "Score: " … setScore, 20, 0,nil, 20)
scoreText:setTextColor(255,255,255)

local function getScore() – increments score value every time it is called
setScore = setScore + 1
scoreText.text = "Score: " … setScore
end

timer.performWithDelay(1000, getScore, 0)

Press = display.newText( “Press”, 0, 0, “Helvetica”, 30 )
Press.x = 100
Press.y = 100
iceGroup:insert(Press);

function ther4(event)
director:changeScene( “Ice2”, “fade” )
end
Press:addEventListener(“tap”, ther4);

return iceGroup;

end

Ice2.lua:

–Ice2.lua:

module(…, package.seeall)

function new()

local ice2Group = display.newGroup();

local setScore = myData:retrieve( “mySetScore” )
local scoreText = display.newText( "Score: " … setScore, 20, 0,nil, 20)
scoreText:setTextColor(255,255,255)

return ice2Group;

end [import]uid: 23689 topic_id: 18792 reply_id: 73894[/import]

Hi, MichaelAssadi, just got back from 2-weeks vacation. I hope you’ve sorted out the problem you were having with Ice library.

Naomi [import]uid: 67217 topic_id: 18792 reply_id: 76695[/import]

Hello Naomi,

Thanks for getting back to me and helping me with this problem. Yes, i have sorted out the problem. Thanks again for your responses.

  • Michael [import]uid: 23689 topic_id: 18792 reply_id: 76719[/import]

Hey, Michael, glad to hear it. Happy New Years!

Naomi [import]uid: 67217 topic_id: 18792 reply_id: 76723[/import]

Same to you! [import]uid: 23689 topic_id: 18792 reply_id: 76737[/import]