User:Toady Nakamura/Simple Listener

From Second Life Wiki
< User:Toady Nakamura
Revision as of 14:38, 11 March 2014 by Toady Nakamura (talk | contribs) (changed teaching script re: expert scriptors comments, left notes & rough edges to show how is used in classroom.)
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.

<lsl> 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(message); // 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();
   }
   

} </lsl>

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

A fancy scriptor came along and changed the code for this teaching script which is used in the classroom for the students to fix and change things.

Note 1: Just 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 are shown for teaching purposes. I can't teach the "if" concept if I do not use an open listener.

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

Note 4: Yes, there are more efficient ways to change the colors. This script is intended as a teaching example only and is not in any way made most efficient.

Note 5: Yes, 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.