Generating Messages
December 11, 1997
by Pat McClellan
Dear Multimedia Handyman
I want to generate a custom message based on the input of users of my program. What is the best approach for storing the user's input and then generating the message?
Signed,
"Enter Name Here"
Dear ENH,
You have a couple of choices. The first approach involves storing the data in the editable fields where the user inputs data, and then extracting data from there for your custom message. The drawback here is that if your user changes movies within your program, the data in the field member could be lost. I prefer to take a different approach which involves a global variable property list to hold all the data. Start by initializing the variable as a property list. (NOTE: only do this before the user enters data, otherwise your data will be wiped out.)
on startMovie global gUserData set gUserData = [:] end
Now, let's say that you have editable fields displayed onstage. Give the fields names which reflect the data that they will collect, such as "firstname", "lastname", "idnumber", etc. We'll use those three for this example, but you can have as many as you want.
I generally put a button on the "log in" screen so that the user can enter the data, go back and change it, then press the button to make it official. On the mouseUp script for the button, I call the following handler which grabs the data out of the fields and saves it in the list. (You'll notice that I extracted "line 1" of the fields. This avoids saving any carriage returns the user might have entered.)
on getUserInput global gUserData setAProp gUserData, #firstName, ¬ line 1 of field "firstname" setAProp gUserData, #lastName, ¬ line 1 of field "lastname" setAProp gUserData, #idNumber, ¬ line 1 of field "idnumber" end
I recommend doing some filtering and checking to make sure that the user input is valid -- for example, that no numbers were entered in the name, or that the id number is a positive integer in the correct format. You can also filter out TABs and RETURNs in the user input. I generally use a keyDownScript to do this filtering.
Let's assume that all of the user entered data is correct. Now, gUserData should look like [#firstname: "Jane", #lastName: "Doe", #idNumber: "1234"]. Since it's a global variable, this data will be maintained until the user quits the program, or if you alter it in any way through Lingo. You can also add to this list later in the program. For example, if the user goes through a quiz, when you tabulate the score (I called this variable theScore) you can add that to the list like this:
addProp gUserData, #quizScore, theScore
Now, let's get the user data out of the list & display it on the stage. We'll also print it so that Jane Doe can prove to her boss that she completed the quiz.
At the end of the program, I'll assume that you have a field on the stage called Goodbye Message. During authoring, this field can be empty, although I recommend putting some text in there to hold the formatting info for the field. Call the handler createMessage before you get to the frame where the message is displayed. This handler uses many of the text concatonation symbols that Director recognizes. For example, you can insert a variable into a string or link two strings together by using the ampersand (&) character. If you want a space between the link, you use a double ampersand (&&).
on createMessage global gUserData set first = getAProp (gUserData, #firstName) set score = getAProp (gUserData, #quizScore) set message = "" -- creates a temporary variable -- where we'll build the message put "Congratulations" && first ¬ & "!" & RETURN into message put "You scored" && score & "% on ¬ the quiz." & RETURN after message put "Press the PRINT button if you ¬ would like a record of your ¬ participation." after message put message into field "goodbye message" end
To print out the results, you'll need to have another field member called printout. This field never appears on the stage. Again, this handler takes the data out of the global property list and plugs that data into strings. For clarity, I temporarily hold the data in a local variable, although this isn't necessary; you can include the getAProp syntax within your string concatonation.
on createPrintout global gUserData set first = getAProp (gUserData, #firstName) set last = getAProp (gUserData, #lastName) set name = first && last set score = getAProp (gUserData, #score) set idnumber = getAProp (gUserData, #idNumber) set message = "" -- creates a temporary variable -- where we'll build the message put "USER RECORD" & RETURN & ¬ RETURN into message put "Name:" && name & RETURN after message put "ID Number:" && idnumber & ¬ RETURN after message put "Course: Quality Training 101" & ¬ RETURN after message put "Date:" && the long date & ¬ RETURN after message put "Quiz Score:" && score & "%" after message put message into field "printout" set the fontStyle of line 1 of ¬ field "printout" to "bold" print member "printout" end
Your printed output will look something like this:
USER RECORD
Name: Jane Dow
ID Number: 1234
Course: Quality Training 101
Date: Monday, December 22, 1997
Quiz Score: 92%
Note that this printout capability uses Print-o-matic Lite Xtra which comes with Director. By using the command print member 'whatever', the font will print as a font and retain the style setting that we applied (bold for the first line.)
Copyright 1997-2024, Director Online. Article content copyright by respective authors.