Monday 13 June 2011

Making a light switch

Today i found out how to make a light switch in my library :)

Here's an easy way to make a basic light switch:

The switch is made of 3 parts/prims: The rotor, switch and light source.

The script for the rotor is as follows:


myon_state()
{
   
   rotation rot = llGetRot();
   vector vrot = llRot2Euler(rot);
   vrot+=<-30*DEG_TO_RAD,0,0>;
   rot=llEuler2Rot(vrot);
   llSetRot(rot); 
   
   
}


myoff_state()
{
    
   rotation rot = llGetRot();
   vector vrot = llRot2Euler(rot);
   vrot+=<30*DEG_TO_RAD,0,0>;
   rot=llEuler2Rot(vrot);
   llSetRot(rot); 
   
   
}


default
{
    state_entry()
    {
           myoff_state();  
                  
    }
   
   
   
   
    link_message(integer sender_num, integer num, string str, key id)
    {
        if(str=="stop")
        {
            myoff_state();
        }
        if(str=="start")
        {
           myon_state();
        }
    }
}

The script for the switch is as follows:

integer myswitch;

default
{
     
   
    state_entry()
    {
       
        myswitch=FALSE;
        llSetText("Light Switch", <1.0, 1.0, 1.0>, 1.0);
    }

    touch_start(integer total_number)
    {   
  
       if(myswitch==FALSE)
       {   
      
       //Turn Light Bulb ON
    
       llMessageLinked(LINK_ALL_CHILDREN, 0, "start", NULL_KEY); 
       myswitch=TRUE;
      
      
      
       }
       else
      {
         
      //Turn Light Bulb Off
         
      llMessageLinked(LINK_ALL_CHILDREN, 0, "stop", NULL_KEY); 
      myswitch=FALSE;   
     
     
   
     
     
      }
   
    }
}

And finally, the script for the light source is as follows:
default
{
    state_entry()
    {
        llSetPrimitiveParams([PRIM_FULLBRIGHT,ALL_SIDES,FALSE]);
                  
    }
   
   
   
   
    link_message(integer sender_num, integer num, string str, key id)
    {
        if(str=="stop")
        {
           llSetPrimitiveParams([PRIM_FULLBRIGHT,ALL_SIDES,FALSE]);
        }
        if(str=="start")
        {
           llSetPrimitiveParams([PRIM_FULLBRIGHT,ALL_SIDES,TRUE]);
        }
    }
}

One you have made these 3 prims and scripted the above, simply link them together into one prim (this can be done by holding down shift or alt and selecting the 3 prims) Once selected, press CTRL-L in order to fuse them into one prim and voila! A lightswitch.

No comments:

Post a Comment