OSSL Script Library/ModSendCommand
From OpenSimulator
m (ModSendCommand moved to OSSL Script Library/ModSendCommand: Moving OSSL scripts to the library....) |
m (Robot: Cosmetic changes) |
||
Line 1: | Line 1: | ||
− | You can pass data back and forth between region modules and scripts using a script function called modSendCommand() which was introduced in OpenSim 0.6.9. | + | You can pass data back and forth between region modules and scripts using a script function called modSendCommand() which was introduced in OpenSim 0.6.9. This is an alternative to manually patching in new script functions. Here's an example of how to do this. |
== Enabling modSendCommand() == | == Enabling modSendCommand() == | ||
− | The first thing is to enable modSendCommand() in OpenSim.ini. | + | The first thing is to enable modSendCommand() in OpenSim.ini. Make sure the line |
<pre> | <pre> | ||
Line 69: | Line 69: | ||
</pre> | </pre> | ||
− | The module subscribes to the OnScriptCommand IScriptModuleComms event in PostInitialise(). | + | The module subscribes to the OnScriptCommand IScriptModuleComms event in PostInitialise(). When a script command event occurs, it checks that the module identifier is correct (MYMOD) and then splits the command into components delimited by a bar (|). This isn’t essential - it’s just one way of invoking arbitrary commands. |
− | In this case, the command name is the first component. | + | In this case, the command name is the first component. If it’s “Greet”, then a reply is sent back using the supplied name argument which is the second component. |
== The In-world Script == | == The In-world Script == | ||
Line 95: | Line 95: | ||
</pre> | </pre> | ||
− | As you can see, a touch triggers the command to the module. | + | As you can see, a touch triggers the command to the module. The script receives back the link_message event and says the data in-world. |
You can download compilable region module source and script code for this example from [http://justincc.org/downloads/src/ModSendCommandExample-1.0.zip here]. | You can download compilable region module source and script code for this example from [http://justincc.org/downloads/src/ModSendCommandExample-1.0.zip here]. | ||
Line 102: | Line 102: | ||
At the moment, this page is a simplified version of [http://justincc.org/blog/2010/07/16/passing-data-between-a-script-and-a-region-module-with-modsendcommand this blog post]. | At the moment, this page is a simplified version of [http://justincc.org/blog/2010/07/16/passing-data-between-a-script-and-a-region-module-with-modsendcommand this blog post]. | ||
− | |||
− | |||
[[Category:Scripts]] | [[Category:Scripts]] |
Revision as of 19:40, 3 March 2012
You can pass data back and forth between region modules and scripts using a script function called modSendCommand() which was introduced in OpenSim 0.6.9. This is an alternative to manually patching in new script functions. Here's an example of how to do this.
Contents |
Enabling modSendCommand()
The first thing is to enable modSendCommand() in OpenSim.ini. Make sure the line
AllowMODFunctions = true
is set to true and uncommented.
The Region Module
Secondly, we need a region module to listen for the data sent by modSendCommand() and send a reply.
using System; using Nini.Config; using OpenMetaverse; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; namespace ModSendCommandExample { public class MyRegionModule : IRegionModule { Scene m_scene; IScriptModuleComms m_commsMod; public string Name { get { return "MyRegionModule"; } } public bool IsSharedModule { get { return false; } } public void Close() {} public void Initialise(Scene scene, IConfigSource source) { m_scene = scene; } public void PostInitialise() { m_commsMod = m_scene.RequestModuleInterface<IScriptModuleComms>(); m_commsMod.OnScriptCommand += ProcessScriptCommand; } void ProcessScriptCommand( UUID scriptId, string reqId, string module, string input, string k) { if ("MYMOD" != module) return; string[] tokens = input.Split( new char[] { '|' }, StringSplitOptions.None); string command = tokens[0]; switch (command) { case "Greet": string name = tokens[1]; m_commsMod.DispatchReply(scriptId, 1, "Hello " + name, ""); break; } } } }
The module subscribes to the OnScriptCommand IScriptModuleComms event in PostInitialise(). When a script command event occurs, it checks that the module identifier is correct (MYMOD) and then splits the command into components delimited by a bar (|). This isn’t essential - it’s just one way of invoking arbitrary commands.
In this case, the command name is the first component. If it’s “Greet”, then a reply is sent back using the supplied name argument which is the second component.
The In-world Script
Here’s the in-world script that makes the call and receives the reply.
default { touch_start(integer total_number) { modSendCommand("MYMOD", "Greet|World", ""); } link_message(integer sender_num, integer num, string message, key id) { if (sender_num == -1) { llSay(0, message); } } }
As you can see, a touch triggers the command to the module. The script receives back the link_message event and says the data in-world.
You can download compilable region module source and script code for this example from here.
References
At the moment, this page is a simplified version of this blog post.