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

From Second Life Wiki
Jump to navigation Jump to search
(Cleaned up code to be more closely aligned to the 'standard' style; fixed some typos.;and "promoted" it to LSL Examples!)
(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)
 
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® (and encounters [[:Category:LSL_Experience|Experiences]] during that travel), 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.


<syntaxhighlight lang="lsl2">
<syntaxhighlight lang="lsl2">
Line 11: Line 11:
vector INACTIVE_COLOR = <1.0, 0.0, 0.0>;  // primary red
vector INACTIVE_COLOR = <1.0, 0.0, 0.0>;  // primary red


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


Line 25: Line 24:
     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, determine if 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-the-Experience API call
         // Experience, then the Agent-in-the-Experience API call
         // will return false.  
         // will return false.
         if (llAgentInExperience(llGetOwner()))
         if (llGetAttached() && llAgentInExperience(llGetOwner()))
         {
         {
             state active;
             state active;
         }
         }
     }
     }
      
      
Line 41: Line 40:
     {
     {
         // If the HUD is being attached, check if the agent is in the Experience
         // If the HUD is being attached, check if the agent is in the Experience
         // and switch to the active state.  
         // and switch to the active state.
         if (inAvatar)
         if (inAvatar)
         {  
         {
             if (llAgentInExperience(inAvatar))
             if (llAgentInExperience(inAvatar))
             {
             {
                 state active;
                 state active;
             }
             }
         }  
         }
     }
     }
      
      
     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, check 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 needed.
         // change to the active state if needed.
         if (inMask & (CHANGED_REGION | CHANGED_OWNER))
         if (inMask & (CHANGED_REGION | CHANGED_OWNER))
Line 63: Line 62:
                 state active;
                 state active;
             }
             }
         }    
         }
     }
     }
}
}
Line 75: Line 74:
     state_entry()
     state_entry()
     {
     {
         // Set the HUD color to active.
         // Set the HUD color to active.
         // This would usually be more elaborate 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 rezzed, 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 as needed.
         // change to the default inactive state as needed.
         if (inAvatar)
         if (inAvatar)
         {  
         {
             if (!llAgentInExperience(inAvatar))
             if (!llAgentInExperience(inAvatar))
             {
             {
                 state default;
                 state default;
             }
             }
         }  
         }
     }
     }
      
      
     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))
         {
         {
Line 113: Line 112:
                 state default;
                 state default;
             }
             }
         }    
         }
     }
     }
}
}
</syntaxhighlight>
</syntaxhighlight>
[[Category:LSL Examples]]
[[Category:LSL Examples]]

Latest revision as of 05:30, 11 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 (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;
            }
        }
    }
}