Difference between revisions of "LSL 101/String Concatenation"

From Second Life Wiki
Jump to navigation Jump to search
m (where is the rest of the article???)
Line 2: Line 2:
{{NavNextPrev|prev=Functions That Return a Value|next=}}
{{NavNextPrev|prev=Functions That Return a Value|next=}}


Thus far, the example scripts we have seen will produce the same output regardless of where in SL thay are executed.  But a script can determine almost everything about the virtual environment around it, and act accordinglyAs a simple case of this, lets have the script welcome us when we touch its object, using the specific name of whatever sim it is in.
String concatenation allows us to combine two or more strings into a single string, as if they were simply pasted togetherHere's an example where we use string concatenation to put the region name into a complete sentence.
 
<lsl>
<lsl>default
default
{
{
     state_entry()
     state_entry()
Line 18: Line 17:
           llOwnerSay( "Welcome to " + llGetRegionName() + "." );
           llOwnerSay( "Welcome to " + llGetRegionName() + "." );
     }
     }
}
}</lsl>
</lsl>
 
In the touch_start event handler, we see ''llGetRegionName()'', which looks like a built-in function with no parameters, which is exactly what it is.  But it doesn't stand by itself on a line with a semicolon after it, as did the previous built-in functions we've seen.  That's because unlike ''llSay'' or ''llOwnerSay'', which cause something to happen in SL, ''llGetRegionName'' retrieves information from SL (in this case, a string containing the name of the current region) and returns it to the script. 
 
When we say ''llGetRegionName'' '''returns''' a string, we mean that when the sim server executes the line
 
<lsl>llOwnerSay( "Welcome to " + llGetRegionName() + "." );</lsl>


it first executes the ''llGetRegionName'' function, getting back the returned string. Now let's suppose we are in Calistoga, in which case the returned string will be "Calistoga"The server then (in essence) replaces the ''llGetRegionName()'' in the above line with "Calistoga", getting
The + sign, when used with strings, indicates concatenationThis is analogous to the use of + in integer addition


<lsl>llOwnerSay( "Welcome to " + "Calistoga" + "." );</lsl>
As in the previous example, the server executes ''llGetRegionName'' and reduces the line to the equivalent of


The server then concatenates the
llOwnerSay( "Welcome to " + "Ganymede" + "." );

Revision as of 11:24, 21 May 2009

← Functions That Return a Value ↑̲  LSL 101  ̲↑

String concatenation allows us to combine two or more strings into a single string, as if they were simply pasted together. Here's an example where we use string concatenation to put the region name into a complete sentence.

<lsl>default {

    state_entry()
    {
         // Let the object's owner know the script is working
         llOwnerSay( "Congratulations! Your script has started to execute." );
    }

    touch_start( integer num_detected )
    {
         // Let the object's owner know the script is working
         llOwnerSay( "Welcome to " + llGetRegionName() + "." );
    }

}</lsl>

The + sign, when used with strings, indicates concatenation. This is analogous to the use of + in integer addition

As in the previous example, the server executes llGetRegionName and reduces the line to the equivalent of

llOwnerSay( "Welcome to " + "Ganymede" + "." );