Difference between revisions of "User:Toady Nakamura/Simple Listener"

From Second Life Wiki
Jump to navigation Jump to search
m (added simple listener)
 
m (changed teaching script re: expert scriptors comments, left notes & rough edges to show how is used in classroom.)
Line 4: Line 4:


<lsl>
<lsl>
integer CHN = 33; // zero channel is public chat - use a different one
integer CHN = 33; // we use a different channel than 0 which is public chat
integer listener; // records if the listener is on or off (1 or 0)
integer listener; // the "listen handler" is an imaginary box that will hold
key owner; // holds the owner's key ID
                  //  each listening transaction (Note 1)
 
default
default
{
{
     state_entry()
     state_entry()
     {   
     {   
         llListenRemove(listener); // clear any old listeners
         listener = llListen(CHN,"","",""); // open an open listener
         owner = llGetOwner(); // find out who owns prim
 
         // you could also write it only to listen to owner ( See Note 2. )
        // listener = llListen(CHN,"",llGetOwner(),""); // only listen to owner


        listener = llListen(CHN,"",owner,""); // open a listener for just the owner
     }
     }
      
      
Line 21: Line 23:


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


         if(owner==llGetOwnerKey(id))
         if ( id == llGetOwner())
         {
         {
            if (p == "red") // if what is heard is "red"
            if (p == "red") // if what is heard is "red"
             {
             {
                 llSetColor(<1,0,0>, ALL_SIDES); // turn the prim red on all sides
                 llSetColor(<1,0,0>, ALL_SIDES);
                              // turn the whole prim red
                              // ( See Note 4. )
             }
             }
             else if (p == "green")
             else if (p == "green")// if what is heard is "green"
             {
             {
                 llSetColor(<0,1,0>, ALL_SIDES);
                 llSetColor(<0,1,0>, ALL_SIDES);
             }
             }
             else if (p  == "blue")
             else if (p  == "blue")// if what is heard is "blue"
             {
             {
                 llSetColor(<0,0,1>, ALL_SIDES);
                 llSetColor(<0,0,1>, ALL_SIDES);
Line 41: Line 46:
                 llOwnerSay(p);  // and it will tell you the message in open chat
                 llOwnerSay(p);  // and it will tell you the message in open chat
             }
             }
         llListenRemove(listener); // close that session
         llListenRemove(listener); // close that session
         listener = llListen(CHN,"",owner,""); // open a new one
         listener = llListen(CHN,"","",""); // open a new one
                                  // ( See Note 4. )
 
        }
     }
     }
      
      
Line 52: Line 61:
}
}
</lsl>
</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.

Revision as of 14:38, 11 March 2014

  • 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.