Difference between revisions of "LSL 101/String Concatenation"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:LSL 101]]
[[Category:LSL 101]]
{{NavNextPrev|prev=Functions That Return Values|next=}}
{{NavNextPrev|prev=Strings and Simple Output|next=Integers}}
{{LSL Wikibook Index}}


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>
<source lang="lsl2">
default
default
{
{
Line 15: Line 16:
     touch_start( integer num_detected )
     touch_start( integer num_detected )
     {
     {
           // Let the object's owner know the script is working
           // Announce the region where the script is running
           llOwnerSay( "Welcome to " + llGetRegionName() + "." );
           llOwnerSay( "Welcome to " + llGetRegionName() + "." );
     }
     }
}
}
</lsl>
</source>
 
The + sign, when used with strings, indicates concatenation.  This is analogous to the use of + with integers to denote integer addition.
 
As in the previous example, the server executes ''llGetRegionName'' and reduces the line to the equivalent of
 
<source lang="lsl2">
    llOwnerSay( "Welcome to " + "Ganymede" + "." );
</source>
 
It then concatenates the three strings, so it is equivalent to
 
<source lang="lsl2">
    llOwnerSay( "Welcome to Ganymede." );
</source>
 
You might be thinking that an alternative to using string concatenation could be


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. 
<source lang="lsl2">
    llOwnerSay( "Welcome to ", llGetRegionName(), "." );
</source>


When we say ''llGetRegionName'' '''returns''' a string, we mean that when the sim server executes the line
But this won't work, because ''llOwnerSay'' expects only ''one'' string.  Another alternative,


llOwnerSay( "Welcome to " + llGetRegionName() + "." );
<source lang="lsl2">
    llOwnerSay( "Welcome to ");
    llOwnerSay( llGetRegionName() );
    llOwnerSay( "." );
</source>


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
is legal, but it produces three separate lines of chat instead of one.


  llOwnerSay( "Welcome to " + "Calistoga" + "." );
Even if everything you read here seems completely clear, your understanding will be much better if you actually write scripts as you go along.  So here's a suggested exercise.  The built-in function [[llGetDate|''llGetDate'']] returns a string representing today's date. Write and test a script that announces the date whenever it is clicked on.


The server then concatenates the
'''Proceed onward to [[LSL 101/Integers | Integers]] or go back to [[LSL 101/Strings and Simple Output| Strings and Simple Output]].
'''

Latest revision as of 13:46, 24 January 2015

← Strings and Simple Output ↑̲  LSL 101  ̲↑ Integers →

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.

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 )
     {
          // Announce the region where the script is running
          llOwnerSay( "Welcome to " + llGetRegionName() + "." );
     }
}

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

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

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

It then concatenates the three strings, so it is equivalent to

    llOwnerSay( "Welcome to Ganymede." );

You might be thinking that an alternative to using string concatenation could be

    llOwnerSay( "Welcome to ", llGetRegionName(), "." );

But this won't work, because llOwnerSay expects only one string. Another alternative,

    llOwnerSay( "Welcome to ");
    llOwnerSay( llGetRegionName() );
    llOwnerSay( "." );

is legal, but it produces three separate lines of chat instead of one.

Even if everything you read here seems completely clear, your understanding will be much better if you actually write scripts as you go along. So here's a suggested exercise. The built-in function llGetDate returns a string representing today's date. Write and test a script that announces the date whenever it is clicked on.

Proceed onward to Integers or go back to Strings and Simple Output.