Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to navigation Jump to search
(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!)
m (Replaced old <LSL> block with <source lang="lsl2">)
(3 intermediate revisions by 2 users not shown)
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.    Someone simplified this without saying so  and it doesn't work  i'm  looking for why.
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.    Corrected and improved.


<lsl>
<source lang="lsl2">
float bulletVelocity = 15.0;
float   gVelocity  = 15.0;
float pauseBetweenShots = 0.30;
float   gReloadTime = 0.30;
string shootingSound = "gun";
string gShootSound = "gun";
string shootingAnimation = "hold_R_bazooka";
string gShootAnimation = "hold_R_bazooka";
string inventoryItemWhichIsBullet = "bullet 1.0";
string gBullet = "bullet 1.0";
integer permFlags;
integer gPermFlags;


default
default
{
{
     on_rez(integer start_param)
     state_entry()
     {
     {
         if (!llGetAttached() )       llOwnerSay("Attach me to your right hand and enter mouselook to fire!");        
        //  sanity check
         else                        llPreloadSound(gunsound);
         if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {
            llOwnerSay("This needs a physical object called " + gBullet + " in it to work");
            return;
         }
        gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
        if ( llGetAttached() )
            llRequestPermissions(llGetOwner(), gPermFlags);
     }
     }


     attach(key id)
     attach(key id)
     {
     {
         if (id == NULL_KEY)
         if (id)
            llRequestPermissions(id, gPermFlags);
        else
         {
         {
             llStopAnimation(shootingAnimation);
             llStopAnimation(gShootAnimation);
             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))           llResetScript();
         if (change & (CHANGED_OWNER | CHANGED_INVENTORY) )
            llResetScript();
     }
     }


     run_time_permissions(integer perm)
     run_time_permissions(integer perm)
     {
     {
         if (perm & permFlags)
        //  ensure ALL required permissions have been granted
         if ( (perm & gPermFlags) == gPermFlags)
         {
         {
            llAttachToAvatar(ATTACH_RHAND);
             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
             llStartAnimation(shootingAnimation);
             llStartAnimation(gShootAnimation);
             llOwnerSay("Gun is ready. Enter mouselook to fire!");
             llOwnerSay("Gun is ready. Enter mouselook and use left click to fire!");
         }
         }
     }
     }


     control(key owner, integer level, integer edge)
     control(key id, integer held, integer change)
     {
     {
        vector cameraPos = llGetCameraPos();
         rotation Rot = llGetCameraRot();
         rotation cameraRot = llGetCameraRot();
         if ( held & change & CONTROL_ML_LBUTTON)
 
         if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
         {
         {
             if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
             if (llGetInventoryType(gShootSound) == INVENTORY_SOUND)
            {
                 llPlaySound(gShootSound, 1.0);
            //  play sound at max volume
                 llPlaySound(shootingSound, 1.0);
            llRezAtRoot(gBullet, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
 
            llSleep(gReloadTime);
                llRezAtRoot(inventoryItemWhichIsBullet,
                    cameraPos + <1.5, 0.0, 0.0>*cameraRot, bulletVelocity*llRot2Fwd(cameraRot), cameraRot, 10);
 
                llSleep(pauseBetweenShots);
            }
         }
         }
     }
     }
}
}
</lsl>
</source>

Revision as of 02:25, 22 January 2015

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. Corrected and improved.

float   gVelocity   = 15.0;
float   gReloadTime = 0.30;
string  gShootSound = "gun";
string  gShootAnimation = "hold_R_bazooka";
string  gBullet = "bullet 1.0";
integer gPermFlags;

default
{
    state_entry()
    {
        //  sanity check
        if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {
            llOwnerSay("This needs a physical object called " + gBullet + " in it to work");
            return;
        }
        gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
 
        if ( llGetAttached() )
            llRequestPermissions(llGetOwner(), gPermFlags);
    }

    attach(key id)
    {
        if (id)
            llRequestPermissions(id, gPermFlags);
        else
        {
            llStopAnimation(gShootAnimation);
            llReleaseControls();
        }
    }

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

    run_time_permissions(integer perm)
    {
        //  ensure ALL required permissions have been granted
        if ( (perm & gPermFlags) == gPermFlags)
        {
            llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
            llStartAnimation(gShootAnimation);
            llOwnerSay("Gun is ready. Enter mouselook and use left click to fire!");
        }
    }

    control(key id, integer held, integer change)
    {
        rotation Rot = llGetCameraRot();
        if ( held & change & CONTROL_ML_LBUTTON)
        {
            if (llGetInventoryType(gShootSound) == INVENTORY_SOUND)
                llPlaySound(gShootSound, 1.0);
 
            llRezAtRoot(gBullet, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
            llSleep(gReloadTime);
        }
    }
}