User:Toady Nakamura/Simple Listener

From Second Life Wiki
Jump to navigation Jump to search
  • Place this script in any prim.
  • Say /33 red ... /33 green ... or ... /33 blue
  • If you are the prim's owner, the prim will change color.
integer CHN = 33; // we use a different channel than 0 which is public chat
integer listener; // the "listen handler" is an imaginary box that will hold
                  //  each listening transaction (Note 1)

default
{
    state_entry()
    {   
        listener = llListen(CHN,"","",""); // open an open listener

        // you could also write it only to listen to owner ( See Note 2. )
        // listener = llListen(CHN,"",llGetOwner(),""); // only listen to owner 

    }
    
    listen(integer channel, string name, key id, string msg)
    { 

        string p = llToLower(msg); // convert the message heard to lower case
                                       // ( See Note 3. )

        if ( id == llGetOwner())
        {
            if (p == "red") // if what is heard is "red"
            {
                llSetColor(<1,0,0>, ALL_SIDES);
                              // turn the whole prim red
                              // ( See Note 4. )
            }
            else if (p == "green")// if what is heard is "green"
            {
                llSetColor(<0,1,0>, ALL_SIDES);
            }
            else if (p  == "blue")// if what is heard is "blue"
            {
                llSetColor(<0,0,1>, ALL_SIDES);
            }
            else  // if the message is none of the above ... 
            {
                llSetColor(<1,1,1>, 0);  // just the top of the box will turn white
                llOwnerSay(p);  // and it will tell you the message in open chat
            }

        llListenRemove(listener); // close that session
        listener = llListen(CHN,"","",""); // open a new one
                                  // ( See Note 4. )

        }
    }
    
    on_rez(integer start_param) 
    {
        llResetScript();
    }
    
}


....................................................... EXTRA NOTES.......................................................

A fancy scriptor recently made comments about this teaching script. I made changes to the script above based on their comments, but not all. I append the comments for the things I did not change below so in case some other fancy scriptor comes along, they don't waste time changing things that will just be changed right back again.

This script will not now nor ever be up to "perfect" script standards in this form because it is used as a base in the classroom for students to fix and change.


Note 1: Saying " The variable holds the current listen handle" regarding the listen handler is really not helpful for new learners. The listen handle can be considered a box that is either "full" or "empty", or if the listener is "active" or "not active". That may not be the perfect way for an experienced scriptor to think of the concept, but once you get the concept you don't need my explanations anyway.

Note 2: Both forms of how to open a listener (for anyone and for owner) are shown for teaching purposes. I can't teach the "if" check for owner if I do not use an open listener.

Note 3: Variable name "p" is not as expressive as some acronym like "lc_msg", but it's simple and valid.

Note 4: This is not intended to be the most efficient way to change colors, but a simple way to show the student how things work.

Note 5: For this example there is no real reason to close & open the listener, but the student is likely to use this framework in times when that is needed and the wee bit of extra script lag introduced by the teaching script in the classroom will pay off later in the more complicated scripts.

Visit my LSL wiki page for my library of simple scripts ! Toady Nakamura