The Personal Touch

Setting user preferences in Shockwave

By Bob Schmitt
March 14, 1997

Lingo — Macromedia Director's authoring language — lets you create a personalized experience in Shockwave. With the Net-specific Lingo setPref and getPref, you can retain specific information about individual users, such as the high score in a game or the user's name. Using this information on subsequent visits can be a valuable technique in personalizing your site. SetPref and getPref allow you to write a text file, containing whatever information you choose, to the user's hard drive and read this information back into the Shockwave movie when the user visits your site again. Because the prefs are written to the user's disk, the information is persistent days or even months later.

An excellent example of using preferences in Shockwave is demonstrated in a game called Jazzman from The Article 19 Group. Jazzman uses the pref commands to keep track of the individual's personal high score. As the user plays Jazzman, the score is continually updated (Figure 1). When the player uses up his last Jazzman life, the Game Over screen pops up (Figure 2), showing the current score and high score totals. This high score is recorded to the player's disk in a Shockwave preferences file.



Figure 1. The high score is displayed during
play so the player can see how his score
compares. Click the image for more detail.



Figure 2. The Game Over screen.
Click the image for more detail.

Reading the High Score

On the start of the movie, a custom handler called checkmyprefs is called (below). This handler first initializes a global variable called gprefsfilename. This variable has been set to the file name of the prefs file that the game will use. This file is called a19_jazz. We'll see how the movie uses this variable in a moment. The next line demonstrates an excellent tip for Shockwave authoring.



Author, Author

Commands such as setPref and getPref are very difficult to test during authoring because Net-specific Lingo is understood only by the Shockwave plug-in and not by Director itself. However, the runmode Lingo command allows you to test whether the movie is in author mode (meaning, in Director) or not, and generate two separate results based on whether you are or not.

If the movie is not in author mode, the next line uses getPref to set a variable called mypref to the text contained in the pref file a19_jazz. Remember, the variable gprefsfilename was set to equal a19_jazz. Next the handler uses the voidP Lingo command to test whether or not the variable mypref contains any text. This checks to see if there is any text in a19_jazz. If it's empty (meaning this is the first time this user has played the game) the variable mypref is void. In this case, the a19_jazz file is written to the user's drive and given a default high score of 500 points. The text field called defaultresult contains this default score. The handler then starts over, by calling itself. This ensures that mypref will not be void the next time through the handler, and will jump to the else line of the if...then statement. The next line sets the text of the field called prefsresult to the text contained in mypref. This is the field on the stage that displays the high score to the user.

If the movie is in the author mode, the field prefsresult is set to the default high score contained in the field defaultresult.

Saving the High Score

When the game reaches the Game Over frame, another custom handler called setmyprefs is set into action (Figure 4). The first line of this script calls the global variable that contained the name of the pref file (a19_jazz). Next the runmode command is used again to make sure the game is not in author mode. If not in author mode, the handler uses the setPref Lingo command to set the file a19_jazz (gprefsfilename) to the text in the field prefsresult. This field has been updated with the high score during the course of game play.



As you can see, this one simple example shows the potential and power to maximize the user's experience by writing out personal preferences to the hard drive of each individual that comes to your site.