Difference between revisions of "Category:LSL User-Defined Functions"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
m (Replaced <source> with <syntaxhighlight> and did some cosmetic improvements)
Line 9: Line 9:
LSL allows user-defined functions.  The syntax is very simple:
LSL allows user-defined functions.  The syntax is very simple:


<source lang="lsl2">
<syntaxhighlight lang="lsl2">
return_type variable_name(par1type par1name, par2type par2name,...)
return_type variable_name(par1type par1name, par2type par2name,...)
{
{
Line 18: Line 18:
   return value_of_return_type;
   return value_of_return_type;
}
}
</source>
</syntaxhighlight>
<source lang="lsl2">
<syntaxhighlight lang="lsl2">
// example with void return type:
// example with void return type:


Line 37: Line 37:
     }
     }
}
}
</source>
</syntaxhighlight>
<source lang="lsl2">
<syntaxhighlight lang="lsl2">
// example with integer return type (actually it's a boolean)
// example with integer return type (actually it's a boolean)


Line 56: Line 56:
     state_entry()
     state_entry()
     {
     {
         if ( is_sun_up() )
         if (is_sun_up())
             llSay(PUBLIC_CHANNEL, "Good day!");
             llSay(PUBLIC_CHANNEL, "Good day!");
         else
         else
Line 62: Line 62:
     }
     }
}
}
</source>
</syntaxhighlight>


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 [[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.
There are no {{mono|function}} or {{mono|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 [[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.


examples can be found linked below
Examples can be found linked below:

Revision as of 13:53, 15 December 2022

User-Defined Functions

This category contains custom pre-defined function contributions from the LSL community to extend the abilities of LSL. To add a wiki page to this category, include a link to this page. You may use the Template:LSL_Function (with "mode" set to user) to create the page to be added here.

Go to the Functions page to view the built-in functions.

About User-Defined Functions

LSL allows user-defined functions. The syntax is very simple:

return_type variable_name(par1type par1name, par2type par2name,...)
{
  function_statements;
  .
  .
  .
  return value_of_return_type;
}
// 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!");
    }
}
// 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!");
    }
}

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 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.

Examples can be found linked below: