Difference between revisions of "LSL 101/Variable Initialization"

From Second Life Wiki
Jump to navigation Jump to search
(New page: Category:LSL 101 {{NavNextPrev|prev=Global vs Local Variables|next=Event Handler Parameters}})
 
Line 1: Line 1:
[[Category:LSL 101]]
[[Category:LSL 101]]
{{NavNextPrev|prev=Global vs Local Variables|next=Event Handler Parameters}}
{{NavNextPrev|prev=Global vs Local Variables|next=Event Handler Parameters}}
When a variable declaration is executed, the variable always gets ''some'' initial value, whether or not our script specifies.  This is called '''variable initialization''', and it deserves a little discussion of its own.  So far, we have passed over this detail by always executing an assignment statement for the variable prior to using it.  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 = "Welcome to " + RegionName  + ".";
          llOwnerSay( WelcomePhrase );
    }
}</lsl>
The only difference between the previous example and this one is that we have replaced the two lines
<lsl> string WelcomePhrase
WelcomePhrase = "Welcome to " + RegionName  + ".";</lsl>
with the single line
<lsl> string WelcomePhrase = "Welcome to " + RegionName  + ".";</lsl>
It looks as though we have combined the variable declaration and the assignment statement into one statement, and in effect, that is what we have done.  Any declaration of a local variable can be followed by the assignment symbol and an expression that follows the same rules as that for an assignment statement.  As a matter of fact, this is generally considered a good practice, and most example you see will probably do that.
However, the rules for initializing a ''global'' variable are different.

Revision as of 17:25, 2 June 2009

← Global vs Local Variables ↑̲  LSL 101  ̲↑ Event Handler Parameters →

When a variable declaration is executed, the variable always gets some initial value, whether or not our script specifies. This is called variable initialization, and it deserves a little discussion of its own. So far, we have passed over this detail by always executing an assignment statement for the variable prior to using it. 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 = "Welcome to " + RegionName  + ".";
         llOwnerSay( WelcomePhrase );
    }

}</lsl>

The only difference between the previous example and this one is that we have replaced the two lines

<lsl> string WelcomePhrase

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

with the single line

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

It looks as though we have combined the variable declaration and the assignment statement into one statement, and in effect, that is what we have done. Any declaration of a local variable can be followed by the assignment symbol and an expression that follows the same rules as that for an assignment statement. As a matter of fact, this is generally considered a good practice, and most example you see will probably do that.

However, the rules for initializing a global variable are different.