Difference between revisions of "LSL 101/Global vs Local Variables"

From Second Life Wiki
Jump to: navigation, search
Line 74: Line 74:
 
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.)
 
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.  But nows a good time to talk about one in particular, [[Event Handler Parameters|event handler parameters]].
+
There are other variations on local variables that we will discuss as the need arises.  But now is a good time to talk about [[Event Handler Parameters|event handler parameters]], which are another form of local variables.

Revision as of 21:42, 30 May 2009

← Variables ↑̲  LSL 101  ̲↑ Event Handler Parameters →

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

<lsl>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  + ".";
         llOwnerSay( WelcomePhrase );
    }

}</lsl>

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

<lsl> llOwnerSay( "Welcome to " + RegionName + ".");</lsl>

with the three lines

<lsl> string WelcomePhrase;

WelcomePhrase = "Welcome to " + RegionName  + ".";
llOwnerSay( WelcomePhrase );</lsl>

This change doesn't change the result of the script. 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

<lsl> string WelcomePhrase;</lsl>

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

<lsl> WelcomePhrase = "Welcome to " + RegionName + ".";</lsl>

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

<lsl> llOwnerSay( WelcomePhrase );</lsl>

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.

<lsl> string WelcomePhrase;

WelcomePhrase = "Welcome to " + RegionName;
WelcomePhrase = WelcomePhrase + ".";
llOwnerSay( WelcomePhrase );</lsl>

This version breaks up the two string concatenations into two separate commands. The first assignment statement

<lsl> WelcomePhrase = "Welcome to " + RegionName;</lsl>

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

The second assignment statement

<lsl> WelcomePhrase = WelcomePhrase + ".";</lsl>

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 sim "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. But now is a good time to talk about event handler parameters, which are another form of local variables.