Difference between revisions of "Attach"

From Second Life Wiki
Jump to navigation Jump to search
m (Replaced <source> with <syntaxhighlight>; added haiku, generated with https://toolbaz.com/writer/haiku-generator and heavily edited)
 
(20 intermediate revisions by 10 users not shown)
Line 3: Line 3:
|event_delay
|event_delay
|event=attach
|event=attach
|inject-2={{Issues/SVC-6687}}
|p1_type=key|p1_name=id
|p1_type=key|p1_name=id
|p1_desc=the avatar if attached, if not attached {{LSL_Constant/NULL_KEY}}.
|p1_desc=the avatar if attached, if not attached {{LSL_Constant/NULL_KEY}}.
Line 8: Line 9:
|event_desc=Triggered in an object when the object [[attachment|attaches]] or detaches from agent.
|event_desc=Triggered in an object when the object [[attachment|attaches]] or detaches from agent.
|constants
|constants
|spec
|spec=
|caveats
===Triggered===
# When the object is attached to an avatar
#* From the ground
#* From inventory
#* When the avatar wearing the object logs in
# When object is detached ('''id''' == {{LSL_Constant/NULL_KEY}})
#* Dropped to the ground
#* Derezzed to inventory
#* <s>When the avatar wearing the object logs out</s> ''(does not occur with current versions, but scripters should anticipate the possibility)''
 
===Not Triggered===
# When the avatar wearing the object teleports
#* Use [[CHANGED_TELEPORT]] to detect when the avatar teleports
# When the avatar wearing the object moves from one region to another
#* Use [[CHANGED_REGION]] to detect when the avatar changes region
# When the attachment is detached after being attached with [[llAttachToAvatarTemp]].
#* There is no way to detect this at this moment.
|caveats=*When detached to inventory, an object is only given limited time to execute its attach events. If these events are active when the script derezzes but have not completed, execution will finish when the object is next rezzed. <sup>[http://forums-archive.secondlife.com/54/8b/65518/1.html#post689314]</sup> If the script is busy handling a ''different'' event at detach time, an attach event with id=NULL_KEY can happen on the next rez, followed by one with the wearer's key. To verify a real detach, check that id == NULL_KEY ''AND'' llGetAttached() == 0.
* If the script is busy with a complex event handler before it is taken to inventory, the [[:Category:LSL_Events|event queue]] can overflow, leaving no room to capture new events. The queue is preserved, so attach and on_rez may not be triggered the next time the object is rezzed. To guard against this possibility, [[llGetAttached]] can be checked in other events.
* When detaching an object, [[llGetAttached]]() returns 0, but the same happens when dropping an object from an attachment point to the ground. No other indicative event is triggered in the latter case; in particular, the [[on_rez]] event is not triggered for attachment drops. If you need to distinguish a drop from a detach, a possible hack is to check [[llGetObjectPrimCount]]([[llGetKey]]()). If it's zero, it can be assumed that the object is being detached; otherwise, that it is being dropped.
* There is a bug when an attach event is in a script in a child prim.  When the object is attached, a detached event is first triggered, and then an attached event is triggered.  When the object is detached, no event is triggered.
|examples=
|examples=
The following is a simplified example of the attach event. The variable id will be the key of the avatar the scripted object is attached to otherwise it will take on the value of {{LSL_Constant/NULL_KEY}}. The conditional [[if]] statement is used to determine the value of the variable id.
The following is a simplified example of the attach event. The variable id will be the key of the avatar the scripted object is attached to otherwise it will take on the value of {{LSL_Constant/NULL_KEY}}. The conditional [[if]] statement is used to determine the value of the variable id.
 
<syntaxhighlight lang="lsl2">
<lsl>default
default
{
{
     attach(key id)
     attach(key id)
     {
     {
         if(id)//tests if it is a valid key and not NULL_KEY
         if (id)     // is a valid key and not NULL_KEY
         {
         {
             llSay(PUBLIC_CHANNEL,"I have been attached!");
             llSay(0, "I have been attached!");
         }
         }
         else
         else
         {
         {
             llSay(PUBLIC_CHANNEL,"I have been detached!");
             llSay(0, "I have been detached!");
         }
         }
     }
     }
}</lsl>
}
</syntaxhighlight>
|helpers
|helpers
|also_header
|also_header
|also_events
|also_events=
{{LSL DefineRow|[[on_rez]]}}
|also_functions=
|also_functions=
{{LSL DefineRow|[[llAttachToAvatar]]}}
{{LSL DefineRow|[[llAttachToAvatar]]}}
Line 37: Line 60:
{{LSL DefineRow|{{LSLGC|Attachment}}}}
{{LSL DefineRow|{{LSLGC|Attachment}}}}
|also_footer
|also_footer
|notes
|notes=
===[[on_rez]] & [[attach]]===
[[on_rez]] will be triggered prior to [[attach]] when attaching from inventory or during login.
|haiku={{Haiku|A nightmare I had:|The box on my head I wear|Can't escape that dream.}}
|mode
|mode
|deprecated
|deprecated

Latest revision as of 08:11, 12 October 2023

Description

! Event: attach( key id ){ ; }

Triggered in an object when the object attaches or detaches from agent.

• key id the avatar if attached, if not attached NULL_KEY.

Specification

Triggered

  1. When the object is attached to an avatar
    • From the ground
    • From inventory
    • When the avatar wearing the object logs in
  2. When object is detached (id == NULL_KEY)
    • Dropped to the ground
    • Derezzed to inventory
    • When the avatar wearing the object logs out (does not occur with current versions, but scripters should anticipate the possibility)

Not Triggered

  1. When the avatar wearing the object teleports
  2. When the avatar wearing the object moves from one region to another
  3. When the attachment is detached after being attached with llAttachToAvatarTemp.
    • There is no way to detect this at this moment.

Caveats

  • When detached to inventory, an object is only given limited time to execute its attach events. If these events are active when the script derezzes but have not completed, execution will finish when the object is next rezzed. [1] If the script is busy handling a different event at detach time, an attach event with id=NULL_KEY can happen on the next rez, followed by one with the wearer's key. To verify a real detach, check that id == NULL_KEY AND llGetAttached() == 0.
  • If the script is busy with a complex event handler before it is taken to inventory, the event queue can overflow, leaving no room to capture new events. The queue is preserved, so attach and on_rez may not be triggered the next time the object is rezzed. To guard against this possibility, llGetAttached can be checked in other events.
  • When detaching an object, llGetAttached() returns 0, but the same happens when dropping an object from an attachment point to the ground. No other indicative event is triggered in the latter case; in particular, the on_rez event is not triggered for attachment drops. If you need to distinguish a drop from a detach, a possible hack is to check llGetObjectPrimCount(llGetKey()). If it's zero, it can be assumed that the object is being detached; otherwise, that it is being dropped.
  • There is a bug when an attach event is in a script in a child prim. When the object is attached, a detached event is first triggered, and then an attached event is triggered. When the object is detached, no event is triggered.


Examples

The following is a simplified example of the attach event. The variable id will be the key of the avatar the scripted object is attached to otherwise it will take on the value of NULL_KEY. The conditional if statement is used to determine the value of the variable id.

default
{
    attach(key id)
    {
        if (id)     // is a valid key and not NULL_KEY
        {
            llSay(0, "I have been attached!");
        }
        else
        {
            llSay(0, "I have been detached!");
        }
    }
}

Notes

on_rez & attach

on_rez will be triggered prior to attach when attaching from inventory or during login.

See Also

Events

• on_rez

Functions

• llAttachToAvatar
• llDetachFromAvatar
• llGetAttached

Articles

• Attachment

Deep Notes

Issues

All Issues

~ Search JIRA for related Issues
   Attach event doesn't trigger in a child-prim when detaching the object

Signature

event void attach( key id );

Haiku

A nightmare I had:
The box on my head I wear
Can't escape that dream.