Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to navigation Jump to search
m (improved readability)
Line 2: Line 2:


<lsl>
<lsl>
//A combination of defunct freebie scripts were used to make this none copyrighted to my knowledge at least.
float bulletVelocity = 15.0;
//Script improved and fixed by Shadow Siamendes.
float pauseBetweenShots = 0.30;
string shootingSound = "gun";
string shootingAnimation = "hold_R_bazooka";
string inventoryItemWhichIsBullet = "bullet 1.0";
integer permFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_ATTACH | PERMISSION_TRACK_CAMERA;


// 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
default
{
{
// Triggered on any state transition and start up
    on_rez(integer start_param)
state_entry()
    {
{
        llOwnerSay("Attach me to your right hand, and enter mouselook to fire!");
llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
        llPreloadSound(gunsound);
}
        llResetScript();
    }


//Triggered when an agent grants run time permissions to task ,its auto grant when attached.
    attach(key id)
run_time_permissions(integer perm)
    {
{
        key owner = llGetOwner();
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
        if (id == owner)
on_rez(integer st)
            llRequestPermissions(owner, permFlags);
{
llOwnerSay("Attach me to your right hand, and enter mouselook to fire!");
llPreloadSound(gunsound);
llResetScript();
}


// When the object is dettached by an avatar
        else if (id == NULL_KEY)
attach(key id)
        {
{
            llStopAnimation(shootingAnimation);
if ( id != NULL_KEY)
            llReleaseControls();
{
        }
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 permissions
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.


    changed(integer change)
    {
        if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
            llResetScript();
    }


    state_entry()
    {
        key owner = llGetOwner();
        llRequestPermissions(owner, permFlags);
    }


 
     run_time_permissions(integer perm)
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)
         if (perm & permFlags)
         {
         {
             llAttachToAvatar(ATTACH_RHAND);
             llAttachToAvatar(ATTACH_RHAND);
             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
             llStartAnimation("hold_R_bazooka");
             llStartAnimation(shootingAnimation);
            llOwnerSay("Gun is ready. Enter mouselook to fire!");
         }
         }
     }
     }
   
  on_rez(integer st)  // Triggered when an object is rezzed
{
               


    control(key owner, integer level, integer edge)
    {
        vector cameraPos = llGetCameraPos();
        rotation cameraRot = llGetCameraRot();


            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)
         if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
         {
         {
            //  Mouse down
             if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
             if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
             {  
             {
             
             //  play sound at max volume
              
                llPlaySound(shootingSound, (float)TRUE);
         
 
            llPlaySound("gun",1.0);
                llRezAtRoot(inventoryItemWhichIsBullet,
          llRezAtRoot(ammo,llGetCameraPos()+<1.5,0,0>*llGetCameraRot(), BULLET_VELOCITY *llRot2Fwd(llGetCameraRot()),llGetCameraRot(),10);  
                    cameraPos + <1.5, 0.0, 0.0>*cameraRot, bulletVelocity*llRot2Fwd(cameraRot), cameraRot, 10);
            llSleep(REPEAT_DELAY);
 
        }
                llSleep(pauseBetweenShots);
     
            }
         }
         }
   
     }
     }
}
}
</lsl>
</lsl>

Revision as of 12:01, 18 October 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> float bulletVelocity = 15.0; float pauseBetweenShots = 0.30; string shootingSound = "gun"; string shootingAnimation = "hold_R_bazooka"; string inventoryItemWhichIsBullet = "bullet 1.0"; integer permFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_ATTACH | PERMISSION_TRACK_CAMERA;

default {

   on_rez(integer start_param)
   {
       llOwnerSay("Attach me to your right hand, and enter mouselook to fire!");
       llPreloadSound(gunsound);
       llResetScript();
   }
   attach(key id)
   {
       key owner = llGetOwner();
       if (id == owner)
           llRequestPermissions(owner, permFlags);
       else if (id == NULL_KEY)
       {
           llStopAnimation(shootingAnimation);
           llReleaseControls();
       }
   }
   changed(integer change)
   {
       if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
           llResetScript();
   }
   state_entry()
   {
       key owner = llGetOwner();
       llRequestPermissions(owner, permFlags);
   }
   run_time_permissions(integer perm)
   {
       if (perm & permFlags)
       {
           llAttachToAvatar(ATTACH_RHAND);
           llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
           llStartAnimation(shootingAnimation);
           llOwnerSay("Gun is ready. Enter mouselook to fire!");
       }
   }
   control(key owner, integer level, integer edge)
   {
       vector cameraPos = llGetCameraPos();
       rotation cameraRot = llGetCameraRot();
       if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
       {
           if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
           {
           //  play sound at max volume
               llPlaySound(shootingSound, (float)TRUE);
               llRezAtRoot(inventoryItemWhichIsBullet,
                   cameraPos + <1.5, 0.0, 0.0>*cameraRot, bulletVelocity*llRot2Fwd(cameraRot), cameraRot, 10);
               llSleep(pauseBetweenShots);
           }
       }
   }

} </lsl>