Category talk:LSL User-Defined Functions

From Second Life Wiki
Jump to navigation Jump to search

Optional (and/or Default) Parameters

Hi, just wondering if LSL has the ability to support optional parameters for UDF's?

As example, I'm wanting to create a function that passes commands between attachments. I want this function to always take a "command" variable, and optionally may also take relevant "parameter" variable(s). Something vaguely like this, for a weapon and HUD...

<syntaxhighlight lang="lsl2"> integer ACCESSORY_COMMAND_CHANNEL = 10; key OWNER_KEY;

accessoryCommand(string cmd, string param = "") {

   llRegionSayTo(OWNER_KEY, ACCESSORY_COMMAND_CHANNEL, cmd + ":" + param);

}

default {

   state_entry() {
       OWNER_KEY = llGetOwner();
   }
   
   touch_start(integer num) {
       accessoryCommand("primary_attached");
       accessoryCommand("ammo", "30");
   }

} </syntaxhighlight> Appreciate any help or direction! Takura Thielt (talk) 13:46, 26 July 2024 (PDT)

Edit: I know I could have, and arguably should have just attempted this ISL myself, but I was hoping there might just be some special syntax that I wasn't thinking of. After some discussion elsewhere, I've learned this isn't available in LSL (as perhaps should have been evidenced by the fact that we sometimes need to pass empty/blank variables to built-in functions.)
So, for my example above, I'll just have to do the same - that is, passing the second variable as an empty string, or else, as was suggested, try utilizing JSON functions. (Also, please forgive me as I'm learning Wiki formatting, lol. I suspect I may not be doing this properly.) Takura Thielt (talk) 13:46, 26 July 2024 (PDT)
No such luck—parameter lists must be finalized at compile time. For some very hairy projects I've occasionally used lists, but for a single optional parameter you'll just have to bite the bullet and write it out every single function call. LSL is little better than structured assembly; the programmer has to write for the machine, rather than expecting the compiler to provide creature comforts. Fortunately, server-side Lua is on the horizon! rhet0rica (talk) 09:29, 14 August 2024 (PDT)