2

I have an iOS game, I am trying to save settings (which are generally stored within an array) to file.

currently,I have the file opened and read in the openStack handler; I have the file written in the shutdown handler...

but, in the openStack handler, how do I test to see if the file has actually been created... and if it doesn't exist I want to create one and write in some default settings

What's the best way to do this?

MadPink
  • 294
  • 1
  • 10

2 Answers2

0

You can check if the file already exists and if it does not exist you can put the default values into the file, which will then be created. See below

if there is not a file "mysettings.dat" 
then put myDefaultsettings into URL "binfile:mysettings.dat"
Matthias Rebbe
  • 116
  • 1
  • 7
0

Usually, I just open and read the file. Next, I put the contents into variables. If the contents happens to be empty, then I use a default value. This makes it unnecessary to check that the file exists and, more importantly, is more compatible with future versions of your software.

on readPrefs
  put specialFolderpath("documents") & "/prefs.dat" into myPath
  put url ("binfile:" & myPath) into myPrefs
  // here, the result may contain "can't open file"
  put line 1 of myPrefs into gHighscore
  if gHighScore is empty then put 0 into gHighscore
  put line 2 of myPrefs into gLicenseKey
  if gLicenseKey is empty then put "unregistered" into gLicenseKey
end readPrefs

You could also check for the file and use a slightly different script:

on readPrefs
  put specialFolderpath("documents") & "/prefs.dat" into myPath
  if there is a file myPath then
    put url ("binfile:" & myPath) into myPrefs
    put line 1 of myPrefs into gHighscore
    put line 2 of myPrefs into gLicenseKey
  end if
  if gHighScore is empty then put 0 into gHighscore
  if gLicenseKey is empty then put "unregistered" into gLicenseKey
end readPrefs

More variations are possible, e.g. you could check the result and set default values if the file can't be opened.

Mark
  • 2,380
  • 11
  • 29
  • 49