Difference between revisions of "User/Digit Ditko/experience hud basic script"
< User
Jump to navigation
Jump to search
Digit Ditko (talk | contribs) |
(Cleaned up code to be more closely aligned to the 'standard' style; fixed some typos.;and "promoted" it to LSL Examples!) |
||
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 | 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"> | |||
// 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) | setColor (vector inColor) | ||
{ | { | ||
llSetLinkPrimitiveParamsFast ( | llSetLinkPrimitiveParamsFast(LINK_SET, | ||
[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. | ||
setColor (INACTIVE_COLOR); | 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 | // to the active state. If the script is NOT compiled to use an | ||
// | // Experience, then the Agent-in-the-Experience API call | ||
if (llAgentInExperience (llGetOwner ())) | // will return false. | ||
if (llAgentInExperience(llGetOwner())) | |||
{ | { | ||
state active; | state active; | ||
Line 45: | Line 38: | ||
} | } | ||
attach(key inAvatar) | |||
attach (key inAvatar) | |||
{ | { | ||
// | // If the HUD is being attached, check if the agent is in the Experience | ||
// and | // and switch to the active state. | ||
if (inAvatar) | if (inAvatar) | ||
{ | { | ||
if (llAgentInExperience (inAvatar)) | if (llAgentInExperience(inAvatar)) | ||
{ | { | ||
state active; | state active; | ||
Line 59: | 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, | // region, check it the avatar is in the Experience again and | ||
// change to the active state if | // 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 79: | 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. | ||
// This would usually be more | // 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 | // 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 | ||
// change to the default inactive state | // change to the default inactive state as needed. | ||
if (inAvatar) | if (inAvatar) | ||
{ | { | ||
if (! llAgentInExperience (inAvatar)) | if (!llAgentInExperience(inAvatar)) | ||
{ | { | ||
state default; | state default; | ||
Line 112: | Line 101: | ||
} | } | ||
changed (integer inMask) | 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 | // 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 127: | Line 116: | ||
} | } | ||
} | } | ||
</syntaxhighlight> | |||
[[Category:LSL Examples]] | |||
</ |
Revision as of 03: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;
}
}
}
}