
info/how to: |
The following information assumes that you have used Director, know what frames, sprites, and channels are, and are familiar with some basic navigational Lingo scripting, and possibly some other scripting commands.
What is a Variable? back to topA variable is a container for information that will change. It can allow Lingo to remember and use information for short or long periods in a Director movie.In the first variables we will look at and use, you can only have one piece of information in a variable at a time. However, certain uses of variables allow you to store many pieces of information in one variable (see lists and list commands for more on this). Variable Names: back to top
In recent versions of Director, colour coding is evident in script windows, which helps you to distinguish Lingo commands from other things by colour. This is useful, because you can check the colour of the names of variables to make sure they are not already Director commands. Variable names should show up black in the scripting window. If your variable name turns any colour other than black (it could turn green or blue, for instance) you need to change the variable name because it is already the name of a Lingo term or command.
Variable Scope: Local vs. Global back to topThere are two choices:
When you quit Director, information remembered in variables is lost. This is useful for clearing the information out of global variables, though there are other ways to do the same thing (see my notes on clearing global variables). However, it is also possible to write variable information to the hard disk so the info in the variables can be saved even though Director may be quit (it can be very useful to be able to do this in the context of certain advanced projects). Scripting: back to topThe following line of actual script puts the word junk into the variable hithere.set hithere="junk"Another way to do the exact same thing (put the word junk into the variable hithere)is to say: put "junk" into hithereAnd yet another way to do the exact same thing (put the word junk into the variable hithere)is to say: hithere = "junk"(Note, the above is the newest Lingo syntax, which you should try to use.) Strings (words) vs. Values (numbers) back to topThe above scripting examples show a word being put into a variable. When you put word or sentence information (also called a string) into a variable, you have to put "quotes" around the information to let Director know that it is a word/sentence/etc, and not a new variable.When you put a number into a variable and you expect to do mathematical calculations with the number/variable, do not use quotes. Numerical information of this kind is called a value.
Alternately, if you want a number to be used as part of a word or phrase, you will either have to put it in "quotes" or convert it to a string in some other way. See working with strings and values for more clarification and information on any of these issues
Local Variable Examples: back to topA local variable example using randomness and an if statement to make decisions: This example and those below assume that Director is looping (with Hold on Current, or a "go to the frame" command in a frame script, etc.) over a frame intersecting the sprite this script is attatched to. This example script is attached to a sprite, and is triggered by a mouse click (understood by Lingo as on mouseup). This script uses the lingo random command to randomly choose a number to put into x so that the movie randomly goes to one of five markers that are set up further on in the Director movie. on mouseup put random(5) into xend Don's example of remembering a choice in a variable, and displaying it, along with other text, in a pop-up window: This script would be attached to a sprite that happens to use a cast member that is an image of a cow. When you click on the cow, the computer uses the variable animalchoice to remember the word "cow," and then beeps and opens a pop-up window (with an OK button) that says, "You clicked on a cow." on mouseup put "cow" into animalchoice alert "You clicked on a " & animalchoice end The alert command is useful. Whatever you put inside the quotes after it comes up as text in a pop-up window (which beeps once & has an OK button for the user to press before the alert window will go away). In the example script above, something else is also going on. The text inside the quotes (after the alert command) is being put together with the information in the variable to give a single text result. This is called concatenating. To do this, you just add a & after the end quotes, and then the name of the variable. If you use two && it adds a space after the quote text and then adds the contents of the variable. on mouseup put "cow" into animalchoice alert "Your" && animalchoice && "is ugly!" end In the above example, more things are being concatenated. The word "Your" has been concatenated with the variable content ("cow" in this case), which is then being added to more text at the end (in this case "is ugly!") So the alert window in this example ends up saying, "Your cow is ugly!" A local variable example using randomness and a repeat loop to assign six sprites to random locations on a 200x100 stage: This example script attaches to a frame in which there are 6 sprites, and is triggered by the movie entering and exiting the frame (understood by Lingo as on enterframe and on exitframe). When the movie enters the frame, six different sprites, in channels 10-15, are assigned random horizontal and vertical positions. When the movie exits the frame, it is told to re-enter the frame. The effect this has is to make the 6 sprites randomly change position rapidly (as the movie enters and exits the frame). on enterFrame me repeat with x=10 to 15 sprite(x).locH = random(200) sprite(x).locV = random(100) end repeat end on exitframe me _movie.go(_movie.frame) end The above script uses the variable x to stand in for the channel number each sprite is in (a sprite is often referred to by channel number in lingo). Because the sprites are in channels 10 through 15, a repeat loop is being used to set the variable x to the number 10 (in the first repetition, and, in later repetitions to 11, 12, 13, 14, 15) so that lingo can set the horizontal (loch) and vertical (locv) location of that sprite. This repeat loop allows very efficient setting of the locations of the sprites in channels 10-15. The stage being 200 pixels across and 100 pixels down, and the random assignment of location, means that the 6 sprites may not end up completely on the stage. This is also dependent on the location of each sprite's registration mark and the horizontal and vertical dimensions of the sprite. If you wanted the sprites to always be wholly on the stage, and never to extend off the stage, you would (at least) want to set the registration mark of all the sprites to their top left corner, and randomly choose locations from smaller numbers than the actual width and height of the stage. Global Variable Examples: back to topBeep the same number of times you click, using a global variable:This script goes on a sprite, and is triggered by a mouse click. This script makes it so that the sprite will beep the same number of times you click on it (because the script remembers and counts each time you click using the global variable counterupper, and it beeps the same number of times as counterupper). on mouseup global counterupper set counterupper = counterupper+1 beep counterupper end The first time you click, it will add 1 to counterupper (which starts at 0 when you open the Director movie) and then beep once. The beep command obviously makes the computer beep; if you put a number after the beep command, or a variable that contains a number, then the computer will beep that number of times. The second time you click, the script adds 1 to counterupper (which already has 1 in it), making 2; then it beeps the same number of times (2). The third time you click, it will beep three times, etc. Something to watch for with this script is that it will always keep counting up as long as you click on it. This is partly because counterupper keeps remembering the number it currently holds while the movie is open in Director, whether or not you stop the movie from playing and then start it again. To get counterupper to start at 0 again, you could have a script attached to another sprite on the stage, and have that sprite put 0 into counterupper when you click on it. Alternately, you can quit Director and start it up again, or use the Watcher window, or the Message window to reset the value of counterupper to 0. If you want to learn how to use these windows, check the clearing variables page, or refer to the course Director text. Don's slightly more complex version of the above, with if statement: This example script goes on a sprite. This script makes it so that the sprite will beep the first ten times you click on it (because the script remembers and counts each time you click, using the global variable counterupper). The script beeps the same number of times that you have clicked, so that the tenth time you click, it will beep 10 times. The eleventh time you click, and each time after, no beeping will occur, because an if statement checks to make sure counterupper is 10 or under before it beeps. on mouseup global counterupper if counterupper<10 then set counterupper = counterupper+1 beep counterupper end if end A slightly more involved if statement variation of the above: This example script goes on a sprite. This script makes it so that the sprite will beep every time you click on it (because the script remembers and counts each time you click, using the global variable counterupper). The script beeps the same number of times that you have clicked, but once you have clicked ten times, counterupper is set back to 1 and proceeds to beep starting at 1 again. Then, as you keep clicking on it, it counts back up (beeping whatever number of times it is at) until it gets to ten, is reset to 1 again, and starts over. This is all made possible by a more complex if statement that has 2 parts. The first part checks to make sure counterupper is 9 or under before it adds 1 to counterupper. The second part of the if statement (starting with else) handles all instances when counterupper is larger than 9, and sets counterupper to 1. After the end of the if statement (indicated by end if) the script executes the beep command. on mouseup global counterupper if counterupper<10 then set counterupper = counterupper+1 else set counterupper=1 end if beep counterupper end Further Variable Resources and Help: back to topRead the notes about how to clear global variables. coming soon...back to top |