Difference between revisions of "User:Shadowen Silvera/EventDisplayScript"

From Second Life Wiki
Jump to navigation Jump to search
(New page: Instructions each line is in the format of:<br> <time>|<color>|<text><br> for example<br> 3|<1,1,1>|testing text<br> will display a white "testing text" for 3 seconds. IMPORTANT INFORMAT...)
 
(Removing all content from page)
 
Line 1: Line 1:
Instructions


each line is in the format of:<br>
<time>|<color>|<text><br>
for example<br>
3|<1,1,1>|testing text<br>
will display a white "testing text" for 3 seconds.
IMPORTANT INFORMATION:
Each line in the notecard cannot be more than 255 characters in length. That includes the timer interval, color, and "|" symbols.
Do not use "|" symbols anywhere in your text. It is used to determind where timer, color, and text begin.
The names of the notecards cannot be more than 24 characters in length.
Do not have any empty lines in the notecard or lines in the wrong format
I have provided some sample notecards. Each sample contains a line that has numbers such as 012345678901234 as the text.. these lines are 255 characters in length.
One of the sample notecards is named similarily. It is 24 characters in length.
The Script:
<lsl>
vector defcolor = <1,1,1>; //default color, used for the infoline.
string infoline = "Click me for Info"; //infoline self-explanatory I think.
integer wrap = 40;  //how many characters to wrap at for word-wrapping
//shouldnt need to edit below this line
key qID; //key of notecard for dataserver event
integer linenum; //current line to read
string notecard; //current selected notecard
integer num_cards; //total number of notecards
integer chan;      //channel to listen on for dialog
integer MenuOffset; //offset for when there are too many notecards to show in one menu.
key currUser;      //currUser of menu
//this function takes the notecard line, parses it, and sets appropriate text and timer.
setMsg(string data)
{
    list tmp = llParseString2List(data,["|"],[]); //parse the line
    float interval = (float)llList2String(tmp,0); //time interval (first entry)
    vector color = (vector)llList2String(tmp,1);  //color (second entry)
    string msg = llList2String(tmp,2);            //the msg itself (third entry)
    msg = format(msg); //this function formats the message (word wrapping)
    llSetText(msg,color,1.0);  //set the text.
    llSetTimerEvent(interval);  //set the timer.
}
//returns a word wrapped string based on passed in value s.
string format(string s)
{
    integer len = llStringLength(s); //the length of the string
    integer i = 0; //iterator for our loops
    string tmp;    //used for a substring checking for spaces
    integer j;      //iterator for our nested loop
    //if the length is greater than the wrap size then we need to insert \n's
    //otherwise we will just return the string unmodified
    if(len > wrap) 
    {
        //in the interest of making this function faster we are going to check
        //to see if the string has no spaces at all.. if it does we will simply
        //insert \n's at the wrap setting and then return.
        if(llSubStringIndex(s," ") == -1) //check for spaces
        {
            i = 0; //set our interator at 0.
            do //this loop will insert the \n's
            {
                s = llInsertString(s,i*wrap,"\n"); //insert \n at i * wrap
                i++; //increase the interator
            }while(i<=(len / wrap)); //stop looping when i is greater than len / wrap
            return s; //return the string and exit
        }
        //if we have made it to this point there are spaces in the string
        i = 0; //set our interator at 0.
        do //this loop, like the one above, loops as many times as len / wrap
        {
            j = i * wrap; //j is the index of where to start wrapping from
            //this loop moves backwards from j till it finds a space it then
            //inserts a \n and starts the loop again from the next index.
            do
            {
                tmp = llGetSubString(s,j,j); //grab teh substring at j
                if(tmp == " ") //is it a space?
                {
                    s = llInsertString(s,j,"\n"); //its a space, insert \n
                    jump div; //jump out of this loop and back to main loop
                }
                j--; //space not found yet decrease j by 1 and try again
            }while(j>((i - 1) * wrap)); //keep looping till we have reached the start
           
        s = llInsertString(s,i*wrap,"\n"); //no spaces found, insert \n at wrap point
        //label used for jumping out of nested loop.
        @div;
        i++; //increase i by 1
        }while(i<=(len / wrap)); //loop till reach len / wrap
    }
    //llWhisper(0,"done");
    return s; //return the string
}
//this function is used to dynamically create a unique channel based on owners key
integer key2int(key id)
{
    return llBase64ToInteger(llStringToBase64(llBase64ToString((string)id)));
}
//initial function runs whenever script is reset
init()
{
    //fetch totalnumber of notecards
    num_cards = llGetInventoryNumber(INVENTORY_NOTECARD);
    chan = key2int(llGetOwner()); //set chan to key2int
    llListen(chan,"",NULL_KEY,"");//listen on channel
    llSetText(infoline,defcolor,1.0); //set the default text
    linenum = 0; //make sure we start reading notecard lines from 0
}
//this fun little function builds a list of our notecards.
//what makes it interesting is that if notecards in the inventory
//are more than the dialog can display (12) it will only show 10
//and insert prev/next buttons as needed. It also displays from an offset
//so when next is chosen the offset increases or decreases when prev is chosen
Menu()
{
    //there is no point in doing all this if there is only 1 notecard.. check for it
    if(num_cards != 1) //how many?
    {
        //max is used for retrieving the names of the notecards.
        integer max; //max will be set to num_cards or 10 + 10 * offset
        if((num_cards - (10 * MenuOffset)) > 10) max = 10 + (10 * MenuOffset); else max = num_cards;
       
        // i is used for our loop that retrieves notecard names
        integer i = (10 * MenuOffset);
   
        list t; //a temporarly list of the notecard names
        do //the loop
        {
            //this is just a memory efficient way to assign values to a list
            t = (t=[]) + t + [llGetInventoryName(INVENTORY_NOTECARD,i)];
            i++; //increase i by 1
        }
        while(i<max); //we are done with i is greater than or equal to max
   
        //these lines insert the prev and next buttons if they are needed
        //if you read it a few times it will make sense :)
        if(max == num_cards && MenuOffset > 0) t = (t = []) + t + ["Prev"];
        else if(max > 10 && MenuOffset > 0) t = (t = []) + t + ["Prev","Next"];
        else if(max == 10 && MenuOffset == 0 && num_cards > 10) t = (t = []) + t + ["Next"];
   
        //send the dialog
        llDialog(currUser,"Choose a Notecard",t,chan);
        //clear the list to be safe
        t=[];
    }
    else if(num_cards == 1) //only one notecard.. just set it and test it
    {
        notecard = llGetInventoryName(INVENTORY_NOTECARD,0);
        qID = llGetNotecardLine(notecard,linenum);
    }
}
default
{
    state_entry()
    {
        init(); //script was reset.. run init
    }
    on_rez(integer start) //object rezzed reset script
    {
        llResetScript();
    }
    touch_start(integer num)
    {
        //check to see if person touching is the owner or in the same group as object
        if(llDetectedKey(0) == llGetOwner() || llSameGroup(llDetectedKey(0)))
        {
            //get the key of the user for teh dialogs
            currUser = llDetectedKey(0);
            MenuOffset = 0; //set offset to 0, this is the first dialog
            Menu(); //do the fun menu
        }
        else if(linenum == 0) //otherwise it is someone else so just display the notecard data
        {
            qID = llGetNotecardLine(notecard,linenum); //fetch the line
        }
    }
    //check to see if owner has changed and reset script
    changed(integer change)
    {
        if(change & CHANGED_OWNER)
        {
            llResetScript();
        }
    }
    //when timer goes off fetch next line
    timer()
    {
        linenum++; //increase linenum by 1
        qID = llGetNotecardLine(notecard,linenum); //get next line
    }
    //this is the listen for the dialog menus
    listen(integer channel, string name, key id, string str)
    {
        if(id == llGetOwner() || llSameGroup(id)) //is owner or same group?
        {
            if(str == "Next") //next chosen, increase offset by 1 and create menu
            {
                  MenuOffset++;
                  Menu();
            }
            else if(str == "Prev") //prev chose, increase offset by 1 and create menu
            {
                MenuOffset--;
                Menu();
            }
            else //a notecard was chosen.. set it and test it.
            {
                notecard = str;
                qID = llGetNotecardLine(notecard,linenum);
            }
        }
    }
   
    //fetches notecard lines
    dataserver(key id, string data)
    {
        if(id == qID && data != EOF) //check to make sure its not END OF FILE
        {
            setMsg(data);  //parse string and set msg/timer
        }
        if(id == qID && data == EOF) //is END OF FILE
        {
            linenum = 0; //reset linenum back to 0
            llSetTimerEvent(0); //set timer to 0
            llSetText(infoline,defcolor,1.0); //set default text
        }
    }
}
</lsl>
Sample Notecards:
Name: Sample
1|<1,1,1>|THis is a line of text for testing this script.
2|<1,0,0>|THis is a line of text for testing this script. THis is a line of text for testing this script.
2|<0,0,1>|THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script.
2|<0,1,0>|THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script.
6|<1,1,0>|0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123
2|<1,1,0>|01234567890
Name: Sample2
1|<1,1,1>|THis is a line of text for testing this other notecard.
2|<1,0,0>|THis is a line of text for testing this other notecard. THis is a line of text for  testing this script.
2|<0,0,1>|THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script.
2|<0,1,0>|THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script.
2|<1,1,0>|0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123
2|<1,1,0>|01234567890
Name: 012345678901234567890123
1|<1,1,1>|THis is a line of text for testing this other notecard.
2|<1,0,0>|THis is a line of text for testing this other notecard. THis is a line of text for testing this script.
2|<0,0,1>|THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script.
2|<0,1,0>|THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script. THis is a line of text for testing this script.
2|<1,1,0>|0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123
2|<1,1,0>|01234567890
Please feel free to add any questions, comments or suggestions. I am sure there are many bugs to fix in this script.

Latest revision as of 01:39, 20 October 2008