Gun and Holster

From Second Life Wiki
Jump to: navigation, search

Example LSL Script - Gun and Holster

The following example script demonstrates the combined use of several LSL features. Among those are:

Purpose of the Script

A common feature provided with a Second Life weapon is the ability to "Draw" or "Holster" it. Typicaly a simple chat command, spoken on a specific channel, will either put the weapon in your hand, or back in the holster. (eg: "/33 draw" or "/33 holster") However, this is not accomplished by moving a Prim Attachment Point or any similar method, it is done by good old fashioned slight of hand.

When the command to "Draw" is spoken by the wearer, the weapon attached to the wearer's hand turns visible and the one in the holster turns invisible. Similarly, when the command to "Holster" is given, the weapon in the wearer's hand turns invisible and the one in the holster goes visible. This visual trick gives the appearance that the weapon has been either drawn into the hand, or put away in the holster.

The llSetAlpha() and llSetLinkAlpha() functions are called to make the object either visible or invisible. When the script is in the Root Prim of a multi-prim object, it will not only turn itself visible or invisible, it will also do the same to all of its Child Prims (by use of the llSetLinkAlpha() function). However, if the script is in the only prim of a single-prim object, or it is in a Child Prim of a multi-prim object, then it will only turn itself visible or invisible (with the llSetAlpha() function).

The weapon in the hand is often a multi-prim object that is itself scripted, usually to provide shooting and aiming abilities. The script presented here should be placed in the Root Prim of the weapon held in the hand, and then set the variable IsGun = TRUE;

The holstered weapon may be either a single-prim "dummy" or a multi-prim but non-functional replica. In the case of a single-prim dummy, the script should be placed in the only prim, and then set the variable IsGun = FALSE;

If the holstered weapon is one prim but is linked to the holster attached to the wearer, it should be linked as a Child Prim and not the Root Prim. This script should then be placed in the weapon prim and then set the variable IsGun = FALSE;

Finally, if the holstered weapon is a multi-prim object just for the weapon and not including the holster, you will have to edit this script to make it leave the holster prim (or prims) always visible, while changing the weapon prims from visible to invisible.

LSL Script

//
// Written and presented by Darrius Gothly 2009
// Free for any use, if it helps you, far out. If not, sorry.
//
 
integer IsGun       = TRUE;             // set to TRUE for Gun, FALSE for Holster
integer ChatChan    = 33;               // set to desired chat channel
 
string DrawCmd      = "draw";
string HolsterCmd   = "holster";        // set to whatever commands you want
 
integer IsVisible;
integer hListen;
 
default
{
    state_entry()
    {
    }
 
    attach(key id)
    {
        if (id != NULL_KEY) {
            hListen = llListen(ChatChan, "", id, "");
        } else {
            llListenRemove(hListen);
        }
    }
 
    listen(integer chan, string name, key id, string msg)
    {
        if (chan == ChatChan) {
            if (llToLower(msg) == llToLower(DrawCmd)) {
                if (IsGun) {
                    if (llGetLinkNumber() == LINK_ROOT)
                        llSetLinkAlpha(LINK_SET, 1.0, ALL_SIDES);
                    else
                        llSetAlpha(1.0, ALL_SIDES);
                } else {
                    if (llGetLinkNumber() == LINK_ROOT)
                        llSetLinkAlpha(LINK_SET, 0.0, ALL_SIDES);
                    else
                        llSetAlpha(0.0, ALL_SIDES);
                }
            } else if (llToLower(msg) == llToLower(HolsterCmd)) {
                if (IsGun) {
                    if (llGetLinkNumber() == LINK_ROOT)
                        llSetLinkAlpha(LINK_SET, 0.0, ALL_SIDES);
                    else
                        llSetAlpha(0.0, ALL_SIDES);
                } else {
                    if (llGetLinkNumber() == LINK_ROOT)
                        llSetLinkAlpha(LINK_SET, 1.0, ALL_SIDES);
                    else
                        llSetAlpha(1.0, ALL_SIDES);
                }
            }
        }
    }
}

Contributed by Darrius Gothly 2009