wiki.sine.space | sinespace

Difference between revisions of "Scripting/SNetwork"

From wiki.sine.space
Jump to: navigation, search
Line 26: Line 26:
  
 
{{ScriptFunction|string|GetShardProperty|(string key);|Gets a previously set key.|5=local scriptedObject = Space.Host.ExecutingObject;<br>scriptedObject.SubscribeToEvents();<br><br>local textValue = Space.Host.GetReference("Text");<br>local retrieved;<br><br>local updateTextUI = function()<br>&nbsp;retrieved = Space.Network.GetShardProperty("testKey");<br>end<br><br>-- this method is called every frame, once "retrieved" gets a value, it displays in text<br>local onUpdateMethod = function()<br>&nbsp;updateTextUI();<br>&nbsp;if retrieved ~= nil then <br>&nbsp;&nbsp;&nbsp;textValue.UIText.Text = "Retrieved msg: " .. retrieved ;<br>&nbsp;end<br>end<br><br>scriptedObject.OnUpdate(onUpdateMethod);<br><br>-- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to the Object References section in your script.}}
 
{{ScriptFunction|string|GetShardProperty|(string key);|Gets a previously set key.|5=local scriptedObject = Space.Host.ExecutingObject;<br>scriptedObject.SubscribeToEvents();<br><br>local textValue = Space.Host.GetReference("Text");<br>local retrieved;<br><br>local updateTextUI = function()<br>&nbsp;retrieved = Space.Network.GetShardProperty("testKey");<br>end<br><br>-- this method is called every frame, once "retrieved" gets a value, it displays in text<br>local onUpdateMethod = function()<br>&nbsp;updateTextUI();<br>&nbsp;if retrieved ~= nil then <br>&nbsp;&nbsp;&nbsp;textValue.UIText.Text = "Retrieved msg: " .. retrieved ;<br>&nbsp;end<br>end<br><br>scriptedObject.OnUpdate(onUpdateMethod);<br><br>-- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to the Object References section in your script.}}
{{ScriptFunction|void|SubscribeToNetwork|(string key, Action<SNetworkMessage> callback);|Subscribes to network messages on 'key', will fire a [[Scripting/SNetworkMessage]] whenever a matching message is received|5=local scriptedObject = Space.Host.ExecutingObject;<br>scriptedObject.SubscribeToEvents();<br><br>local textValue = Space.Host.GetReference("Text");<br><br>-- This function gets used by other objects that are subscribed to the network<br>
+
{{ScriptFunction|void|SubscribeToNetwork|(string key, Action<SNetworkMessage> callback);|Subscribes to network messages on 'key', will fire a [[Scripting/SNetworkMessage]] whenever a matching message is received|5=local scriptedObject = Space.Host.ExecutingObject;<br>scriptedObject.SubscribeToEvents();<br><br>local textValue = Space.Host.GetReference("Text");<br><br>-- This function gets used by other objects that are subscribed to the network<br>gotAMessage = function(arguments)<br>  textValue.UIText.Text = "Got a message with the argument " .. arguments.Message["someArgument"];<br>end<br><br>-- This function gets used by "this" object. Plus, it sends out a message to objects on network.<br>local updateTextUI = function()<br>  textValue.UIText.Text = "Got a message with the argument Hi there!";<br>  Space.Network.SendNetworkMessage("helloworld", {someArgument = "Hi there!"});<br>end<br><br>-- Subscribe to network (note that "this" object doesn't execute gotAMessage / only the other objects on the network do<br>Space.Network.SubscribeToNetwork("helloworld", gotAMessage);<br><br>local onUpdateMethod = function()<br>  
gotAMessage = function(arguments)<br>  textValue.UIText.Text = "Got a message with the argument " .. arguments.Message["someArgument"];<br>end<br><br>-- This function gets used by "this" object. Plus, it sends out a message to objects on network.<br>local updateTextUI = function()<br>  textValue.UIText.Text = "Got a message with the argument Hi there!";<br>  Space.Network.SendNetworkMessage("helloworld", {someArgument = "Hi there!"});<br>end<br><br>-- Subscribe to network (note that "this" object doesn't execute gotAMessage / only the other objects on the network do<br>Space.Network.SubscribeToNetwork("helloworld", gotAMessage);<br><br>local onUpdateMethod = function()<br>  
+
 
  updateTextUI();<br>end<br><br>scriptedObject.OnMouseDown(onUpdateMethod);<br><br>-- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to the Object References section in your script.}}
 
  updateTextUI();<br>end<br><br>scriptedObject.OnMouseDown(onUpdateMethod);<br><br>-- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to the Object References section in your script.}}
  
  
 
{{Scripting Navbox}}
 
{{Scripting Navbox}}

Revision as of 17:01, 20 April 2017

The SNetwork class allows you to send messages to other users in the region, and persist/retrieve variables which have been stored within the region.

Members

SendNetworkMessage

void SendNetworkMessage (string key, IDictionary<object,object>);

Sends a networked message to every client with a subscriber listening on 'key'. The message itself can be a dictionary/table containing two columns and any network serializable type (string, float, int, byte, bool and arrays of those types)

This is a modified version of the example for *.Renderer.Material.SetTexture that demonstrates networking.

note that this example applies also to SubscribeToNetwork.

image = "mrlee.jpg"

server = "https://middleware.systems/"
obj = Space.Host.GetReference("Gino")

Space.Network.SubscribeToNetwork("smack", onSmack)

function hithere()

  resrc = Space.WebServices.GetImage(server .. image)
  obj.Renderer.Material.SetTexture("_MainTex", resrc)
  Space.Network.SendNetworkMessage("smack",{'smack'})

end

function update()

  resrc = Space.WebServices.GetImage(server .. "mrlee.jpg")
  obj.Renderer.Material.SetTexture("_MainTex", resrc)

end

function onSmack()

  update()
end


SetShardProperty

void SetShardProperty (string key, string value);

Sets a property named 'key' of the region to 'value'. Will persist until the region is shut down or restarted. (Use SPersistence for longer term storage)

local scriptedObject = Space.Host.ExecutingObject;
scriptedObject.SubscribeToEvents();

local testMsg = "Hello";
local updateTextUI = function()
 Space.Network.SetShardProperty("testKey", testMsg);
end

scriptedObject.OnMouseDown(updateTextUI);

-- Once this object is clicked, it will create a shard property with the key "testKey" and give it the value of "Hello".


GetShardProperty

string GetShardProperty (string key);

Gets a previously set key.

local scriptedObject = Space.Host.ExecutingObject;
scriptedObject.SubscribeToEvents();

local textValue = Space.Host.GetReference("Text");
local retrieved;

local updateTextUI = function()
 retrieved = Space.Network.GetShardProperty("testKey");
end

-- this method is called every frame, once "retrieved" gets a value, it displays in text
local onUpdateMethod = function()
 updateTextUI();
 if retrieved ~= nil then
   textValue.UIText.Text = "Retrieved msg: " .. retrieved ;
 end
end

scriptedObject.OnUpdate(onUpdateMethod);

-- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to the Object References section in your script.


SubscribeToNetwork

void SubscribeToNetwork (string key, Action<SNetworkMessage> callback);

Subscribes to network messages on 'key', will fire a Scripting/SNetworkMessage whenever a matching message is received

local scriptedObject = Space.Host.ExecutingObject;
scriptedObject.SubscribeToEvents();

local textValue = Space.Host.GetReference("Text");

-- This function gets used by other objects that are subscribed to the network
gotAMessage = function(arguments)
textValue.UIText.Text = "Got a message with the argument " .. arguments.Message["someArgument"];
end

-- This function gets used by "this" object. Plus, it sends out a message to objects on network.
local updateTextUI = function()
textValue.UIText.Text = "Got a message with the argument Hi there!";
Space.Network.SendNetworkMessage("helloworld", {someArgument = "Hi there!"});
end

-- Subscribe to network (note that "this" object doesn't execute gotAMessage / only the other objects on the network do
Space.Network.SubscribeToNetwork("helloworld", gotAMessage);

local onUpdateMethod = function()
updateTextUI();
end

scriptedObject.OnMouseDown(onUpdateMethod);

-- "Text" used with GetReference is a UItext canvas object used to display the test text inworld. After you add UI > Text to your scene, drag the text object to the Object References section in your script.