Difference between revisions of "LlGetNumberOfNotecardLines"

From Second Life Wiki
Jump to navigation Jump to search
m
m
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{LSL_Function/notecard|name|uuid=true}}{{LSL_Function
{{LSL_Function
|inject-2={{LSL_Function/notecard|name|uuid=true}}
|func_id=276|func_sleep=0.1|func_energy=10.0
|func_id=276|func_sleep=0.1|func_energy=10.0
|func=llGetNumberOfNotecardLines
|func=llGetNumberOfNotecardLines
|return_type=key
|return_type=key
|return_subtype=handle
|p1_type=string|p1_name=name
|p1_type=string|p1_name=name
|func_footnote
|func_footnote
|func_desc=Requests number of lines in notecard '''name''' via the [[dataserver]] event (cast dataserver value to integer)
|func_desc=Requests the number of lines in notecard {{LSLP|name}} via the [[dataserver]] event (cast dataserver value to integer)
|return_text=that is the handle for a [[dataserver]] event response.
|Return_text=for a [[dataserver]] event response.
|spec
|spec
|caveats=* If notecard contains embedded inventory items (such as textures and landmarks), invalid data will be returned.
|caveats=* If notecard contains embedded inventory items (such as textures and landmarks), invalid data will be returned.
|constants
|constants
|examples=
|examples=
<lsl>
<source lang="lsl2">
string NOTECARD_NAME = "config"; // name of the card we are going to read
// Ascertain the number of lines in a notecard in the prim's contents
integer notecard_line = 0;
integer num_notecard_lines = 0;
key notecard_request = NULL_KEY;
list card_data; // the data in the card


integer
string gNotecard = "Config";      // Name of notecard to be examined
check_card(string name) // check that that the named inventory item is a notecard
key     gLineRequestID;             // Identity of expected dataserver event
{
integer gLineCounter;               // The number of lines in the NC, as determined by this script
     integer i = llGetInventoryType(name);
    return i == INVENTORY_NOTECARD;
}


default
default
Line 29: Line 24:
     state_entry()
     state_entry()
     {
     {
         state init;
         // Ask how many lines are in the notecard.
    }
         // The answer will arrive via a dataserver event
}
         gLineRequestID = llGetNumberOfNotecardLines(gNotecard);
 
state ready
{
    touch_start(integer detected)
    {
         llOwnerSay("the notecard contained the following data:");
         llOwnerSay(llDumpList2String(card_data, "\n"));
     }
     }
     changed(integer change)
      
    dataserver(key requested, string data)
     {
     {
         if (change & (CHANGED_INVENTORY)) // if someone edits the card, reset the script
         if (requested == gLineRequestID)
         {
         {
             llResetScript();
             llOwnerSay( "The notecard '" + gNotecard + "' contains " + data + " lines" );
         }
            // cast the data string to an integer if you need to access the counter later
            gLineCounter = (integer) data;
         }      
     }
     }
}
}
</source>
<source lang="lsl2">
// Check for a valid existant notecard, and read it into a list
// On touch, say the total number of lines, number of lines containing data, and say each line
// Omei Qunhua  7-Jan-2013
string  gNotecard = "Config";      // Name of notecard to be examined
key    gLineRequestID;            // Identity of expected line count dataserver event
key    gReadRequestID;            // Identity of expected data read dataserver event
integer gLineTotal;                // The number of lines in the NC, as determined by this script
integer gLineIndex;                // Index for data read requests
list    gDataLines;                // List containing all data lines from notecard, excluding blank and comment lines
string  gStatus;                    // Will contain EOF when notecard reading has finished


state init
default
{
{
     state_entry()
     state_entry()
     {
     {
         if (!check_card(NOTECARD_NAME)) // check the card exists
         if (llGetInventoryKey(gNotecard) )         // Test if notecard exists and has been saved (returned key will be NULL_KEY otherwise)
        {
            gLineRequestID = llGetNumberOfNotecardLines(gNotecard);       // Kick off a request for the total number of lines that the notecard contains
            state error;
         else
        }
            llOwnerSay("Notecard '" + gNotecard + "' does not exist or has no saved data");
        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
     changed(integer change)
     {
     {
         llSetTimerEvent(0.0);
         if (change & CHANGED_INVENTORY)
        state error;
            llResetScript();
     }
     }
     dataserver(key query_id, string data)
     dataserver(key requested, string data)
     {
     {
         if (query_id == notecard_request) // make sure it's an answer to a question we asked - this should be an unnecessary check
         if (requested == gLineRequestID)
         {
         {
             llSetTimerEvent(0.0); // at least one line, so don't worry any more
             gLineTotal = (integer) data;           // Cast the data string to an integer to get the number of lines
            if (data == EOF) // end of the notecard, change to ready state
             gReadRequestID = llGetNotecardLine(gNotecard, gLineIndex);     // Request a read of the first notecard line
            {
             return;
                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
         if (requested != gReadRequestID)
         llSetText("read " + (string)(notecard_line) + " of " + (string)num_notecard_lines + " lines", <1, 1, 1>, 1);
            return;
    }
         if ( (gStatus = data) == EOF)             // Save and test the current data (so that other code can tell when we've finished too)
            return;


    state_exit()
        // A notecard line has been read. Kick off the process of fetching the next line, while we process this line
    {
         gReadRequestID = llGetNotecardLine(gNotecard, ++gLineIndex);
         llSetText("", <0, 0, 0>, 0);
    }
}


state error
        data = llStringTrim(data, STRING_TRIM);    // chop off any leading or trailing blanks
{
         if (data == "" || llGetSubString(data, 0, 0) == "#")   
    state_entry()
            return;                               // ignore blank or comment lines
    {
       
         llOwnerSay("something went wrong; try checking that the notecard [ " + NOTECARD_NAME + " ] exists and contains data");
        // Do any further processing of the data here
        gDataLines += data;                       // Add this data line to our global list
     }
     }
     changed(integer change)
     touch_start(integer total_number)
     {
     {
         if (change & CHANGED_INVENTORY)
         if (gStatus != EOF)
        {
            llOwnerSay("Please wait");            // Still reading the notecard
            return;
        }
        integer count = (gDataLines != [] );      // Get list length (efficient shortcut)
        llOwnerSay(gNotecard + " had a total of  " + (string) gLineTotal + " lines, of which " + (string) count + " contained data." );
 
        integer x;
        for ( ; x < count; ++x)                    // Loop through the data list
         {
         {
             llResetScript();
             llOwnerSay( llList2String(gDataLines, x) );
         }
         }
        llOwnerSay("---- end of data ----");
     }
     }
}</lsl>
}
</source>
|helpers
|helpers
|also_functions={{LSL DefineRow||[[llGetNotecardLine]]|}}
|also_functions={{LSL DefineRow||[[llGetNotecardLine]]|}}

Latest revision as of 01:16, 22 January 2015

Summary

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

Requests the number of lines in notecard name via the dataserver event (cast dataserver value to integer)
Returns the handle (a key) 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

// Ascertain the number of lines in a notecard in the prim's contents

string  gNotecard = "Config";       // Name of notecard to be examined
key     gLineRequestID;             // Identity of expected dataserver event
integer gLineCounter;               // The number of lines in the NC, as determined by this script

default
{
    state_entry()
    {
        // Ask how many lines are in the notecard.
        // The answer will arrive via a dataserver event
        gLineRequestID = llGetNumberOfNotecardLines(gNotecard);
    }
    
    dataserver(key requested, string data)
    {
        if (requested == gLineRequestID)
        {
            llOwnerSay( "The notecard '" + gNotecard + "' contains " + data + " lines" );
            // cast the data string to an integer if you need to access the counter later
            gLineCounter = (integer) data;
        }        
    }
}
// Check for a valid existant notecard, and read it into a list
// On touch, say the total number of lines, number of lines containing data, and say each line
// Omei Qunhua  7-Jan-2013

string  gNotecard = "Config";       // Name of notecard to be examined
key     gLineRequestID;             // Identity of expected line count dataserver event
key     gReadRequestID;             // Identity of expected data read dataserver event
integer gLineTotal;                 // The number of lines in the NC, as determined by this script
integer gLineIndex;                 // Index for data read requests
list    gDataLines;                 // List containing all data lines from notecard, excluding blank and comment lines
string  gStatus;                    // Will contain EOF when notecard reading has finished

default
{
    state_entry()
    {
        if (llGetInventoryKey(gNotecard) )         // Test if notecard exists and has been saved (returned key will be NULL_KEY otherwise)
            gLineRequestID = llGetNumberOfNotecardLines(gNotecard);       // Kick off a request for the total number of lines that the notecard contains
        else
            llOwnerSay("Notecard '" + gNotecard + "' does not exist or has no saved data");
    }
    changed(integer change)
    {
        if (change & CHANGED_INVENTORY)
            llResetScript();
    }
    dataserver(key requested, string data)
    {
        if (requested == gLineRequestID)
        {
            gLineTotal = (integer) data;           // Cast the data string to an integer to get the number of lines
            gReadRequestID = llGetNotecardLine(gNotecard, gLineIndex);      // Request a read of the first notecard line
            return;
        }
        if (requested != gReadRequestID)
            return;
        if ( (gStatus = data) == EOF)              // Save and test the current data (so that other code can tell when we've finished too)
            return;

        // A notecard line has been read. Kick off the process of fetching the next line, while we process this line
        gReadRequestID = llGetNotecardLine(gNotecard, ++gLineIndex);

        data = llStringTrim(data, STRING_TRIM);    // chop off any leading or trailing blanks
        if (data == "" || llGetSubString(data, 0, 0) == "#")     
            return;                                // ignore blank or comment lines
        
        // Do any further processing of the data here
        gDataLines += data;                        // Add this data line to our global list
    }
    touch_start(integer total_number)
    {
        if (gStatus != EOF)
        {
            llOwnerSay("Please wait");             // Still reading the notecard
            return;
        }
        integer count = (gDataLines != [] );       // Get list length (efficient shortcut)
        llOwnerSay(gNotecard + " had a total of  " + (string) gLineTotal + " lines, of which " + (string) count + " contained data." );

        integer x;
        for ( ; x < count; ++x)                    // Loop through the data list
        {
            llOwnerSay( llList2String(gDataLines, x) );
        }
        llOwnerSay("---- end of data ----");
    }
}

See Also

Events

•  dataserver

Functions

•  llGetNotecardLine

Deep Notes

Signature

function key llGetNumberOfNotecardLines( string name );