Incarnadine



 info/how to:


 multimedia:

 facs 3930
 general






Introduction to Lingo Scripting using Variables

a version of this page for printing out

Topics:




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 top

A 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

  • Variable names can be almost anything. You make the name up.
  • They can't have spaces or punctuation in them.
  • Variable names can't be names already used for other things in Lingo.
  • Use letters for variable names (along with numbers, if you wish), but not numbers by themselves.
  • Make sure all your variables names start with a letter.
  • No two variable names in a project should be the same.

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 top

There are two choices:

  1. Local variables
    • A local variable is only active in the script it is in, and for the time it takes to run the script once.
    • Once the script has run once, the information in the variable is forgotten.
      This means that if the script is run twice, the variable remembers the information while it runs once, then forgets it; and then starts over, with the variable empty, getting new information and remembering only this information while the script runs the second time, then forgetting it again.
    • Local variables are common in Lingo, often helping with repeat or contingent (if… then…) processes.
    • Variables in Lingo are assumed to be local unless declared otherwise.

  2. Global variables - are essential for more complex scripting.
    • Global variables allow other scripts throughout the Director movie to use the information in the global variable.
    • The information in global variables is remembered after the script is finished running, between different scripts, between different movies, and no matter how many times you run the script.
    • Global variables must be declared, by announcing at the beginning of each script they are used in that the variable is global

    For example:
    In this script, the variables username and oldusers and usernum are all declared to be global variables:

    on mouseup
    global username, oldusers, usernum
    insert other lines of scripting here
    end

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 top

The 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 hithere
And 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 top

The 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 top


A 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 x

if x=1 then
   _movie.go("mountain")
else if x=2 then
   _movie.go("valley")
else if x=3 then
   _movie.go("forest")
else if x=4 then
  _movie.go("river")
else if x=5 then
  _movie.go("village")
end if
end



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 top

Beep 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 top

Read the notes about how to clear global variables. coming soon...


back to top