User:Myst Leissa/Snippets

From Second Life Wiki
< User:Myst Leissa
Revision as of 13:59, 4 July 2014 by Myst Leissa (talk | contribs) (Added DoAuth and DoAuth List examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:Wifify Note: If you decide and/or find a better way to list these snippets, I give you my trust and my permission as author to put them where you think they will be most useful, without jeopardizing the rules governing SL Wiki Entry creation. --Myst Leissa 20:41, 31 July 2013 (PDT)

Myst's Snippet Library

If I'm understanding how userpages work this should be a subpage of my userpage, if that is correct then these will be helpful snippets for Scripters both old and new.

Memory Control

While my content often includes memory limiters I felt I should make a minor snippet to explain how best to implement a MONO Script Memory Limiter <lsl> default {

   state_entry()
   {
       llSetMemoryLimit(llGetUsedMemory()+2024);
   }

} </lsl> Explaination: The reason I call llGetUsedMemory()+2024 is for llGetUsedMemory() it returns the amount of memory a script uses at that exact time, however to limit it exactly to that js a mistake that will lead to buffer overflow and stackheap collisions. The answer is to offset the amount of memory by a few kilobytes extra for the script to process new information with. Depending on the content of your script it may actually be more effective to leave certain scripts unlimited while limiting the rest, this is most often the case in scripts where you expect large lists to form after initialization.

SayAsAvatar Snippet

This Function will execute the LSL Command to create a line of text in a Given Avatar's Display Name by Key. (ie instead of llSay(PUBLIC_CHANNEL, "/me "+llGetDisplayName(llDetectedKey(0)+" hides") Which will produce the emote of the "<object name>: <display_name> Hides" You Can Instead use this snippet to cut off <object name> and replace it with the llGetDisplayName(llDetectedKey(0))

<lsl> AvTalk(string msg, key id) {

   string obj_name = llGetObjectName();
   llSetObjectName(llGetDisplayName(id));
   llSay(PUBLIC_CHANNEL, msg);
   llSetObjectName(obj_name);

} </lsl>

An Example of how to use this is:

<lsl> string text_string;

AvTalk(string message, key id) {

   string obj_name = llGetObjectName();
   llSetObjectName(llGetDisplayName(id));
   llSay(PUBLIC_CHANNEL, message);
   llSetObjectName(obj_name);

}

default {

   state_entry()
   {
       key owner = llGetOwner();
       llListen(1, "", owner, "");
       llOwnerSay("Ready to test, type /1<message> to set message the person who touches will say.");
   }
   listen(integer channel, string name, key id, string message)
   {
       text_string = message;
       llOwnerSay("Next Person to touch this object will say '" + text_string + "'.");
   }
   touch_start(integer num_detected)
   {
       key id = llDetectedKey(0);
       if (llStringLength(text_string))
       {
           AvTalk(text_string, id);
       }
       else
       {
           AvTalk("Nothing has been set.", id);
       }
   }

} </lsl>

DoAuth

Ever wanted to setup an access list for something but found it too complex to keep listing more and more conditions? This is a snippet that explains how to "Check" for access to something. The function will return TRUE if finds it's OK to let the avatar in, it'll return FALSE if it's inappropriate for the user to access the function

<lsl>

// First we need to set the access list to our UUIDs so we can check specific avatars quickly

list users = ["b213944a-fb96-49c3-999a-42aaf8b2c664"];

// We'll also need to define flags for when a particular behavior should be displayed

integer selfok_user_flag = TRUE; integer groupok_user_flag = TRUE; integer everyoneok_user_flag = FALSE;

// Next is the function itself

integer DoAuth(key id) {

  // First is it valid for us to use our own device? (useful for devices like restraints etc)
  if(!selfok_user_flag && id==llGetOwner()) return FALSE;
  // Next is it ok if this person is a group member of the same group as the object the script is in
  if(groupok_user_flag && llSameGroup(id)) return TRUE;
  // The core of a DoAuth Function is llListFindList, it returns -1 if there's no exact match for the search criteria
  // otherwise it returns the index of that items place on the list
  if(llListFindList(users,[(string)id])!=-1) return TRUE;
  // Now we need to check to see if this device is public or not
  if(everyoneok_user_flag==TRUE) return TRUE;
  
  // Finally we need to set the behavior if none of the other triggers activate - usually a FALSE return
  return FALSE;

} // To Use the function check for "if(DoAuth(avatar_id))..." condition, which will return TRUE if the auth goes through or FALSE if the auth fails. </lsl> Notes:

  • Operation
    • DoAuth Normally needs to be in the same region as the avatar it's checking
    • DoAuth relies on the fact that functions only return ONCE and only at the first time they are told to do so.
  • Customization
    • DoAuth can be setup with more complex conditions, this is just a simple one.
    • DoAuth is useful for all sorts of things, but sometimes you don't want to just have one static set of users, in these cases you use DoAuthList()

DoAuthList()

This function is an expansion of the DoAuth() function, but it still works fairly well, for the easiness of writing, it will be assumed you are only checking against conditional lists and not other variables <lsl> // First List list owners = ["b213944a-fb96-49c3-999a-42aaf8b2c664"]; list guests = ["7b2c4d53-1bee-4d8f-8279-35b58a584cba"];

// Now the function integer DoAuthList(key id, list params) {

 // We'll check the list params for the key id
 if(llListFindList(params,[(string)id])!=-1) return TRUE;
 return FALSE;

} </lsl> Notes:

  • Operation
    • The simple function written in this example is just a "proof of concept" it will not hold up in the real world, and it is unwise to check lists with JUST the list for TRUE/FALSE, as this can easily be done with just llListFindList looking for -1
  • Customization
    • This is a very simple example, it can be much much more complex.
    • Good Uses include single scripts to control an entire skybox, or Rezzers that should respond differently based on whose using them.

Back to the my main profile