llGetNumberOfNotecardLines

From Second Life Wiki
Revision as of 12:11, 20 January 2010 by Strife Onizuka (talk | contribs)
Jump to navigation Jump to search

Summary

Function: key llGetNumberOfNotecardLines( string name );
0.1 Forced Delay
10.0 Energy

Requests number of lines in notecard name via the dataserver event (cast dataserver value to integer)
Returns a key that is the handle for a dataserver event response.

• string name a notecard in the inventory of the prim this script is in or a UUID of a notecard

Caveats

  • This function causes the script to sleep for 0.1 seconds.
  • If name is missing from the prim's inventory and it is not a UUID or it is not a notecard then an error is shouted on DEBUG_CHANNEL.
  • If name is a UUID then there are no new asset permissions consequences for the object.
    • The resulting object develops no new usage restrictions that might have occurred if the asset had been placed in the prims inventory.
  • If name is a new empty notecard (never saved) then an error "Couldn't find notecard ~NAME~" (~NAME~ being the value of name) will be shouted on the DEBUG_CHANNEL. This is because until a notecard is saved for the first time, it does not exist as an asset only as an inventory placeholder.
  • If notecard contains embedded inventory items (such as textures and landmarks), invalid data will be returned.

Examples

<lsl> string NOTECARD_NAME = "config"; // name of the card we are going to read integer notecard_line = 0; integer num_notecard_lines = 0; key notecard_request = NULL_KEY; list card_data; // the data in the card

integer check_card(string name) // check that that the named inventory item is a notecard {

   integer i = llGetInventoryType(name);
   return i == INVENTORY_NOTECARD;

}

default {

   state_entry()
   {
       state init;
   }

}

state ready {

   touch_start(integer detected)
   {
       llOwnerSay("the notecard contained the following data:");
       llOwnerSay(llDumpList2String(card_data, "\n"));
   }
   changed(integer change)
   {
       if (change & (CHANGED_INVENTORY)) // if someone edits the card, reset the script
       {
           llResetScript();
       }
   }

}

state init {

   state_entry()
   {
       if (!check_card(NOTECARD_NAME)) // check the card exists
       {
           state error;
       }
       llSetText("initialising...", <1, 1, 1>, 0);
       notecard_request = NULL_KEY;
       notecard_line = 0;
       num_notecard_lines = 0;
       notecard_request = llGetNumberOfNotecardLines(NOTECARD_NAME); // ask for the number of lines in the card
       llSetTimerEvent(5.0); // if we don't hear back in 5 secs, then the card might have been empty
   }
   timer() // if we time out, it meant something went wrong - the notecard was probably empty
   {
       llSetTimerEvent(0.0);
       state error;
   }
   dataserver(key query_id, string data)
   {
       if (query_id == notecard_request) // make sure it's an answer to a question we asked - this should be an unnecessary check
       {
           llSetTimerEvent(0.0); // at least one line, so don't worry any more
           if (data == EOF) // end of the notecard, change to ready state
           {
               state ready;
           }
           else if (num_notecard_lines == 0) // first request is for the number of lines
           {
               num_notecard_lines = (integer)data;
               notecard_request = llGetNotecardLine(NOTECARD_NAME, notecard_line); // now get the first line
           }
           else
           {
               if (data != "" && llGetSubString(data, 0, 0) != "#") // ignore empty lines, or lines beginning with "#"
               {
                   card_data = (card_data = []) + card_data + data;
               }
               ++notecard_line;
               notecard_request = llGetNotecardLine(NOTECARD_NAME, notecard_line); // ask for the next line
           }
       }
       // update the hover-text with the progress
       llSetText("read " + (string)(notecard_line) + " of " + (string)num_notecard_lines + " lines", <1, 1, 1>, 1);
   }
   state_exit()
   {
       llSetText("", <0, 0, 0>, 0);
   }

}

state error {

   state_entry()
   {
       llOwnerSay("something went wrong; try checking that the notecard [ " + NOTECARD_NAME + " ] exists and contains data");
   }
   changed(integer change)
   {
       if (change & CHANGED_INVENTORY)
       {
           llResetScript();
       }
   }
}</lsl>

See Also

Events

•  dataserver

Functions

•  llGetNotecardLine

Deep Notes

Signature

function key llGetNumberOfNotecardLines( string name );