虚拟世界/脚本间通信

来自人工智能助力教育知识百科
Wyg讨论 | 贡献2024年8月23日 (五) 08:46的版本 (创建页面,内容为“{{VWSnav}} ===llMessageLinked( integer link, integer num, string str, key id )=== 该脚本目的是允许一个物体中脚本之间进行通信。它将触发link…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

百科首页 - 3D虚拟世界 - 音乐与人工智能 - 人工智能机器人 - 知识百科 - 关于我们 - 网站首页

脚本首页 | Vehicles | NPC | HTTP | 脚本间通信 | 翻译参考

llMessageLinked( integer link, integer num, string str, key id )

该脚本目的是允许一个物体中脚本之间进行通信。它将触发link_message事件,事件相关参数num、str、id相同,捕获到的元件由link参数决定。

  1. 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.
  2. integer num: Value of the second parameter of the resulting link_message event.
  3. string str: Value of the third parameter of the resulting link_message event.
  4. key id: Value of the fourth parameter of the resulting link_message event.
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
LINK_ALL_CHILDREN	-3	sends to all children, (everything but the root)
LINK_THIS	-4	sends to the prim the script is in
  • 警告
  1. 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.
  2. 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.
    1. If an event is received and the queue is full the event is silently dropped.
    2. 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.
    3. 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.
  3. When a script state changes, all pending events are deleted, including queued link_messages.
  4. If link is an invalid link number then the function silently fails.
  5. 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.
  • 例子
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
    }
}

无穷循环,注意只用于测试功能。

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!
    }
}

有用的代码片段

default
{
    // 在debug窗口输出,捕获link message事件
    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
{
    // 演示如何传输多个参数,放到list中!
    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
    }
}