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

From Second Life Wiki
Jump to navigation Jump to search
m (dejargoning)
m (typo fixes & headers & footers)
Line 1: Line 1:
[[Category:LSL 101]]
[[Category:LSL 101]]
{{NavNextPrev|prev=Variables|next=Variable Initialization}}
{{NavNextPrev|prev=Variables|next=Functions}}
{{LSL Wikibook Index}}
{{LSL Wikibook Index}}


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


'''Please continue the tutorial with [[LSL 101/Variable Initialization|Variable Initialization]] or go back to [[LSL 101/Using Variables | Using Variables]].'''
'''Please continue the tutorial with [[LSL 101/Variable Initialization|Variable Initialization]], skip that and go to [[LSL 101/Functions | Functions]] or go back to [[LSL 101/Using Variables | Using Variables]].'''

Revision as of 21:55, 7 July 2012

← Variables ↑̲  LSL 101  ̲↑ Functions →

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

<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  + ".";
         llSay(0, WelcomePhrase );
    }

}</lsl>

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

<lsl> llSay(0, "Welcome to " + RegionName + ".");</lsl>

with the three lines

<lsl> string WelcomePhrase;

WelcomePhrase = "Welcome to " + RegionName  + ".";
llSay(0, WelcomePhrase );</lsl>

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

<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 straightforward. 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. Remember that a *concatenation* is when two strings are joined with a + plus sign. Thus, 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 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, skip that and go to Functions or go back to Using Variables.