Difference between revisions of "User/Digit Ditko/experience hud basic script"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "{{TOCright}} == HUD Experience Detection == If you need a HUD that will activate and inactivate itself as an avatar moves about Second Life© then this example shows the...")
 
(Cleaned up code to be more closely aligned to the 'standard' style; fixed some typos.;and "promoted" it to LSL Examples!)
 
(4 intermediate revisions by one other user not shown)
Line 2: Line 2:


== HUD Experience Detection  ==
== HUD Experience Detection  ==
If you need a HUD that will activate and inactivate itself as an avatar moves about Second Life© then this example shows the minimum necessary to do that.   
If you need a HUD that will activate and inactivate itself as an avatar moves about Second Life® (and encounters [[:Category:LSL_Experience|Experiences]] during that travel), then this example shows the minimum necessary to do that.   


<source lang="lsl2">
<syntaxhighlight lang="lsl2">
// 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


// the color of the HUD for the active mode
// Simple function to change the HUD color.  
vector ACTIVE_COLOR = <0.0, 1.0, 0.0>;
 
// the color of the HUD in inactive mode
vector INACTIVE_COLOR = <1.0, 0.0, 0.0>;
 
 
// simple function to change the HUD color.  
setColor (vector inColor)
setColor (vector inColor)
{
{
     llSetLinkPrimitiveParamsFast (
     llSetLinkPrimitiveParamsFast(LINK_SET,
         LINK_SET, [PRIM_COLOR, ALL_SIDES, inColor, 1.0]);  
         [PRIM_COLOR, ALL_SIDES, incolor, 1.0]);  
}
}


// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Default state
// Default state
// The default state is the logical "inactive" state of the HUD.   
// The default state is the logical "inactive" state of the HUD.   
default
default
{
{
     state_entry ()
     state_entry()
     {
     {
         // for the initial setup of the HUD, just set the color to inactive.   
         // For the initial setup of the HUD, just set the color to inactive.   
         setColor (INACTIVE_COLOR);   
         setColor(INACTIVE_COLOR);   
          
          
         // after the setup is complete , determing is the HUD should go  
         // After the setup is complete, determine if the HUD should go  
         // to the active state.  If the script is NOT compiled to use an  
         // to the active state.  If the script is NOT compiled to use an  
         // experience, then the agent in experience API call will return false.   
         // Experience, then the Agent-in-the-Experience API call
         if (llAgentInExperience (llGetOwner ()))
        // will return false.   
         if (llAgentInExperience(llGetOwner()))
         {
         {
             state active;
             state active;
Line 43: Line 38:
     }
     }
      
      
   
     attach(key inAvatar)
     attach (key inAvatar)
     {
     {
         // if the HUD is beinf attached, check if the agent is in the experience
         // If the HUD is being attached, check if the agent is in the Experience
         // and swich to the active state.  
         // and switch to the active state.  
         if (inAvatar)
         if (inAvatar)
         {  
         {  
             if (llAgentInExperience (inAvatar))
             if (llAgentInExperience(inAvatar))
             {
             {
                 state active;
                 state active;
Line 57: Line 51:
     }
     }
      
      
     changed (integer inMask)
     changed(integer inMask)
     {   
     {   
         // if the owner changes or if the avatar goes to a different  
         // if the owner changes or if the avatar goes to a different  
         // region, chesk it the avatar is in the experience again and  
         // region, check it the avatar is in the Experience again and  
         // change to the active state if neeed.
         // change to the active state if needed.
         if (inMask & (CHANGED_REGION | CHANGED_OWNER))
         if (inMask & (CHANGED_REGION | CHANGED_OWNER))
         {
         {
             key av = llGetOwner ();
             key av = llGetOwner();
             if (llAgentInExperience (av))
             if (llAgentInExperience(av))
             {
             {
                 state active;
                 state active;
Line 77: Line 71:
// in the "active" state the HUD should have all standard functions.
// in the "active" state the HUD should have all standard functions.
// All this demo script does is turn the HUD to the active color.
// All this demo script does is turn the HUD to the active color.
state active
state active
{
{
     state_entry ()
     state_entry()
     {
     {
         // set the HUD color to active.   
         // Set the HUD color to active.   
         // This would usually be more elabotrate in a real script
         // This would usually be more elaborate in a real script.
         setColor (ACTIVE_COLOR);  
         setColor(ACTIVE_COLOR);  
     }
     }
      
      
   
     on_rez(integer inParam)
     on_rez (integer inParam)
     {
     {
         // When the object is trezzed, the state of the HUD is unknown.  
         // When the object is rezzed, the state of the HUD is unknown.  
         // Just change to the default state to run through the initialization  
         // Just change to the default state to run through the initialization  
         // logic as needed.
         // logic as needed.
         state default;   
         state default;   
     }
     }
   


     attach (key inAvatar)
     attach(key inAvatar)
     {
     {
         // when attached, check if the agent is in the experience and  
         // When attached, check if the agent is in the Experience and  
         // change to the default inactive state ias needed.
         // change to the default inactive state as needed.
         if (inAvatar)
         if (inAvatar)
         {  
         {  
             if (! llAgentInExperience (inAvatar))
             if (!llAgentInExperience(inAvatar))
             {
             {
                 state default;
                 state default;
Line 110: Line 101:
     }
     }
      
      
     changed (integer inMask)
     changed(integer inMask)
     {   
     {   
         // need to go through initialization logic if the owner changes or if  
         // Need to go through initialization logic if the owner changes or if  
         // the avatar goes to a different region.  NOTE: This does not detect
         // the avatar goes to a different region.  NOTE: This does not detect
         // individual parcel changes.     
         // individual parcel changes.     
         if (inMask & (CHANGED_REGION | CHANGED_OWNER))
         if (inMask & (CHANGED_REGION | CHANGED_OWNER))
         {
         {
             key av = llGetOwner ();
             key av = llGetOwner();
             if (! llAgentInExperience (av))
             if (!llAgentInExperience(av))
             {
             {
                 state default;
                 state default;
Line 125: Line 116:
     }
     }
}
}
 
</syntaxhighlight>
 
[[Category:LSL Examples]]
</source>

Latest revision as of 04:23, 5 May 2024

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 (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;
            }
        }      
    }
}