We've now given our trivial little lottery program a setting which the user can change. As you can imagine, it would quickly get frustrating if the user had to alter the setting every time the program was run, what's needed is a way of storing the value of this parameter when the program is closed down. The next time the program runs, it should load up this value again and use it as the new default for that setting.
Sounds easy enough, doesn't it? There are a few steps (as usual) needed to achieve this, which will introduce you to the concept of simple database programming (Don't Panic! - I really do mean simple!).
Find the shutdown: procedure and enter the following code before the
STOP command:
IF EXIST("milo.ini")<>0
DELETE "milo.ini"
ENDIF
CREATE "milo.ini",A,sound%
A.sound%=sound%
APPEND
CLOSE
The first line simply asks if the settings file already
exists. If it does, we delete it so that we can create a new version of it.
Although there are other ways to achieve this effect, the method above works
fine and is quite quick enough for our purposes.
The section starting CREATE opens an OPL database with the name
"A" and the parameter sound%. OPL programs can open up to four databases at
the same time, with names A,B,C and D. Each database is opened with one or more
parameters which describe to the program what sort of variables are being
stored inside. In our simple case, we're simply storing one integer. We're only
using one record as well, so you can't get much simpler than that! 8-)
Note that the sound% in the database parameter list is nothing to do
with the global variable sound% in the main program. Yes, the two do
correspond to each other and one stores the result of the other, but I could
have called it fred% or temp% and it would still have worked. What matters
is the type of information (i.e. integer, string etc) being stored. I usually
do use the same name as the variable I want to store, though, as it makes the
program easier to understand and to fix if anything goes wrong!
The A.sound%=sound% bit simply means "Set the value of sound% in the current
record of database A to the same as our global variable of the same name". The
APPEND command simply means "Save the record to disk" and the CLOSE command
of course closes the database file completely. Translate and run the program if
you like...
Series 5 users will normally find the "milo.ini" file created in their root folder, Series 3 users will find it in \OPD in their default disk.
So far, so good. We've saved sound%'s contents into a file before the
program shuts down. We now need to read the file when MiLo starts up again.
Find the WHILE 1 line near the top of the program and insert the following code
just before it:
IF EXIST("milo.ini")<>0
OPEN "milo.ini",A,sound%
sound%=A.sound%
CLOSE
ENDIF
This code is very similar to the code which saved the
variable in the first place. First we check if the settings file exists; if it
does, we open it up and read in a value for sound% from the first parameter in
the first record. And then we close the database again.
Time to test our work. Translate and run MiLo and change the sound setting. Then exit the program and start it up again. Note that the setting has been successfully remembered!
One thing to note is the order of events up at the top of the program. First we set up a default value for sound% and then we look for a settings file. If we did it the other way around, the default would replace the newly-restored settings file value! 8-) If the settings file doesn't exist (i.e. because this is the first time the program's been run, or because the user's deleted the file), the default value still stands etc.
See you next week!
Go to next lesson | Programming index