Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to: navigation, search
m (this revision added perm track camera)
Line 1: Line 1:
Uses tons of useful functions and a great example for those starting out. I've had tons of fun with this and all its variants i've made in my time here in Second Life. Obviously only can be used were you can rez and run scripts.  
+
Uses tons of useful functions and a great example for those starting out. I've had tons of fun with this and all its variants i've made in my time here in Second Life. Obviously only can be used were you can rez and run scripts.  
  
 +
<lsl>
 +
//A combination of defunct freebie scripts were used to make this none copyrighted to my knowledge at least.
 +
//Script improved and fixed by Shadow Siamendes.
  
 +
// variables to be changed below.
 +
float BULLET_VELOCITY = 15.0;      // change this to change the speed of the bullet.
 +
float REPEAT_DELAY = 0.30;        // delay between bullets, I recommend you don't set it too low.
 +
string gunsound = "gun";          // name of sound in inventory
 +
string gunanim = "hold_R_bazooka"; // name of anim in inventory
 +
string ammo = "bullet 1.0";        // name of desired object to be shot out. Must be in the inventory of the "gun".
 +
 +
//don't alter anything below if your not familiar with it.
 +
 +
//The main state is the default state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition can follow additional state definitions which the script may use to change how and which events are handled.
 +
default
 +
{
 +
// Triggered on any state transition and start up
 +
state_entry()
 +
{
 +
llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
 +
}
 +
 +
//Triggered when an agent grants run time permissions to task ,its auto grant when attached.
 +
run_time_permissions(integer perm)
 +
{
 +
if (perm)
 +
{
 +
llAttachToAvatar(ATTACH_RHAND);
 +
llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
 +
llStartAnimation(gunanim);
 +
llOwnerSay("Gun is ready. Enter mouselook to fire!");
 +
}
 +
}
 +
 +
// Triggered when an object is rezzed
 +
on_rez(integer st)
 +
{
 +
llOwnerSay("Attach me to your right hand, and enter mouselook to fire!");
 +
llPreloadSound(gunsound);
 +
llResetScript();
 +
}
 +
 +
// When the object is dettached by an avatar
 +
attach(key id)
 +
{
 +
if ( id != NULL_KEY)
 +
{
 +
llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
 +
}
 +
else if ( id == NULL_KEY)
 +
{
 +
llStopAnimation(gunanim);
 +
llReleaseControls();
 +
}
 +
}
 +
 +
// Checks if the ownership of the object was changed, if yes the script is reseted to ensure the premissions
 +
changed(integer change)
 +
{
 +
if(change & CHANGED_OWNER)
 +
{
 +
llResetScript();
 +
}
 +
}
 +
 +
control(key owner, integer level, integer edge)
 +
{
 +
if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
 +
{
 +
//  Mouse down
 +
if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
 +
{
 +
llPlaySound(gunsound,1.0);
 +
// Rezz the bullet object with a specified velocity
 +
llRezAtRoot(ammo,llGetCameraPos()+<1.5,0,0>*llGetCameraRot(), BULLET_VELOCITY *llRot2Fwd(llGetCameraRot()),llGetCameraRot(),10);
 +
llSleep(REPEAT_DELAY); // Pauses the script for a delay between bullets
 +
}
 +
}
 +
}
 +
}
 +
</lsl>
  
 +
NOTE: The script above is a fixed version of the script below. Follows the original version, just for purpose of knowledge about the changes and improvements. The touch event was removed in the script above (as you can compare with the script below) since it wasn't usefull on the most of cases.
  
 
<lsl>
 
<lsl>
Line 114: Line 195:
 
     }
 
     }
 
}
 
}
 +
</lsl>

Revision as of 06:46, 10 February 2012

Uses tons of useful functions and a great example for those starting out. I've had tons of fun with this and all its variants i've made in my time here in Second Life. Obviously only can be used were you can rez and run scripts.

<lsl> //A combination of defunct freebie scripts were used to make this none copyrighted to my knowledge at least. //Script improved and fixed by Shadow Siamendes.

// variables to be changed below. float BULLET_VELOCITY = 15.0; // change this to change the speed of the bullet. float REPEAT_DELAY = 0.30; // delay between bullets, I recommend you don't set it too low. string gunsound = "gun"; // name of sound in inventory string gunanim = "hold_R_bazooka"; // name of anim in inventory string ammo = "bullet 1.0"; // name of desired object to be shot out. Must be in the inventory of the "gun".

