Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to navigation Jump to search
(corrected definition of permFlags. The definition now done in on_rez cannot be done in the global area)
(Rewrote attach and on_rez events and removed llResetScript(). Assignment of computed perm mask to permFlags can't be done in global definitions. Pointless asking if it's attached to owner!)
Line 13: Line 13:
     on_rez(integer start_param)
     on_rez(integer start_param)
     {
     {
         permFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_ATTACH | PERMISSION_TRACK_CAMERA;
         if (!llGetAttached() )      llOwnerSay("Attach me to your right hand and enter mouselook to fire!");        
        llOwnerSay("Attach me to your right hand, and enter mouselook to fire!");
         else                        llPreloadSound(gunsound);
         llPreloadSound(gunsound);
        llResetScript();
     }
     }


     attach(key id)
     attach(key id)
     {
     {
         key owner = llGetOwner();
         if (id == NULL_KEY)
 
        if (id == owner)
            llRequestPermissions(owner, permFlags);
 
        else if (id == NULL_KEY)
         {
         {
             llStopAnimation(shootingAnimation);
             llStopAnimation(shootingAnimation);
             llReleaseControls();
             llReleaseControls();
         }
         }
        else    llRequestPermissions(llGetOwner(), (permFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_ATTACH | PERMISSION_TRACK_CAMERA) );
     }
     }


     changed(integer change)
     changed(integer change)
     {
     {
         if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
         if (change & (CHANGED_OWNER | CHANGED_INVENTORY))           llResetScript();
            llResetScript();
    }
 
    state_entry()
    {
        key owner = llGetOwner();
        llRequestPermissions(owner, permFlags);
     }
     }


Line 66: Line 53:
             {
             {
             //  play sound at max volume
             //  play sound at max volume
                 llPlaySound(shootingSound, (float)TRUE);
                 llPlaySound(shootingSound, 1.0);


                 llRezAtRoot(inventoryItemWhichIsBullet,
                 llRezAtRoot(inventoryItemWhichIsBullet,

Revision as of 14:21, 7 December 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. Someone simplified this without saying so and it doesn't work i'm looking for why.

<lsl> float bulletVelocity = 15.0; float pauseBetweenShots = 0.30; string shootingSound = "gun"; string shootingAnimation = "hold_R_bazooka"; string inventoryItemWhichIsBullet = "bullet 1.0"; integer permFlags;

default {

   on_rez(integer start_param)
   {
       if (!llGetAttached() )       llOwnerSay("Attach me to your right hand and enter mouselook to fire!");          
       else                         llPreloadSound(gunsound);
   }
   attach(key id)
   {
       if (id == NULL_KEY)
       {
           llStopAnimation(shootingAnimation);
           llReleaseControls();
       }
       else    llRequestPermissions(llGetOwner(), (permFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_ATTACH | PERMISSION_TRACK_CAMERA) );
   }
   changed(integer change)
   {
       if (change & (CHANGED_OWNER | CHANGED_INVENTORY))           llResetScript();
   }
   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, 1.0);
               llRezAtRoot(inventoryItemWhichIsBullet,
                   cameraPos + <1.5, 0.0, 0.0>*cameraRot, bulletVelocity*llRot2Fwd(cameraRot), cameraRot, 10);
               llSleep(pauseBetweenShots);
           }
       }
   }

} </lsl>