Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
RealLingo
Auto Complete
123 Flash Menu
Direct TTS (Text to speech)
Generic Countdown Timer
Opening and closing a CD-door behaviour
asUDP
Tint bitmap behaviour
Set Properties of a Flash Sprite
MP3 player and project source code
 

 

 

Article Saving Data in Shockwave

Added on 4/24/2000

 

Compatibilities:
D7 D8 Mac PC Shockwave

This item has not yet been rated

Author: BarrySwan (website)

How can I save and retrieve user data in shockwave?

Get the source
General usage:

Shockwave is designed so that you cannot do anything harmful to a user's hard-drive. As a result, the only way to save data to disk and load it back in is to use the specific commands setPref and getPref functions as follows:

setPref tPrefName, tPrefValue
tValue = getPref(tPrefName)

tPrefName can be any standard file name (although it is better to stick to 8 characters for early versions of Windows), but the extension (if not supplied, it defaults to .TXT) can only be .TXT or .HTM.

tPrefValue can only be a string. However, virtually anything in Director can be converted to a string by using the string() function. From a single variable, to a property list, an entire field-full of text or even HTML code, anything can be stored this way

The value returned from getPref will likewise always be a string. However, since you know what you saved, it is easy to convert.

For example I wish to save the value contained in gHiScore using the file 'hiscore.txt' then I would do:

setPref "hiscore.txt", string(gHiScore)

and to load it in:

gHiScore = value(getPref("hiscore.txt"))

Note how I converted gHiScore to a string before saving and converted back to a value when I loaded it in. This is important to note if you are attempting to save and load floating point values. When you convert a floating point value into a string, it is converted to use the number of decimal places used in the floatPrecision movie variable. To maintain maximum accuracy set this before you convert:

the floatPrecision = 15

If this code is part of a general routine, or you are setting the floatPrecision elsewhere in your movie, it is good practice to store the old value of the floatPrecision and revert back to it after you have finished:

tOldPrecision = the floatPrecision
the floatPrecision = 15
setPref "float.txt", string(tFloat)
the floatPrecision = tOldPrecision

Saving / loading multiple items:

If you wish to save more than one item of data, then rather than use multiple files you could, for example, place all the values in a list and save that:

setPref "savedata.txt", string(tDataList)
tDataList = value(getPref("savedata.txt"))

Another alternative would be to store the variables into individual lines in a field member and save the text of the field:

setPref "savedata.txt", member("Save data field").text
member("Save data field").text = getPref("savedata.txt")

Saving / loading as an HTM file:

The most complicated use of setPref is to save out a HTML file. The easiest way to do this is to have the HTML stored in a field:

setPref "hiscores.htm", member("Hi score page HTML").text

For the extra complicated, it is entirely possible to create the HTML page saving data the user cannot see, by embedding the data in the middle of a comment tag before saving the whole lot in one go. This is easiest if you use a field (in this example called "HTML to save") to hold the contents of what you want to save first. Assuming you have another field (in this example called "Empty HTML") which contains:



Demo saving data into a comment tag




This is the text you see when you view this as a web page.



Note that the line is the first line of the field. This means that the empty comment line is line 8 of the field.

So to save a variable called tName to a file called "Name.htm" in the hidden comment line you’d do:

member("HTML to save").text = member("Empty HTML").text
member("HTML to save").line[8] = tName
setPref "Name.htm", member("HTML to save").text

To get the value out you’d reverse the above sequence (this example uses another field "HTML to load" although you do not need two separate fields using these examples):

member("HTML to load").text = getPref("Name.htm")
tName = member("HTML to load").line[8]

Error checking:

In all cases when using getPref() it is usually safer to check the results from it. If the loading was unsuccessful, then getPref() returns VOID. For the sake of clarity I have omitted this step in my code examples above. One simple technique would be to attempt to load the pref file at the start, and if it returns VOID then create a default one immediately.

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail