Difference between revisions of "Read Note Card Configuration"

From Second Life Wiki
Jump to: navigation, search
m (added category)
(reading notecard should be faster now, changed a few variable names to improve readability)
Line 28: Line 28:
  
 
<lsl>
 
<lsl>
 +
string configurationNotecardName = "Application.Config";
 +
key notecardQueryId;
 
integer line;
 
integer line;
string configurationFile = "Application.Config";
+
 
key readLineId;
+
 
string AvatarName;
 
string AvatarName;
 
string FavoriteColor;
 
string FavoriteColor;
Line 36: Line 37:
 
init()
 
init()
 
{
 
{
    // reset configuration values to default
+
// reset configuration values to default
 
     AvatarName = "Unknown";
 
     AvatarName = "Unknown";
 
     FavoriteColor = "None";
 
     FavoriteColor = "None";
   
+
 
    // make sure the file exists and is a notecard
+
// make sure the file exists and is a notecard
     if(llGetInventoryType(configurationFile) != INVENTORY_NOTECARD)
+
     if(llGetInventoryType(configurationNotecardName) != INVENTORY_NOTECARD)
 
     {
 
     {
        // notify owner of missing file
+
    // notify owner of missing file
         llOwnerSay("Missing inventory notecard: " + configurationFile);
+
         llOwnerSay("Missing inventory notecard: " + configurationNotecardName);
        return; // don't do anything else
+
    // don't do anything else
 +
        return;
 
     }
 
     }
   
+
 
    // initialize to start reading from first line
+
// initialize to start reading from first line (which is 0)
 
     line = 0;
 
     line = 0;
      
+
     notecardQueryId = llGetNotecardLine(configurationNotecardName, line);
    // read the first line
+
    readLineId = llGetNotecardLine(configurationFile, line++);
+
   
+
 
}
 
}
 +
 
processConfiguration(string data)
 
processConfiguration(string data)
 
