Difference between revisions of "LSL 101/Functions That Return a Value"
(New page: Category:LSL 101 {{NavNextPrev|prev=Compile Time Errors|next=String Concatenation}} The built-in functions we've seen so far, ''llSay'' and ''llOwnerSay'', act as commands, causing so...) |
m (<lsl> tag to <source>) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:LSL 101]] | [[Category:LSL 101]] | ||
{{NavNextPrev|prev= | {{NavNextPrev|prev=Functions|next=Creating a Script}} | ||
{{LSL Wikibook Index}} | |||
The built-in functions we've seen so far, ''llSay'' and ''llOwnerSay'', act as commands, causing something to happen in SL. But other built-in functions are used to ''retrieve'' information from SL and make it available to the script. In this case, we say that the function ''returns'' the information. | The built-in functions we've seen so far, ''llSay'' and ''llOwnerSay'', act as commands, causing something to happen in SL. But other built-in functions are used to ''retrieve'' information from SL and make it available to the script. In this case, we say that the function ''returns'' the information. | ||
Line 6: | Line 8: | ||
The built-in function ''llGetRegionName'' is an example of this. When executed, it returns the name of whatever sim the script is executing in. If the script is running in Ganymede, for example, executing ''llGetRegionName'' would return the string "Ganymede". | The built-in function ''llGetRegionName'' is an example of this. When executed, it returns the name of whatever sim the script is executing in. If the script is running in Ganymede, for example, executing ''llGetRegionName'' would return the string "Ganymede". | ||
Here's how that would work in a | Here's how that would work in a script. | ||
< | <source lang="lsl2">default | ||
default | |||
{ | { | ||
state_entry() | state_entry() | ||
Line 15: | Line 16: | ||
llOwnerSay( llGetRegionName() ); | llOwnerSay( llGetRegionName() ); | ||
} | } | ||
} | }</source> | ||
</ | |||
Notice that '' | Notice that ''llGetRegionName'' does not take any parameters, but it still has to be followed by a pair of parentheses. It is ''not'' followed by a semicolon, because it is not, by itself, a statement. | ||
When the SL sim server executes the line | When the SL sim server executes the line | ||
Line 27: | Line 27: | ||
llOwnerSay( "Ganymede" ); | llOwnerSay( "Ganymede" ); | ||
which chats the string "Ganymede" to the object's owner. | which chats the string "Ganymede" to the object's owner. | ||
Now let's make the output a little more meaningful by introducing [[LSL 101/String Concatenation|string concatenation]]. | |||
'''To continue this tutorial go forward to [[LSL 101/Creating a Script|Creating a Script]] or review the page before, to [[LSL 101/Functions| Functions]].''' |
Latest revision as of 13:42, 24 January 2015
← Functions | ↑̲ LSL 101 ̲↑ | Creating a Script → |
The built-in functions we've seen so far, llSay and llOwnerSay, act as commands, causing something to happen in SL. But other built-in functions are used to retrieve information from SL and make it available to the script. In this case, we say that the function returns the information.
The built-in function llGetRegionName is an example of this. When executed, it returns the name of whatever sim the script is executing in. If the script is running in Ganymede, for example, executing llGetRegionName would return the string "Ganymede".
Here's how that would work in a script.
default
{
state_entry()
{
// Announce the name of the current region
llOwnerSay( llGetRegionName() );
}
}
Notice that llGetRegionName does not take any parameters, but it still has to be followed by a pair of parentheses. It is not followed by a semicolon, because it is not, by itself, a statement.
When the SL sim server executes the line
llOwnerSay( llGetRegionName() );
it first executes the llGetRegionName function and (in essence) replaces llGetRegionName() with the string that is returned. This results in something like
llOwnerSay( "Ganymede" );
which chats the string "Ganymede" to the object's owner.
Now let's make the output a little more meaningful by introducing string concatenation.
To continue this tutorial go forward to Creating a Script or review the page before, to Functions.