wiki.sine.space | sinespace

Difference between revisions of "Scripting/SNetwork"

From wiki.sine.space
Jump to: navigation, search
Line 26: Line 26:
 
scriptedObject.SubscribeToEvents();
 
scriptedObject.SubscribeToEvents();
  
local testMsg = "yo";
+
local testMsg = "Hello";
  
 
local updateTextUI = function()
 
local updateTextUI = function()
     Space.Network.SetShardProperty("testProp", testMsg);
+
     Space.Network.SetShardProperty("testKey", testMsg);
 
end
 
end
  
scriptedObject.OnMouseDown(updateTextUI);}}
+
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".}}
 +
 
 
{{ScriptFunction|string|GetShardProperty|(string key);|Gets a previously set key.}}
 
{{ScriptFunction|string|GetShardProperty|(string key);|Gets a previously set key.}}
 
{{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}}
 
{{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}}

Revision as of 05:22, 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.

No example provided yet


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

No example provided yet