{
 
{
    // if we are at the end of the file
+
// if we are at the end of the file
 
     if(data == EOF)
 
     if(data == EOF)
 
     {
 
     {
        // notify the owner
+
    // notify the owner
 
         llOwnerSay("We are done reading the configuration");
 
         llOwnerSay("We are done reading the configuration");
       
+
 
        // notify what was read
+
    // notify what was read
 
         llOwnerSay("The avatar name is: " + AvatarName);
 
         llOwnerSay("The avatar name is: " + AvatarName);
 
         llOwnerSay("The favorite color is: " + FavoriteColor);
 
         llOwnerSay("The favorite color is: " + FavoriteColor);
       
+
 
        // do not do anything else
+
    // do not do anything else
 
         return;
 
         return;
 
     }
 
     }
   
+
 
    // if we are not working with a blank line
+
// if we are not working with a blank line
 
     if(data != "")
 
     if(data != "")
 
     {
 
     {
        // if the line does not begin with a comment
+
    // if the line does not begin with a comment
 
         if(llSubStringIndex(data, "#") != 0)
 
         if(llSubStringIndex(data, "#") != 0)
 
         {
 
         {
            // find first equal sign
+
        // find first equal sign
 
             integer i = llSubStringIndex(data, "=");
 
             integer i = llSubStringIndex(data, "=");
           
+
 
            // if line contains equal sign
+
        // if line contains equal sign
 
             if(i != -1)
 
             if(i != -1)
 
             {
 
             {
                // get name of name/value pair
+
            // get name of name/value pair
 
                 string name = llGetSubString(data, 0, i - 1);
 
                 string name = llGetSubString(data, 0, i - 1);
               
+
 
                // get value of name/value pair
+
            // get value of name/value pair
 
                 string value = llGetSubString(data, i + 1, -1);
 
                 string value = llGetSubString(data, i + 1, -1);
               
+
 
                // trim name
+
            // trim name
 
                 list temp = llParseString2List(name, [" "], []);
 
                 list temp = llParseString2List(name, [" "], []);
 
                 name = llDumpList2String(temp, " ");
 
                 name = llDumpList2String(temp, " ");
               
+
 
                // make name lowercase (case insensitive)
+
            // make name lowercase (case insensitive)
 
                 name = llToLower(name);
 
                 name = llToLower(name);
               
+
 
                // trim value
+
            // trim value
 
                 temp = llParseString2List(value, [" "], []);
 
                 temp = llParseString2List(value, [" "], []);
 
                 value = llDumpList2String(temp, " ");
 
                 value = llDumpList2String(temp, " ");
               
+
 
                // name
+
            // name
 
                 if(name == "name")
 
                 if(name == "name")
 
                     AvatarName = value;
 
                     AvatarName = value;
               
+
 
                // color
+
            // color
 
                 else if(name == "favorite color")
 
                 else if(name == "favorite color")
 
                     FavoriteColor = value;
 
                     FavoriteColor = value;
               
+
 
                // unknown name  
+
            // unknown name
 
                 else
 
                 else
 
                     llOwnerSay("Unknown configuration value: " + name + " on line " + (string)line);
 
                     llOwnerSay("Unknown configuration value: " + name + " on line " + (string)line);
  
 
             }
 
             }
            else  // line does not contain equal sign
+
        // line does not contain equal sign
 +
            else
 
             {
 
             {
 
                 llOwnerSay("Configuration could not be read on line " + (string)line);
 
                 llOwnerSay("Configuration could not be read on line " + (string)line);
Line 119: Line 120:
 
         }
 
         }
 
     }
 
     }
   
+
 
    // read the next line
+
// read the next line
     readLineId = llGetNotecardLine(configurationFile, line++);
+
     notecardQueryId = llGetNotecardLine(configurationNotecardName, ++line);
   
+
 
}
 
}
 +
 
default
 
default
 
{
 
{
    state_entry()
 
    {
 
        init();
 
    }
 
 
     on_rez(integer start_param)
 
     on_rez(integer start_param)
 
     {
 
     {
 
         init();
 
         init();
 
     }
 
     }
 +
 
     changed(integer change)
 
     changed(integer change)
 
     {
 
     {
         if(change & CHANGED_INVENTORY) init();
+
         if(change & (CHANGED_OWNER | CHANGED_INVENTORY))
        else if(change & CHANGED_OWNER) init();
+
            init();
 
     }
 
     }
 +
 +
    state_entry()
 +
    {
 +
        init();
 +
    }
 +
 
     dataserver(key request_id, string data)
 
     dataserver(key request_id, string data)
 
     {
 
     {
         if(request_id == readLineId)
+
         if(request_id == notecardQueryId)
 
             processConfiguration(data);
 
             processConfiguration(data);
       
 
 
     }
 
     }
 
}
 
}
 
</lsl>
 
</lsl>
 
{{LSLC|Library}}{{LSLC|Tutorials|Notecard}}
 
{{LSLC|Library}}{{LSLC|Tutorials|Notecard}}

Revision as of 09:43, 9 October 2012

Learn how to read configuration files with Linden Scripting Language (LSL) in Second Life (SL). After viewing this tutorial, you will be able to:

  • Create a script
  • Read each line of a note card
  • Skip over comments
  • Skip blank lines
  • Notify the owner of the lines with unknown settings
    • Unknown setting name
    • Missing delimiter (equal sign)
  • Notify the owner of missing configuration note card
  • Detect when the notecard has been changed
  • Offer case-insensitive settings
  • Trim white-space from name/value settings
  • Initialize with default values
  • Detect that the name of the configuration file is a notecard
  • Detect that you have reached the end of the file


Dedric Mauriac - Configuration Reading Tutorial.jpg

  1. DOWNLOAD Getvidtut.png How to read configuration information from a notecard
# this is a file to configure your application
# blank lines are ignored as well as lines
# proceeded with a "#" sign.
Name = Dedric Mauriac
Favorite Color = Blue

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

string AvatarName; string FavoriteColor;

init() { // reset configuration values to default

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

// make sure the file exists and is a notecard

   if(llGetInventoryType(configurationNotecardName) != INVENTORY_NOTECARD)
   {
   //  notify owner of missing file
       llOwnerSay("Missing inventory notecard: " + configurationNotecardName);
   //  don't do anything else
       return;
   }

// initialize to start reading from first line (which is 0)

   line = 0;
   notecardQueryId = llGetNotecardLine(configurationNotecardName, line);

}

processConfiguration(string data) { // if we are at the end of the file

   if(data == EOF)
   {
   //  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;
   }

// if we are not working with a blank line

   if(data != "")
   {
   //  if the line does not begin with a comment
       if(llSubStringIndex(data, "#") != 0)
       {
       //  find first equal sign
           integer i = llSubStringIndex(data, "=");
       //  if line contains equal sign
           if(i != -1)
           {
           //  get name of name/value pair
               string name = llGetSubString(data, 0, i - 1);
           //  get value of name/value pair
               string value = llGetSubString(data, i + 1, -1);
           //  trim name
               list temp = llParseString2List(name, [" "], []);
               name = llDumpList2String(temp, " ");
           //  make name lowercase (case insensitive)
               name = llToLower(name);
           //  trim value
               temp = llParseString2List(value, [" "], []);
               value = llDumpList2String(temp, " ");
           //  name
               if(name == "name")
                   AvatarName = value;
           //  color
               else if(name == "favorite color")
                   FavoriteColor = value;
           //  unknown name
               else
                   llOwnerSay("Unknown configuration value: " + name + " on line " + (string)line);
           }
       //  line does not contain equal sign
           else
           {
               llOwnerSay("Configuration could not be read on line " + (string)line);
           }
       }
   }

// read the next line

   notecardQueryId = llGetNotecardLine(configurationNotecardName, ++line);

}

default {

   on_rez(integer start_param)
   {
       init();
   }
   changed(integer change)
   {
       if(change & (CHANGED_OWNER | CHANGED_INVENTORY))
           init();
   }
   state_entry()
   {
       init();
   }
   dataserver(key request_id, string data)
   {
       if(request_id == notecardQueryId)
           processConfiguration(data);
   }

} </lsl>