Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to navigation Jump to search
m (Replaced <source> with <syntaxhighlight>, placed it in the correct category etc.)
 
(8 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.  
{{LSL Header}}
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 where you can rez and run scripts.    Corrected and improved.


<lsl>
<syntaxhighlight lang="lsl2">
//A combination of defunct freebie scripts were used to make this none copyrighted to my knowledge at least.
float  gVelocity  = 15.0;
//Script improved and fixed by Shadow Siamendes.
float  gReloadTime = 0.30;
string  gShootSound = "gun";
string  gShootAnimation = "hold_R_bazooka";
string  gBullet = "bullet 1.0";
integer gPermFlags;


// 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
    state_entry()
state_entry()
    {
{
        // sanity check
llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
        if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {
}
            llOwnerSay("This needs a physical object called " + gBullet + " in it to work");
 
            return;
//Triggered when an agent grants run time permissions to task ,its auto grant when attached.
        }
run_time_permissions(integer perm)
        gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
{
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 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.
 
 
 
 
integer on = TRUE;  
 
   
   
        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. 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 (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)
        {
         
        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)
    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)
    llResetScript();
                llPlaySound(gShootSound, 1.0);
}
}
            llRezAtRoot(gBullet, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
    control(key owner, integer level, integer edge)
             llSleep(gReloadTime);
    {
        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>
</syntaxhighlight>
[[Category:LSL Library]]

Latest revision as of 02:40, 6 December 2023

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 where 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);
        }
    }
}