Tracking multiple users
July 20, 1998
by Pat McClellan
Dear Multimedia Handyman,
Ok, what advice you got regarding setting up a movie that can be used by several users, one that tracks where they have been within the movie and allows them to save the place. then come back later...enter their user name and carry on from where they were.
lists, fileIO and read/writing ini files??
Louis Savy
empower new media, london
Dear Louis,
Glad you asked... because this is one I've done many times. And yes, you've hit the nail on the head: lists and fileIO.
In the training CDs I produce, like you, I want to allow the user to save their position in the program -- a bookmark. Along with the bookmark, I save the user's name, employee number, date of participation, and scores on various quizzes. To accomplish this, I create a global property list, which is saved when the user quits the program. Then, next time the user logs into the program, that information is retrieved and the user can pick up where they left off. FileIO takes care of the saving and retrieving of that information.
For starters, you won't find the instructions for FileIO in the manuals. I know that FileIO comes free from Macromedia with Director, but it is an Xtra, so you'll have to treat it like one. Find the FileIO folder and print out the enclosed read me file. Those are the instructions, complete with some examples & definitions of all the commands.
Here's what must be done to save info as a text file:
1) Create a new instance of FileIO. You do this by setting a variable -- I'll use "myFile" -- equal to the instance. If you've never used Xtras or objects, this will seem strange, but it's not hard.
set myFile = new (xtra "fileio")2) Designate that the file we save will be a text file. Since Windows & Mac differ in this aspect, the code must test to see which platform you're using.
if the platform = 256 then -- Windows setFilterMask (myFile,"Text Files, *.txt") else -- Mac setFilterMask (myFile,"Text Files, TEXT") end if3) Pick a name & location for the file. You can either designate one, or use a standard Save/Open dialog box and let the user do it.
set fileName = displaySave(myFile)4) Create an empty file, which we will soon put information into. This does NOT save the file, it just creates it in RAM. (It "names" the file whatever was designated in step 2.)
createfile (myFile, fileName)5) Now that it has been created, open it. You can open it in read only (1), write only (2), or read/write mode (0).
openFile(myFile, fileName, 2)6) You're going to be writing information into the file, so we need to tell it WHERE to start (at the top).
setPosition (myFile, 2)7) Now write the information to the file. Currently, the info is stored in a global variable, in the form of a property list. You need to convert that data to a string so that it can be written to the text file. I also end it with a RETURN.
writeString (myFile, string ¬ (gThisUsersInfoList)&RETURN)8) Now that we have written the data to the file, close it.
closeFile (myFile)9) Last step is to "dispose" of the Xtra. This is important, but easy.
set myFile = 0Congratulations, you have just saved a file using FileIO. Here's the handler in its entirety. The handler is called when the user presses the Exit button.
on saveUserInfo -- initializes fileIO global gThisUsersInfoList set myFile = new (xtra "fileio") if the platform = 256 then -- Windows setFilterMask (myFile,"Text Files, *.txt") else -- Mac setFilterMask (myFile,"Text Files, TEXT") end if -- allows user to designate the pathname & filename set fileName = displaySave(myFile) createfile (myFile, fileName) -- this opens the file in write mode openFile(myFile, fileName, 2) --set position to top of file setPosition (myFile, 0) -- writes data as string ending -- in a RETURN into the text file writeString (myFile, string¬ (gThisUsersInfoList)&RETURN) closeFile (myFile) set myFile = 0 end saveUserInfoHere's the handler for retrieving that information when the user logs in next time. It gets called from a keyDownScript when the user presses Control-O or Command-O. I've commented the code, so you should be able to figure it out.
on getUserInfo global gThisUsersInfoList -- initializes fileIO set myFile = new (xtra "fileio") if the platform = 256 then -- Windows setFilterMask (myFile,"Text Files, *.txt") else -- Mac setFilterMask (myFile,"Text Files, TEXT") end if -- allows user to designate the pathname & filename set fileName = displayOpen(myFile) -- opens the file in read only mode openFile(myFile, fileName, 1) --set position to top of file setPosition (myFile, 0) -- this reads the file into memory -- (as string data, up to the first RETURN) set theFile = readLine(myFile) -- puts the string data into -- a pre-existing field member put theFile into field "theData" -- closes the file closeFile(myFile) -- disposes of this instance of fileIO XTRA set myFile = 0 -- tests to see if the data is a list if NOT listP(value(theFile)) then alert "The data in this file appears ¬ to be corrupted." else -- converts the string data -- back into a property list set gThisUsersInfoList = value(theFile) end if end getUserInfoOnce you're comfortable with what's going on in these handlers, you should explore the other commands available to you with FileIO. It's amazing just how much functionality you can add to your Director programs with the use of "Save" and "Open".
Note: For similar functionality in Shockwave, take a look at the "setPrefs ()" and "getPrefs ()" commands.
Copyright 1997-2024, Director Online. Article content copyright by respective authors.