Difference between revisions of "LSL 101/Variable Initialization"

From Second Life Wiki
Jump to navigation Jump to search
m (Changed "this" to "thus" for correctness.)
m (<lsl> tag to <source>)
 
Line 5: Line 5:
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.
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
<source lang="lsl2">string RegionName;    // This is a global variable
   
   
default
default
Line 21: Line 21:
           llOwnerSay( WelcomePhrase );
           llOwnerSay( WelcomePhrase );
     }
     }
}</lsl>
}</source>


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


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


with the single line
with the single line


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


The first version allocates the variable and initializes it with the string containing no characters (written as "").  Then, it updates the variable with the string "Welcome to Ganymede.".
The first version allocates the variable and initializes it with the string containing no characters (written as "").  Then, it updates the variable with the string "Welcome to Ganymede.".
Line 36: Line 36:
The second version both declares the variable and initializes it with the value "Welcome to Ganymede." in the same statement.  The syntax for this is easy to remember because it is written as though the declaration and assignment statements were rolled into one.  But keep in mind that this is not simply another form of an assignment statement.  For example, the following code fragment is '''not''' legal.
The second version both declares the variable and initializes it with the value "Welcome to Ganymede." in the same statement.  The syntax for this is easy to remember because it is written as though the declaration and assignment statements were rolled into one.  But keep in mind that this is not simply another form of an assignment statement.  For example, the following code fragment is '''not''' legal.


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


It is not legal because the second statement is an attempt to declare a variable that already exists, and isn't allowed.
It is not legal because the second statement is an attempt to declare a variable that already exists, and isn't allowed.
Line 45: Line 45:
However, the rules for initializing a ''global'' variable are different.  Global variables can have a initializer, but it can only be a simple value.  It can't be the result of a built-in function and can't have any operators.  Thus, we could '''not''' change our global variable declaration to
However, the rules for initializing a ''global'' variable are different.  Global variables can have a initializer, but it can only be a simple value.  It can't be the result of a built-in function and can't have any operators.  Thus, we could '''not''' change our global variable declaration to


<lsl>string RegionName = llGetRegionName();    // This is a global variable</lsl>
<source lang="lsl2">string RegionName = llGetRegionName();    // This is a global variable</source>


On the other hand, we could do this:
On the other hand, we could do this:


<lsl>string RegionName = "Region not known";    // This is a global variable</lsl>
<source lang="lsl2">string RegionName = "Region not known";    // This is a global variable</source>


Initializing global variables to a unique value like this has an advantage over letting it be compiled with the empty string.  When testing your script, if the phrase "Region not known" appears in the output where you expected a region name, you'll immediately know that no assignment to that variable was ever executed.  If, on the other hand, the region name was simply missing (i.e. the empty string), it might be less obvious where things went wrong.
Initializing global variables to a unique value like this has an advantage over letting it be compiled with the empty string.  When testing your script, if the phrase "Region not known" appears in the output where you expected a region name, you'll immediately know that no assignment to that variable was ever executed.  If, on the other hand, the region name was simply missing (i.e. the empty string), it might be less obvious where things went wrong.


'''Continue this tutorial at [[LSL 101/Functions|Functions]] or go back to [[LSL 101/Global vs Local Variables| Global vs Local Variables]].'''
'''Continue this tutorial at [[LSL 101/Functions|Functions]] or go back to [[LSL 101/Global vs Local Variables| Global vs Local Variables]].'''

Latest revision as of 12:48, 24 January 2015

← Global vs Local Variables ↑̲  LSL 101  ̲↑ Functions →

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.

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 );
     }
}

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

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

with the single line

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

The first version allocates the variable and initializes it with the string containing no characters (written as ""). Then, it updates the variable with the string "Welcome to Ganymede.".

The second version both declares the variable and initializes it with the value "Welcome to Ganymede." in the same statement. The syntax for this is easy to remember because it is written as though the declaration and assignment statements were rolled into one. But keep in mind that this is not simply another form of an assignment statement. For example, the following code fragment is not legal.

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

It is not legal because the second statement is an attempt to declare a variable that already exists, and isn't allowed.

Any declaration of a local variable can be followed by the assignment symbol and an initialization expression that follows the same form as an assignment statement. As a matter of fact, providing an initial value is generally considered a good practice, and most examples you see will probably do that.

However, the rules for initializing a global variable are different. Global variables can have a initializer, but it can only be a simple value. It can't be the result of a built-in function and can't have any operators. Thus, we could not change our global variable declaration to

string RegionName = llGetRegionName();    // This is a global variable

On the other hand, we could do this:

string RegionName = "Region not known";    // This is a global variable

Initializing global variables to a unique value like this has an advantage over letting it be compiled with the empty string. When testing your script, if the phrase "Region not known" appears in the output where you expected a region name, you'll immediately know that no assignment to that variable was ever executed. If, on the other hand, the region name was simply missing (i.e. the empty string), it might be less obvious where things went wrong.

Continue this tutorial at Functions or go back to Global vs Local Variables.