User/Digit Ditko/experience hud basic script

From Second Life Wiki
< User
Revision as of 05:30, 11 May 2024 by Nexii Malthus (talk | contribs) (Also check if it is attached on initialisation -- this is optional but it might be on the ground and being edited and having a HUD running on the ground can be awkward)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

HUD Experience Detection

If you need a HUD that will activate and inactivate itself as an avatar moves about Second Life® (and encounters Experiences during that travel), then this example shows the minimum necessary to do that.

// The color of the HUD for the active mode.
vector ACTIVE_COLOR = <0.0, 1.0, 0.0>;    // primary green

// The color of the HUD in inactive mode.
vector INACTIVE_COLOR = <1.0, 0.0, 0.0>;  // primary red

// Simple function to change the HUD color.
setColor (vector inColor)
{
    llSetLinkPrimitiveParamsFast(LINK_SET, [PRIM_COLOR, ALL_SIDES, inColor, 1.0]);
}

// -----------------------------------------------------------------------------
// Default state
// The default state is the logical "inactive" state of the HUD.  
default
{
    state_entry()
    {
        // For the initial setup of the HUD, just set the color to inactive.
        setColor(INACTIVE_COLOR);
        
        // After the setup is complete, determine if the HUD should go
        // to the active state.  If the script is NOT compiled to use an
        // Experience, then the Agent-in-the-Experience API call
        // will return false.
        if (llGetAttached() && llAgentInExperience(llGetOwner()))
        {
            state active;
        }
    }
    
    attach(key inAvatar)
    {
        // If the HUD is being attached, check if the agent is in the Experience
        // and switch to the active state.
        if (inAvatar)
        {
            if (llAgentInExperience(inAvatar))
            {
                state active;
            }
        }
    }
    
    changed(integer inMask)
    {
        // if the owner changes or if the avatar goes to a different
        // region, check it the avatar is in the Experience again and
        // change to the active state if needed.
        if (inMask & (CHANGED_REGION | CHANGED_OWNER))
        {
            key av = llGetOwner();
            if (llAgentInExperience(av))
            {
                state active;
            }
        }
    }
}


// --------------------------------------------------------------------------
// in the "active" state the HUD should have all standard functions.
// All this demo script does is turn the HUD to the active color.
state active
{
    state_entry()
    {
        // Set the HUD color to active.
        // This would usually be more elaborate in a real script.
        setColor(ACTIVE_COLOR);
    }
    
    on_rez(integer inParam)
    {
        // When the object is rezzed, the state of the HUD is unknown.
        // Just change to the default state to run through the initialization
        // logic as needed.
        state default;
    }
    
    attach(key inAvatar)
    {
        // When attached, check if the agent is in the Experience and
        // change to the default inactive state as needed.
        if (inAvatar)
        {
            if (!llAgentInExperience(inAvatar))
            {
                state default;
            }
        }
    }
    
    changed(integer inMask)
    {
        // Need to go through initialization logic if the owner changes or if
        // the avatar goes to a different region.  NOTE: This does not detect
        // individual parcel changes.
        if (inMask & (CHANGED_REGION | CHANGED_OWNER))
        {
            key av = llGetOwner();
            if (!llAgentInExperience(av))
            {
                state default;
            }
        }
    }
}