Data storage

Hi,

I’ve tried the sample code in the CoronaSDK for saving data and it works perfectly on the simulator, but when I export my project to a build using the same code, my iPhone does’nt seem to locate the text file.

I’m using the SaveTable sample with “local filePath = system.pathForFile( “data.txt”, system.DocumentsDirectory )”

Anything special I need to do when building for this to work?

Thanks! [import]uid: 32882 topic_id: 18919 reply_id: 318919[/import]

Hey there,

You may actually find it easier to use either the Ice library or Jon Beebe’s Games class.

Is that an option or do you want to keep working from the SaveTable sample? Last I checked the sample as is ran fine.

Peach :slight_smile: [import]uid: 52491 topic_id: 18919 reply_id: 72897[/import]

Hi again!

Thanks for the tip, I’ll check that out if I can’t crack this nut!

Here’s the code I use:

  
local filePath = system.pathForFile("highscore.txt", system.DocumentsDirectory);  
  
 -- SAVE DATA  
  
 function saveData()  
 if file then  
 local file = io.open(filePath, "w");  
  
 file:write(\_score);  
 print("saving is done!");  
  
 highScore = \_score;  
  
 io.close(file);  
 else  
 print ("no file found")  
 end  
 end  
  
 -- LOAD DATA  
  
 function loadData()   
 local file = io.open( filePath, "r" )  
  
 if file then  
  
 -- Read file contents into a string  
 highScore = file:read( "\*a" );  
 highScore = tonumber(highScore);  
  
 io.close( file ) -- important!  
  
 else  
 print ("no file found")  
 end  
 end  
  

where I call the loadData and saveData at according places. It works on the simulator and the txt is saved, but when I build it and try it on my iPhone4, It doesn’t seem to save/locate the file. :confused: [import]uid: 32882 topic_id: 18919 reply_id: 72918[/import]

Hey there,

Try putting;
[lua]local file = io.open(filePath, “w”);[/lua]

Before [lua]if file then[/lua].

Let me know how that goes :slight_smile:

Peach

PS - Else “FileDemo” sample code runs fine, just tested. [import]uid: 52491 topic_id: 18919 reply_id: 73018[/import]

That solved it, thanks Peach! [import]uid: 32882 topic_id: 18919 reply_id: 73112[/import]

Excellent!

Peach :slight_smile: [import]uid: 52491 topic_id: 18919 reply_id: 73146[/import]