Difference between revisions of "Gun Script"
Omei Qunhua (talk | contribs) (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!) |
Omei Qunhua (talk | contribs) (Cleaned up. Tightened perms checking. Removed erroneous camera references. Added sanity checks. (With assistance from Innula Zenovka)) |
||
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. | ||
<lsl> | <lsl> | ||
float | float gVelocity = 15.0; | ||
float | float gReloadTime = 0.30; | ||
string | string gShootSound = "gun"; | ||
string | string gShootAnimation = "hold_R_bazooka"; | ||
string | string gBullet = "bullet 1.0"; | ||
integer | integer gPermFlags; | ||
default | default | ||
{ | { | ||
state_entry() | |||
{ | |||
if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) { // sanity check | |||
llOwnerSay("This needs a physical object called " + gBullet + " in it to work"); | |||
return; | |||
} | |||
gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS; | |||
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) | |||
{ | |||
if ( (perm & gPermFlags) == gPermFlags) // ensure ALL required permissions have been granted | |||
{ | |||
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 = llGetRot(); | |||
if ( held & change & CONTROL_ML_LBUTTON) | |||
{ | |||
if (llGetInventoryType(gShootSound) == INVENTORY_SOUND) | |||
llPlaySound(gShootSound, 1.0); | |||
llRezAtRoot(gBullet, llGetPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10); | |||
llSleep(gReloadTime); | |||
} | |||
} | |||
} | } | ||
</lsl> | </lsl> |
Revision as of 06:08, 8 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. Corrected and improved.
<lsl> 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() { if (llGetInventoryType(gBullet) != INVENTORY_OBJECT) { // sanity check llOwnerSay("This needs a physical object called " + gBullet + " in it to work"); return; } gPermFlags = PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS;
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) { if ( (perm & gPermFlags) == gPermFlags) // ensure ALL required permissions have been granted { 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 = llGetRot(); if ( held & change & CONTROL_ML_LBUTTON) { if (llGetInventoryType(gShootSound) == INVENTORY_SOUND) llPlaySound(gShootSound, 1.0);
llRezAtRoot(gBullet, llGetPos() + <1.5, 0.0, 0.0>*Rot, gVelocity*llRot2Fwd(Rot), Rot, 10); llSleep(gReloadTime); } } } </lsl>