Difference between revisions of "Gun Script"

From Second Life Wiki
Jump to navigation Jump to search
(Re-instated llGetCameraPos() and llGetCameraRot() which I'm advised is the preferred approach. Tested (!!). Sorry about the 8-column tabbing)
m (Replaced <source> with <syntaxhighlight>, placed it in the correct category etc.)
 
(2 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.    Corrected and improved.
{{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">
float  gVelocity  = 15.0;
float  gVelocity  = 15.0;
float  gReloadTime = 0.30;
float  gReloadTime = 0.30;
Line 11: Line 12:
default
default
{
{
state_entry()
    state_entry()
{
    {
if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {   // sanity check
        //  sanity check
llOwnerSay("This needs a physical object called " + gBullet + " in it to work");
        if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) {
return;
            llOwnerSay("This needs a physical object called " + gBullet + " in it to work");
}
            return;
gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
        }
        gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS | PERMISSION_TRACK_CAMERA;
        if ( llGetAttached() )
            llRequestPermissions(llGetOwner(), gPermFlags);
    }


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


attach(key id)
    changed(integer change)
{
    {
if (id)
        if (change & (CHANGED_OWNER | CHANGED_INVENTORY) )
llRequestPermissions(id, gPermFlags);
            llResetScript();
else
    }
{
llStopAnimation(gShootAnimation);
llReleaseControls();
}
}


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


run_time_permissions(integer perm)
    control(key id, integer held, integer change)
{
    {
if ( (perm & gPermFlags) == gPermFlags)  // ensure ALL required permissions have been granted
        rotation Rot = llGetCameraRot();
{
        if ( held & change & CONTROL_ML_LBUTTON)
llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
        {
llStartAnimation(gShootAnimation);
            if (llGetInventoryType(gShootSound) == INVENTORY_SOUND)
llOwnerSay("Gun is ready. Enter mouselook and use left click to fire!");
                llPlaySound(gShootSound, 1.0);
}
}
            llRezAtRoot(gBullet, llGetCameraPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10);
 
            llSleep(gReloadTime);
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);
}
}
}
}
</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);
        }
    }
}