Difference between revisions of "LlMessageLinked"

From Second Life Wiki
Jump to navigation Jump to search
(Added snippet how to use list type parameter)
m (<lsl> tag to <source>)
 
(44 intermediate revisions by 19 users not shown)
Line 1: Line 1:
{{LSL Function/link}}{{LSL_Function
{{LSL_Function
|inject-2={{LSL Function/link|link|, controls which prim(s) receive the [[link_message]].|, controls which prim(s) receive the link_message.|verb=sends to}}
|func_id=164|func_sleep=0.0|func_energy=10.0
|func_id=164|func_sleep=0.0|func_energy=10.0
|func=llMessageLinked|sort=MessageLinked
|func=llMessageLinked|sort=MessageLinked
|p1_type=integer|p1_name=linknum|p1_desc=Link number or a LINK_* flag.
|p1_type=integer|p1_subtype=link|p1_name=link|p1_desc
|p2_type=integer|p2_name=num
|p2_type=integer|p2_name=num|p2_desc=Value of the second parameter of the resulting [[link_message]] [[event]].|p2_hover=Value of the second parameter of the resulting link_message event.
|p3_type=string|p3_name=str
|p3_type=string|p3_name=str|p3_desc=Value of the third parameter of the resulting [[link_message]] [[event]].|p3_hover=Value of the third parameter of the resulting link_message event.
|p4_type=key|p4_name=id
|p4_type=key|p4_name=id|p4_desc=Value of the fourth parameter of the resulting [[link_message]] [[event]].|p4_hover=Value of the fourth parameter of the resulting link_message event.
|func_desc=Triggers a {{LSLG|link_message}} event with the parameters '''num''', '''str''', and '''id''' in the group or link '''linknum'''.
|func_desc=The purpose of this function is to allow scripts in the same object to communicate. It triggers a [[link_message]] [[event]] with the same parameters {{LSLP|num}}, {{LSLP|str}}, and {{LSLP|id}} in all scripts in the prim(s) described by {{LSLP|link}}.
|func_footer=You can use '''id''' as a second string field (in LSL the [[key]] type is implemented as a [[string]] with just custom operators). Typecasting between string and key types has no effect on the data contained.
|func_footer=You can use {{LSLP|id}} as a second string field{{Footnote|In LSL the [[key]] type is implemented as a [[string]] (but with different operators and restrictions). [[typecast|Typecasting]] between string and key types has no effect on the data contained.}}. The sizes of {{LSLP|str}} and {{LSLP|id}} are only limited by available script memory.
|constants
|constants
|func_footnote
|func_footnote
|return_text
|return_text
|spec
|spec
|caveats=*A script can hear its own linked messages if '''linknum''' is set to '''LINK_SET''' or '''LINK_THIS'''. This creates the possibility of an infinite loop (a bad thing); be very careful about how messages are handled and passed along.
|caveats=*A script can hear its own linked messages if {{LSLP|link}} targets the prim it is in{{Footnote|There are four ways for a script to target itself: by [[llGetLinkNumber|precise link number]], [[LINK_THIS]], [[LINK_SET]] and [[LINK_ALL_CHILDREN]] (if the prim is a child prim).}}. This creates the possibility of an infinite loop (a bad thing); be very careful about how messages are handled and passed along.
|examples=<pre>
* Messages sent via [[llMessageLinked]] to a script that is [[llSleep|sleeping]], [[LSL Delay|delayed]], or [[lag|lagged]], are queued until the end of the delay. The [[event]] queue can hold 64 [[event|events]].
default{ // assumptions  // object name: LSLWiki // script name: _lslwiki
** If an [[event]] is received and the queue is full the [[event]] is silently dropped.
     state_entry() {
** Avoid sending link_messages to large numbers of scripts simultaneously as it can cause lag spike. This most often happens when using the multi-prim <code>LINK_*</code> flags and can cause script execution to slow or halts.
         llMessageLinked(LINK_THIS,0,llGetScriptName(), "")
** Avoid sending link_messages to a target faster than they can be handled. Doing so risks filling the [[event]] queue and subsequent messages being silently discarded.
* When a script [[state]] changes, all pending [[event|events]] are deleted, including queued link_messages.
* If {{LSLP|link}} is an invalid link number then the function silently fails.
* If {{LSLP|str}} & {{LSLP|id}} exceed the available memory of a script that catches the resulting [[link_message]] [[event]], that script will crash with a [[LSL_Errors#Script_run-time_error:_Stack-Heap_Collision|Stack-Heap Collision]].
|examples=<source lang="lsl2">default
{
    // assumptions  // object name: LSLWiki // script name: _lslwiki
     state_entry()
    {
         llMessageLinked(LINK_THIS, 0, llGetScriptName(), "");
     }
     }


     link_message(integer sender_num, integer num, string msg, key id) {
     link_message(integer sender_num, integer num, string msg, key id)
    {
         llOwnerSay(msg);
         llOwnerSay(msg);
         // the owner of object LSLWiki will hear
         // the owner of object LSLWiki will hear
         // LSLWiki:_lslwiki
         // LSLWiki:_lslwiki
     }  
     }
}</source>
 
=== Infinite Loop ===
 
<source lang="lsl2">Message_Control(integer l, integer n) // Message_Total_Lack_Of_Control
{
    integer r = (++n); // Increment the value of n.
    llMessageLinked( l, r, "", ""); // Send the result to l
}
}
</pre>
 
|helpers=<pre>
default
default
{ // Quick and dirty debugging link_messages
{
     link_message(integer sender_num, integer num, string msg, key id) {
    state_entry()
    {
        Message_Control(LINK_SET, 0); // Tell all the scripts in the object that we have state_entered.
    }
    link_message(integer Sender, integer Number, string String, key Key) // This script is in the object too.
    {
        Message_Control(Sender, Number); // No filtering condition exists.
        llOwnerSay(((string)Number)); // Look at all the pretty numbers!
    }
}</source>
|helpers=<source lang="lsl2">default
{
    // Quick and dirty debugging link_messages
     link_message(integer sender_num, integer num, string msg, key id)
    {
         llSay(DEBUG_CHANNEL, llList2CSV([sender_num, num, msg, id]));
         llSay(DEBUG_CHANNEL, llList2CSV([sender_num, num, msg, id]));
     }
     }
}
}</source>
</pre>
<source lang="lsl2">// This is just an example script, you shouldn't handle link message within single script this way.
<pre>
// This is just an example script, you shouldn't handle link message within single script this way.


default{ // To propagate an unlimted number of arguments of any type.
default
// Presumed, the separator string isn't used in any source string!
{
     state_entry() {  
    // To propagate an unlimted number of arguments of any type.
         list my_list = [1, 2.0, "a string", <1, 2, 3>, <1, 2, 3, 4>, llGetOwner()];
    // Presumed, the separator string isn't used in any source string!
         string list_parameter = llDumpList2String(my_list, "|"); // Typecast list to a string
     state_entry()
         llMessageLinked(LINK_THIS, 0, list_parameter, NULL_KEY)
    {
         list my_list = [1, 2.0, "a string", <1, 2, 3>, <1, 2, 3, 4>, llGetOwner()];
         string list_parameter = llDumpList2String(my_list, "|");   // Convert the list to a string
         llMessageLinked(LINK_THIS, 0, list_parameter, "");
     }
     }


     link_message(integer sender_num, integer num, string list_argument, key id) {
     link_message(integer sender_num, integer num, string list_argument, key id)
         list re_list = llParseString2List(list_argument, ["|"], [""]); // Typecast string back to a list
    {
     }  
         list re_list = llParseString2List(list_argument, ["|"], []);   // Parse the string back to a list
}
     }
</pre>
}</source>
|also_header
|also_header
|also_events=*{{LSLG|link_message}}
|also_events={{LSL DefineRow||[[link_message]]|}}
|also_functions
|also_functions
|also_articles
|also_articles
|also_footer
|also_footer
|notes=Using llMessageLinked in a single prim object allows developers to mitigate some LSL limits by breaking up functionality between cooperating scripts and synchronizing actions. When you do this, be extremely careful not to create infinite loops as mentioned above.
|notes=*Using [[llMessageLinked]] in a single prim object allows developers to mitigate some [[Limits#Scripting|LSL limits]] by breaking up functionality between cooperating scripts and synchronizing actions. When you do this, be extremely careful not to create infinite loops as mentioned above.
*Estimated <code>.25</code> to <code>.50</code> delay between receiving and sending of [[llMessageLinked]] has been observed by some users
*Some users have noted occasional failures of linked messages when sending a message to a large number of receiving scripts in different prims using [[LINK_SET]], [[LINK_ALL_OTHERS]], & [[LINK_ALL_CHILDREN]]. If you encounter this problem, a workaround is to place all child prim scripts in a single prim, using targeted functions like [[llSetLinkPrimitiveParams]] to modify the prim in which the script previously resided. -- [[User:Void Singer|Void Singer]]
*This function seems to create a lower lag level then [[llListen]] since it does not need a listener.
|cat1=Communications
|cat1=Communications
|cat2=Link
|cat2=Link

Latest revision as of 12:57, 22 January 2015

Summary

Function: llMessageLinked( integer link, integer num, string str, key id );
0.0 Forced Delay
10.0 Energy

The purpose of this function is to allow scripts in the same object to communicate. It triggers a link_message event with the same parameters num, str, and id in all scripts in the prim(s) described by link.

• integer link Link number (0: unlinked, 1: root prim, >1: child prims and seated avatars) or a LINK_* flag, controls which prim(s) receive the link_message.
• integer num Value of the second parameter of the resulting link_message event.
• string str Value of the third parameter of the resulting link_message event.
• key id Value of the fourth parameter of the resulting link_message event.

You can use id as a second string field[2]. The sizes of str and id are only limited by available script memory.

Flag Description
LINK_ROOT 1 sends to the root prim in a multi-prim linked set[1]
LINK_SET -1 sends to all prims
LINK_ALL_OTHERS -2 sends to all other prims
Flag Description
LINK_ALL_CHILDREN -3 sends to all children, (everything but the root)
LINK_THIS -4 sends to the prim the script is in

Caveats

  • A script can hear its own linked messages if link targets the prim it is in[3]. This creates the possibility of an infinite loop (a bad thing); be very careful about how messages are handled and passed along.
  • Messages sent via llMessageLinked to a script that is sleeping, delayed, or lagged, are queued until the end of the delay. The event queue can hold 64 events.
    • If an event is received and the queue is full the event is silently dropped.
    • Avoid sending link_messages to large numbers of scripts simultaneously as it can cause lag spike. This most often happens when using the multi-prim LINK_* flags and can cause script execution to slow or halts.
    • Avoid sending link_messages to a target faster than they can be handled. Doing so risks filling the event queue and subsequent messages being silently discarded.
  • When a script state changes, all pending events are deleted, including queued link_messages.
  • If link is an invalid link number then the function silently fails.
  • If str & id exceed the available memory of a script that catches the resulting link_message event, that script will crash with a Stack-Heap Collision.

Examples

default
{
    // assumptions  // object name: LSLWiki // script name: _lslwiki
    state_entry()
    {
        llMessageLinked(LINK_THIS, 0, llGetScriptName(), "");
    }

    link_message(integer sender_num, integer num, string msg, key id)
    {
        llOwnerSay(msg);
        // the owner of object LSLWiki will hear
        // LSLWiki:_lslwiki
    }
}

Infinite Loop

Message_Control(integer l, integer n) // Message_Total_Lack_Of_Control
{
    integer r = (++n); // Increment the value of n.
    llMessageLinked( l, r, "", ""); // Send the result to l
}

default
{
    state_entry()
    {
        Message_Control(LINK_SET, 0); // Tell all the scripts in the object that we have state_entered.
    }
    link_message(integer Sender, integer Number, string String, key Key) // This script is in the object too.
    {
        Message_Control(Sender, Number); // No filtering condition exists.
        llOwnerSay(((string)Number)); // Look at all the pretty numbers!
    }
}

Useful Snippets

default
{
    // Quick and dirty debugging link_messages
    link_message(integer sender_num, integer num, string msg, key id)
    {
        llSay(DEBUG_CHANNEL, llList2CSV([sender_num, num, msg, id]));
    }
}
// This is just an example script, you shouldn't handle link message within single script this way.

default
{
    // To propagate an unlimted number of arguments of any type.
    // Presumed, the separator string isn't used in any source string!
    state_entry()
    {
        list my_list = [1, 2.0, "a string", <1, 2, 3>, <1, 2, 3, 4>, llGetOwner()];
        string list_parameter = llDumpList2String(my_list, "|");    // Convert the list to a string
        llMessageLinked(LINK_THIS, 0, list_parameter, "");
    }

    link_message(integer sender_num, integer num, string list_argument, key id)
    {
        list re_list = llParseString2List(list_argument, ["|"], []);    // Parse the string back to a list
    }
}

Notes

Link Numbers

Each prim that makes up an object has an address, a link number. To access a specific prim in the object, the prim's link number must be known. In addition to prims having link numbers, avatars seated upon the object do as well.

  • If an object consists of only one prim, and there are no avatars seated upon it, the (root) prim's link number is zero.
  • However, if the object is made up of multiple prims or there is an avatar seated upon the object, the root prim's link number is one.

When an avatar sits on an object, it is added to the end of the link set and will have the largest link number. In addition to this, while an avatar is seated upon an object, the object is unable to link or unlink prims without unseating all avatars first.

Counting Prims & Avatars

There are two functions of interest when trying to find the number of prims and avatars on an object.

integer GetPrimCount() { //always returns only the number of prims
    if(llGetAttached())//Is it attached?
        return llGetNumberOfPrims();//returns avatars and prims but attachments can't be sat on.
    return llGetObjectPrimCount(llGetKey());//returns only prims but won't work on attachments.
}
See llGetNumberOfPrims for more about counting prims and avatars.

Errata

If a script located in a child prim erroneously attempts to access link 0, it will get or set the property of the linkset's root prim. This bug (BUG-5049) is preserved for broken legacy scripts.

  • Using llMessageLinked in a single prim object allows developers to mitigate some LSL limits by breaking up functionality between cooperating scripts and synchronizing actions. When you do this, be extremely careful not to create infinite loops as mentioned above.
  • Estimated .25 to .50 delay between receiving and sending of llMessageLinked has been observed by some users
  • Some users have noted occasional failures of linked messages when sending a message to a large number of receiving scripts in different prims using LINK_SET, LINK_ALL_OTHERS, & LINK_ALL_CHILDREN. If you encounter this problem, a workaround is to place all child prim scripts in a single prim, using targeted functions like llSetLinkPrimitiveParams to modify the prim in which the script previously resided. -- Void Singer
  • This function seems to create a lower lag level then llListen since it does not need a listener.

See Also

Events

•  link_message

Functions

•  llGetLinkNumber Returns the link number of the prim the script is in.

Deep Notes

Footnotes

  1. ^ LINK_ROOT does not work on single prim objects. Unless there is an avatar sitting on the object.
  2. ^ In LSL the key type is implemented as a string (but with different operators and restrictions). Typecasting between string and key types has no effect on the data contained.
  3. ^ There are four ways for a script to target itself: by precise link number, LINK_THIS, LINK_SET and LINK_ALL_CHILDREN (if the prim is a child prim).

Signature

function void llMessageLinked( integer link, integer num, string str, key id );