LSL 101/Global vs Local Variables

From Second Life Wiki
Jump to: navigation, search
← Using Variables ↑̲  LSL 101  ̲↑ Variable Initialization →

A variable whose declaration occurs before the word default is called a global variable. Global variables are accessible to all event handlers in the script. Consider this example

string RegionName;    // This is a global variable
 
default
{
     state_entry()
     {
          // Store the name of the current sim for later use
 
          RegionName = llGetRegionName();
     }
 
     touch_start( integer num_detected )
     {
          // Announce the region where the script is running
 
          string WelcomePhrase;
          WelcomePhrase = "Welcome to " + RegionName  + ".";
          llSay(0, WelcomePhrase );
     }
}

The only difference between this and the previous example is that we replaced the one line

 llSay(0, "Welcome to " + RegionName  + ".");

with the three lines

 string WelcomePhrase;
 WelcomePhrase = "Welcome to " + RegionName  + ".";
 llSay(0, WelcomePhrase );

This change doesn't change the way the script behaves. But it does break up the one slightly complex line into smaller pieces. In this case, the one line wasn't complex enough to cause a problem, but scripts that involve more computation can be much easier to read when the computation is broken up into smaller pieces. To do this, variables with meaningful names can be used to hold the intermediate results.

Let's walk through the execution of the touch_start handler. The first executable statement is the variable declaration

 string WelcomePhrase;

This is called a local variable because its use is going to be restricted to this event handler. Just like the global variable RegionName, the declaration causes the sim server to reserve a chunk of memory capable of storing a string and to give that chunk of memory the name WelcomePhrase. What is different is that this declaration is executed each time the touch_start handler is executed, whereas the global declaration is only executed once.

The next line

 WelcomePhrase = "Welcome to " + RegionName  + ".";

is straightforward. It creates the string just as before, and then assigns the whole string to the variable WelcomePhrase. The third line

 llOwnerSay( WelcomePhrase );

retrieves the value stored in the variable WelcomePhrase and passes it to llOwnerSay.

Note that if we really wanted to, we could break things up into even smaller pieces. Consider the following fragment.

 string WelcomePhrase;
 WelcomePhrase = "Welcome to " + RegionName;
 WelcomePhrase = WelcomePhrase + ".";
 llOwnerSay( WelcomePhrase );

This version breaks up the two string concatenations into two separate commands. Remember that a *concatenation* is when two strings are joined with a + plus sign. Thus, the first assignment statement

 WelcomePhrase = "Welcome to " + RegionName;

results in the variable WelcomePhrase holding the string "Welcome to Ganymede" (assuming that we're executing the script in Ganymede).

The second assignment statement

 WelcomePhrase = WelcomePhrase + ".";

takes the current value of WelcomePhrase, concatenates the "." and stores the new result back in the the variable WelcomePhrase. This process of replacing the current value of a variable with a new one is called updating the variable.

Remember we said that memory was set aside for WelcomePhrase each time the touch_start handler is executed. You might think that this would slowly use up the memory that is available to the script. But this doesn't happen because at the end of the touch_start event handler, the server"un-reserves" the memory, allowing it to be used again when it is needed for some other purpose.

It is preferable to use local variables instead of global variables any time a value doesn't need to be retained once the event handler completes. (The reserving and un-reserving of local variables happens so efficiently that it is not a significant contribution to the work the sim server has to perform.)

There are other variations on local variables that we will discuss as the need arises.

Please continue the tutorial with Variable Initialization or go back to Using Variables.