Its not difficult to check in advance if the file is there using the command getFileExists(), but for convenience, ease of use and to save doing it multiple times, it makes sense to create a dedicate function to load files of each type.
First up is a function to load Images, as always with functions, this goes after the do / loop section
function SafeLoadImage( thisFileName$ )
if getFileExists( thisFileName$ )
thisImageRef = loadImage( thisFileName$ )
else
thisImageRef = 0
endif
endfunction thisImageRef
Since I skimmed over functions a little before, I'll go through in more detail now how functions work.
The first line of the function definition always starts with the word function, this tells AGK that you are defining a function.
It is followed by the function name - in this case SafeLoadImage(), with any parameters that it requires in brackets - here just the one thisFileName$.
Parameters are local variables which will be used within the function itself, at the start of the function the variable will have in it whatever was passed from the program. So the main program may use the line
ImageRef = SafeLoadImage( "Image.png" )
and when the function starts, the variable thisFilename$ will have in it Image.png ready to use.
The $ symbol at the end of the variable tells AGK that this is a string variable so will contain a string of text. Strings are defined in the program surrounded by quotes, but the quotes are not part of the string.
As a general rule, I tend to start local variable names with the word this to show they are local, but this is not a rule and any variable name can be used, provided it is not the same as a variable defined as global elsewhere.
Within the function is an if/else/endif loop, which checks to see if the filename stored in the local variable exists, if so the first loop is used, if not, the second is used.
The first loop then loads the image file as normal, storing the returned image reference in another local variable called thisImageRef. The second loop simply sets this variable to zero.
So if the file existed, the image is loaded and the image reference is in thisImageRef, if it did not exist, then thisImageRef contains zero.
The last line always starts with endfunction which can be on its own - if the function does not return a value, or is followed by the name of the variable containing the value to be passed back to the main program - in this case the image reference stored in thisImageRef.
Back in the main program, the line.
ImageRef = SafeLoadImage( "Image.png" )
Indicates that the result of the function ( the image reference ) is stored in the variable ImageRef for use later.
The function does not need to know where the result will be stored and the main program does not need to know the variables used in the function, the two things are separate and independent.
When the function exits, all the local variables used within the function are forgotten.
To incorporate this into our project, everywhere we previously used LoadImage(), we now change to SafeLoadImage() and everything will work as before.
So;
panel.Image = loadImage( "panel.jpg" )
simply becomes
panel.Image = SafeLoadImage( "panel.jpg" )
and
imageRef = loadImage( "finger.png" )
becomes
imageRef = SafeloadImage( "finger.png" )
We now repeat this for sound loading with the function SafeLoadSound();
function SafeLoadSound( thisFileName$ )
if getFileExists( thisFileName$ )
thisSoundRef = loadSound( thisFileName$ )
else
thisSoundRef = 0
endif
endfunction thisSoundRef
And change the call
panel.sound = loadSound("panel.wav")
to
panel.sound = SafeLoadSound("panel.wav")
We also need to change the following line in the ToggleTrayOpen() function;
playSound( panelSound() )
to
if panelSound() > 0 then playSound( panelSound() )
So that the sound is only played if the sound was actually loaded, in which case panelSound() will return a reference to the sound rather than zero
No comments:
Post a Comment