| Lesson 2: The Message Window |
|
|
Goal In this exercise you will learn how to use the Message Window to test single-line Lingo statements, and to store and retrieve values from variables. Definitions Used in this Exercise Concatenation Combining, or joining together two or more strings. Expression A chunk of code that results in a value. Expressions exist within statements. An expression might be a single variable, or it might be a formula. Statement One executable line of Lingo code. A line of code is always terminated by a carriage return (the Return key). String A string is a series of alphanumeric characters enclosed within double quotation marks. You might think of a string as a series of characters "strung" together like beads on a necklace. Strings are often used for literal expressions such as the name of a person, or the content of a message you want the user to read. Variable A storage area in the computer's memory for a value. The type or value of this data can "vary" at any time so it is called a variable. The Message Window Keyboard Shortcuts For example, try typing each of the following in the Message Window to see the results of each statement. Don't forget to hit the Return key at the end of each statement below. trace(_system.date()) You will see in most of the above examples that Director returns information, based on the command you sent. In the last example, Director computes the value of '4+8' and returns the result. You may enter more complex mathematical expressions into the Message Window and Director can display the result as well. In addition to its other uses, the Message Window is a good place to experiment with formulas, or single line Lingo statements that you want to explore before attaching those statements to Lingo objects, such as sprites. Note: The 'trace()' function used above prints a string (text) to the Message Window. The string 'trace()' prints is the result of the expression in the two parentheses ( ) that appear after the word trace. Next, we'll create a variable and see how Director stores and retrieves its value. A variable can be thought of as a storage area for a value. Try the following example one line at a time in the message window. myVariable = 10 You should see " -- 10" as the result. To understand why this happened you need to know that the equal sign operator '=' tells Lingo that you want to assign a variable a value. Lingo reads the statement as "assign the variable named 'myVariable' the value of 10." Once the variable 'myVariable' is assigned a value it will contain that value until it is assigned another value, or until it is initialized. An initialized variable usually has the value of zero '0', but zero is still considered a value. All variables contain a default value of 0, or VOID, when first created. Enter the following in the message window: myVariable = "Hello" You should see " -- "Hello"" as the result. The variable named 'myVariable' now contains another value, which is not a number, but a series of alphanumeric characters called a string. Though the string is not a numeric value it can be stored in the variable as well. The word "myVariable" is a name for a variable that we made up. We can name a variable anything we like, provided that we follow the rules regarding how variables must be named. The '=' operator assigns the value we specify to the variable we named, so when we ask the computer to show us what value is stored within that variable it gives us the result. You can make up as many variables as you like when writing Lingo, but you need to be aware of the following rules regarding variables: Variables
Variables represent stored data within the computer. Depending on how those data are used, different results may be obtained. Lingo supports five types of variables which are briefly described below. Variables may also contain more complex data in the form of an object, but that will be saved for another lesson.
Float A fractional, or non whole number value, either positive or negative. String A series of one or more alphanumeric characters. Symbols A special type of variable that is processed like a number, but is itself a form of string. Lists The equivalent to an array in many other programming languages. A list can store a series of variables of different types.
Strings A string is a series of alphanumeric characters enclosed within double quotation marks. Here's an example of a string: "Once upon a time" To assign the string to a variable you can use the '=' operator. Type the following into the Message Window: myString = "Once upon a time" The message window will echo -- "Once upon a time" on the next line. You can also concatenate, or combine strings using the ampersand "&" symbol. Type the following into the Message Window for an example. string1 = "Once upon a time" The resulting string will be: "Once upon a timethere was a frog, who was actually a prince." Notice that the words "time" and "there" do not have a space between them. To ensure the two strings look right when concatenated you will need to insert a space before the second string, or use a double ampersand "&&." Using the double ampersand, the above example would look like this: string1 = "Once upon a time" The resulting string in the message window should be: --"Once upon a time there was a frog, who was actually a prince."
In this exercise you have learned that the Message Window can be used for a variety of things, including storing values in variables, and retrieving the values stored in variables. You have also learned fundamental properties of variables and how to correctly name them in Lingo. As you use Director more consistently you will find more practical uses for the Message Window. Go through the self-test below to check your understanding of this exercise.
Self-Test View the Self-Test for Exercise 2
Set as favorite
Bookmark
Email this
Hits: 1729 Trackback(0)
Comments (0)
![]() Write comment
|


