Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:scripting_portal:lsl_user-defined_functions [2018/10/29 02:26] (current)
Royale Mobian created
Line 1: Line 1:
 +====== User-Defined Functions ======
  
 +This category contains custom pre-defined function contributions from the LSL community to extend the abilities of LSL.
 +
 +Go to the [[wiki:​scripting_portal:​lsl_functions|Functions]] page to view the built-in functions.
 +===== About User-Defined Functions =====
 +
 +LSL allows user-defined functions. The syntax is very simple:
 +<code ossl>
 +return_type variable_name(par1type par1name, par2type par2name,​...)
 +{
 +  function_statements;​
 +  .
 +  .
 +  .
 +  return value_of_return_type;​
 +}
 +</​code>​
 +<code ossl>
 +// example with void return type:
 + 
 +let_prim_say_something_and_include_owner_info(string message)
 +{
 +    key ownerKey = llGetOwner();​
 +    string ownerName = llKey2Name(ownerKey);​
 + 
 +    llSay(PUBLIC_CHANNEL,​ "/me (owned by " + ownerName + "): " + message);
 +}
 + 
 +default
 +{
 +    state_entry()
 +    {
 +        let_prim_say_something_and_include_owner_info("​Hello world!"​);​
 +    }
 +}
 +</​code>​
 +<code ossl>
 +// example with integer return type (actually it's a boolean)
 + 
 +integer is_sun_up()
 +{
 +    vector sunDirection = llGetSunDirection();​
 + 
 +    if (sunDirection.z < 0.0)
 +        return FALSE;
 + 
 +    // else
 +        return TRUE;
 +}
 + 
 +default
 +{
 +    state_entry()
 +    {
 +        if ( is_sun_up() )
 +            llSay(PUBLIC_CHANNEL,​ "Good day!"​);​
 +        else
 +            llSay(PUBLIC_CHANNEL,​ "Good night!"​);​
 +    }
 +}
 +</​code>​
 +There are no "​function"​ or "​def"​ keywords or function type/​inheritance specifiers needed. I have not yet discovered what limits (if any) exist on the length or composition of the parameter list. Multiple types can be used, as shown in the example below. A list can be returned, so a single function can return a list with several values in it. User-defined functions will help keep scripts compact and readable, and allow rudimentary code reuse. The [[wiki:​scripting_portal:​lsl_style_guide|LSL Style Guide]] suggests that user-defined functions should be placed before the default state definition and after user-defined variables. Placing user-defined functions after the first state will result in a Syntax Error. ​
QR Code
QR Code wiki:scripting_portal:lsl_user-defined_functions (generated for current page)