Talk:Read Note Card Configuration

From Second Life Wiki
Jump to: navigation, search

Observations

  1. if(llSubStringIndex(data, "#") != 0) <<== This involves a byte-by-byte search for '#', even if none exists. Better to examine the first byte only, but do a STRING_TRIM first
  2. notecardQueryId = llGetNotecardLine(configurationNotecardName, ++line); <<== this can be done earlier in the dataserver event, to gain time from background processing by the server
  3. string name = llGetSubString(data, 0, i - 1); <<== confusing choice of data name for this application, which is processing avatar names
  4. There's no point having a user defined function that is only called once. In fact both UDF's can easily be avoided, providing savings in script size.

Omei Qunhua 03:48, 17 December 2013 (PST)

  1. Agreed.
  2. Agreed.
  3. In other programming contexts they would be a key/value pair. But key is reserved in LSL. It could be better named or documented.
  4. Yes they can both be entirely avoided but they show the user which code is specific to the actual processing and initialization and which is just stuff necessary for running the script. It nicely divides the code into reasonable chunks. So I agree but it comes at the cost of readability.
This is in the Tutorials category, readability is more important than optimization. I too have trouble not optimizing it. That said, one way to both keep the readability and improve the article would be to discuss the shortcomings (that we don't fix) of the script and then show the corrected script. -- Strife (talk|contribs) 22:18, 17 December 2013 (PST)
Well on point 4, surely the different events achieve very nicely the dividing of the script into the specific areas of initialisation and notecard processing :) I've called the key/value pair 'ident/value'.

<lsl> string configurationNotecardName = "Application.Config"; key notecardQueryId; integer linenumber = 0;

string AvatarName = "Unknown"; string FavoriteColor = "None";

default {

   state_entry()
   {
       //  make sure the file exists, and is a notecard
       if (llGetInventoryType(configurationNotecardName) != INVENTORY_NOTECARD)
       {
           //  notify owner of missing notecard
           llOwnerSay("Inventory notecard is missing: " + configurationNotecardName);
           //  don't do anything else
           return;
       }
       notecardQueryId = llGetNotecardLine(configurationNotecardName, linenumber);
   }
   on_rez(integer start_param)
   {
       llResetScript();
   }
   changed(integer change)
   {
       if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
           llResetScript();
   }
   dataserver(key request_id, string data)
   {
       if (request_id != notecardQueryId)
           return;      // Ignore events apart from the one we want
       if (data == EOF)                             //  we are at the end of the notecard ...
       {
           //  notify the owner
           llOwnerSay("We are done reading the configuration");
           //  notify what was read
           llOwnerSay("The avatar name is: " + AvatarName);
           llOwnerSay("The favorite color is: " + FavoriteColor);
           //  do not do anything else
           return;
       }
       //  kick-off the process of reading the next notecard line. We can do this as early as possible
       notecardQueryId = llGetNotecardLine(configurationNotecardName, ++linenumber);
       data = llStringTrim(data, STRING_TRIM);
       if (data == "")
           return;         // Ignore blank lines
       if (llGetSubString(data, 0, 0) == "#")
           return;         // Ignore comment lines
       //  find first equal sign
       integer i = llSubStringIndex(data, "=");
       if (i != -1)   //  if line contains an equal sign
       {
           //  get the ident/value pair
           string ident = llGetSubString(data, 0, i - 1);
           string value = llGetSubString(data, i + 1, -1);
           //  trim ident
           list temp = llParseString2List(ident, [" "], []);
           ident = llDumpList2String(temp, " ");
           //  make ident lowercase (case insensitive)
           ident = llToLower(ident);
           //  trim value
           temp = llParseString2List(value, [" "], []);
           value = llDumpList2String(temp, " ");
           if (ident == "name")
               AvatarName = value;
           //  color
           else if (ident == "favorite color")
               FavoriteColor = value;
           //  unknown ident
           else
               llOwnerSay("Unknown configuration value: " + ident + " on line " + (string) linenumber);
       }
       else          //  line does not contain equal sign
       {
           llOwnerSay("Configuration could not be read on line " + (string) linenumber + "  " + data);
       }
   }

} </lsl>

Omei Qunhua 06:16, 18 December 2013 (PST)

Seems fine after a first look, not tested. The only thing that comes to my mind when looking at this is that the lsl tags in the wiki do not make the content within them break lines at the viewports right side. In other words, when looking at this page on a lower resolution screen the script body will overflow the lsl container and continue until the end of the line (wherever that is). Maybe break lines that are longer than a hundred or something? Cheers -- Kireji Haiku 11:28, 19 December 2013 (PST)