Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "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…")
 
m (Replaced old <LSL> block with <source lang="lsl2">)
(11 intermediate revisions by 4 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.  
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.


<source lang="lsl2">
float  gVelocity  = 15.0;
float  gReloadTime = 0.30;
string  gShootSound = "gun";
string  gShootAnimation = "hold_R_bazooka";
string  gBullet = "bullet 1.0";
integer gPermFlags;


 
default
 
{
<lsl>
    state_entry()
 
    {
// Based on phillip linden's gunscript with many refinements by rhonin nissondorf.
        //  sanity check
 
        if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {
 
            llOwnerSay("This needs a physical object called " + gBullet + " in it to work");
 
            return;
 
        }
integer on = TRUE;  
        gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
vector pos;
rotation rot; 
vector offset;  
   
   
        if ( llGetAttached() )
            llRequestPermissions(llGetOwner(), gPermFlags);
    }


 
     attach(key id)
// 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.
string message1 = "stop playing with mine and  ask for yours";
string message2 = " Hey put a message here"; //message when you touch it.
 
//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 startup
    {
        llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH);
   
 
}
   
     run_time_permissions(integer perm)   //Triggered when an agent grants run time permissions to task ,its auto grant when attached.
     {
     {
         if (perm)
         if (id)
            llRequestPermissions(id, gPermFlags);
        else
         {
         {
             llAttachToAvatar(ATTACH_RHAND);
             llStopAnimation(gShootAnimation);
             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
             llReleaseControls();
            llStartAnimation("hold_R_bazooka");
         }
         }
     }
     }
   
  on_rez(integer st)  // Triggered when an object is rezzed
{
               


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


            llWhisper(0, "Attach me to your right hand, and enter mouselook to fire!");
    run_time_permissions(integer perm)
            llPreloadSound(gunsound);
    {
llResetScript();
        //  ensure ALL required permissions have been granted
         
         if ( (perm & gPermFlags) == gPermFlags)
         
         }
      touch_start(integer nd)   // Triggered by the start of agent clicking on object
         {
         {
             if(on== TRUE)
             llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
      {
            llStartAnimation(gShootAnimation);
        llWhisper(0, message1);
            llOwnerSay("Gun is ready. Enter mouselook and use left click to fire!");
        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) 
        {
         
        llStopAnimation("hold_R_bazooka");
}         
     }
     }
       
 
     changed(integer change)   //Various changes to the object/prim trigger this event.
     control(key id, integer held, integer change)
     {
     {
            if(change & CHANGED_OWNER)
        rotation Rot = llGetCameraRot();
        if ( held & change & CONTROL_ML_LBUTTON)
         {
         {
 
            if (llGetInventoryType(gShootSound) == INVENTORY_SOUND)
    llStopAnimation("hold_R_bazooka");
                llPlaySound(gShootSound, 1.0);
    llResetScript();
   
}
             llRezAtRoot(gBullet, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
}
             llSleep(gReloadTime);
    control(key owner, integer level, integer edge) // Result of llTakeControls library function call and user input.
    {
        if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON) //level is when you click ,edge would be if you let up.
        {
         
             
           
            //  Fire 1 bullet,, the heart of the gun script.
             pos = llGetPos();
            rot = llGetRot();
            offset = <1.0, 0.0, 0.0>;
            offset *= rot;
            pos += offset;
            llPointAt(pos);
            vector fwd = llRot2Fwd(rot);
            fwd *= BULLET_VELOCITY;
            integer i = 5;
            rot *= llEuler2Rot(<0, PI_BY_TWO, 0>);
            llPlaySound(gunsound,1.0); // here "gunsound"is a variable defined above.
            llRezObject(ammo, pos, fwd, rot, 1); // here ammo is variable defined above.
             llSleep(REPEAT_DELAY); // ditto with  REPEAT_DELAY.
         }
         }
     
    }
        }
   
   
}
}
</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);
        }
    }
}