Having now set up a few parameters to control how program MiLo works, it
would be really nice to add these into the Preferences screen, to save having
to physically alter the code each time. mannum% and nballs% are the two magic
numbers we wish to both ask the user values for and also store away in our
settings file, ready for the next time the program is launched.
The most logical place to begin is the procedure prefs:, right down at
the bottom of the code, so go there now. Add lines to the procedure as follows:
PROC prefs:
LOCAL number&
number
dINIT "Preferences"
dCHOICE sound%,"Sound","On,Off"
dCHOICE nballs%,"Number of balls to be drawn","1,2,3,4,5,6,7,8"
dLONG number&,"Maximum number on each ball?",10,80
dBUTTONS "OK",13
DIALOG
maxnum%=number&
RETURN
ENDP
Which should leave your screen looking something like this:
Right, let's unpack what we've done so far. The easy bit is
the second dCHOICE line, which simply sets nballs% to the numbered element that
the user selects from the comma-separated list. In our case, the number of each
element in the list is exactly the same as the integer in that position, i.e.
the first number is "1", the second is "2" etc. This is a neat trick for
providing a friendly dialog line for a beginner to select a smallish integer
number.
Things are not so easy for maxnum%. For starters, the possible values
are large enough that we couldn't contemplate using the same trick as for
nballs%. And OPL doesn't offer an easy way for a user to be able to change a
normal integer, unfortunately. The standard way of approaching this is to use a
temporary LOCAL variable, a so-called 'long' integer. This is a special form
of number variable which uses more memory but allows much higher numbers to be
stored inside it. And most importantly, OPL does have a dialog command
for changing the value of a long integer!
In the same way as the "%" character is tacked on the end of a normal
variable to denote it's an integer, a long integer always has a "&" after
the main name. The way we use it is to define number& as a LOCAL variable
within the relevant procedure and then immediately set it to the value of
maxnum%. We then use number& inside the DIALOG with the dLONG command.
Notice the two numbers at the end of this command. As you'll see in the OPL
manual, they represent the minimum and maximum numbers the user is allowed to
type in - in this case set by us to something sensible, like 10 and 80
respectively.
After the DIALOG, of course, we set maxnum% equal to the new value of
number& ... Easy when you know how! 8-)
Translate and run the program and go into the Preferences screen.
Experiment with the "Number of balls" line by pressing TAB and note the effect
of our dCHOICE command:
Note also the behaviour of the last line and watch what
happens if you try and enter a number less than 10 or greater than 80:
Of course, we ideally need to save the values of these two
variables in our settings file to save having to set them up each time in the
program. Find the shutdown: procedure and alter it to include:
CREATE setting$,A,sound%,nballs%,maxnum%
A.sound%=sound%
A.nballs%=nballs%
A.maxnum%=maxnum%
i.e. we're just adding two new fields to the settings database file, each called the same as the variable in the program which we want to save (for simplicity) and containing the appropriate value.
That takes care of saving the values, but we also need to
change the code at the start of MiLo which reads these numbers back. Go up near
the top of the code and change the bit that starts IF EXIST(setting$) to also include the
lines:
OPEN setting$,A,sound%,nballs%,maxnum%
sound%=A.sound%
nballs%=A.nballs%
maxnum%=A.maxnum%
So far so good, but if you translate and run now you'll just
get an error message from the program as the old settings file won't have the
necessary fields in it. So, delete the "milo.ini" file from your root or \OPD
directory and then try again. This time it should work fine! So we've now
allowed the user to configure the program how he/she likes and have stored the
configuration ready for the next time the program is run. Good stuff.
Next week we'll do so more working with OPL's graphics functions, and in a fortnight's time we'll look some more at the differences between normal and 'long' integers... See you then!
Go to next lesson | Programming index