//don't alter anything below if your not familiar with it.

//The main state is the default state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition can follow additional state definitions which the script may use to change how and which events are handled. default { // Triggered on any state transition and start up state_entry() { llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA); }

//Triggered when an agent grants run time permissions to task ,its auto grant when attached. run_time_permissions(integer perm) { if (perm) { llAttachToAvatar(ATTACH_RHAND); llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE); llStartAnimation(gunanim); llOwnerSay("Gun is ready. Enter mouselook to fire!"); } }

// Triggered when an object is rezzed on_rez(integer st) { llOwnerSay("Attach me to your right hand, and enter mouselook to fire!"); llPreloadSound(gunsound); llResetScript(); }

// When the object is dettached by an avatar attach(key id) { if ( id != NULL_KEY) { llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA); } else if ( id == NULL_KEY) { llStopAnimation(gunanim); llReleaseControls(); } }

// Checks if the ownership of the object was changed, if yes the script is reseted to ensure the premissions changed(integer change) { if(change & CHANGED_OWNER) { llResetScript(); } }

control(key owner, integer level, integer edge) { if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON) { // Mouse down if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON) { llPlaySound(gunsound,1.0); // Rezz the bullet object with a specified velocity llRezAtRoot(ammo,llGetCameraPos()+<1.5,0,0>*llGetCameraRot(), BULLET_VELOCITY *llRot2Fwd(llGetCameraRot()),llGetCameraRot(),10); llSleep(REPEAT_DELAY); // Pauses the script for a delay between bullets } } } } </lsl>

NOTE: The script above is a fixed version of the script below. Follows the original version, just for purpose of knowledge about the changes and improvements. The touch event was removed in the script above (as you can compare with the script below) since it wasn't usefull on the most of cases.

<lsl>

//A combination of defunct freebie scripts were used to make this none copyrighted to my knowledge at least.



integer on = TRUE;



// variables to be changed below. float BULLET_VELOCITY = 15.0; // change this to change the speed of the bullet. float REPEAT_DELAY = 0.30; //delay between bullets, i recommend you dont' set it to low. string gunsound = "gun"; // string; name of sound in inventory string ammo = "bullet 1.0"; //name of desired object to be shot out. Must be in the inventory of the "gun". string message1 = "stop playing with mine and ask for yours"; //message when you touch it. string message2 = " Hey put a message here";

//dont' alter anything below if your not familiar with it.

default //The main state is the default state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition can follow additional state definitions which the script may use to change how and which events are handled. {

  state_entry()    // Triggered on any state transition and start up
    {
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
   

}

   run_time_permissions(integer perm)    //Triggered when an agent grants run time permissions to task ,its auto grant when attached.
   {
       if (perm)
       {
           llAttachToAvatar(ATTACH_RHAND);
           llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
           llStartAnimation("hold_R_bazooka");
       }
   }
   
  on_rez(integer st)  // Triggered when an object is rezzed

{


           llWhisper(0, "Attach me to your right hand, and enter mouselook to fire!");
            llPreloadSound(gunsound);

llResetScript();


       }
      touch_start(integer nd)   // Triggered by the start of agent clicking on object
       {
           if(on== TRUE)
     {
        llWhisper(0, message1);
       on= FALSE;
   }
   
   else if (on ==FALSE)
   {
       llWhisper(0, message2);
        on= TRUE;
       } 
      
          
       }
   
   
   attach(key id)    // When the object is dettached by an avatar 
   {
       if ( id != NULL_KEY)
       {
         
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);

}

   else if ( id == NULL_KEY)
       {
    llStopAnimation("hold_R_bazooka");
 llReleaseControls();
   }
    }   
    changed(integer change)
   {
           if(change & CHANGED_OWNER)
       {
  
   llResetScript();

} }

    control(key owner, integer level, integer edge)
   {
       if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
       {
           //  Mouse down
           if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
           { 
              
           
         
           llPlaySound("gun",1.0);
          llRezAtRoot(ammo,llGetCameraPos()+<1.5,0,0>*llGetCameraRot(), BULLET_VELOCITY *llRot2Fwd(llGetCameraRot()),llGetCameraRot(),10); 
           llSleep(REPEAT_DELAY);
       }
      
       }
   
   }

} </lsl>