Returns True upon successful connection to the Region/Shard properties' storage. Region/Shard property operations may not be ready when used during initialization and this function ill help determine readiness.
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)
Sends a networked message to a specific user 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)
onRecieve(NetworkPropertyUpdate) will be called when a Region Property is updated.
NetworkPropertyUpdate = { Key: "property_name", Message: "property_value" }
local scriptedObject = Space.Host.ExecutingObject;local textValue = Space.Host.GetReference("Text");scriptedObject.SubscribeToEvents();-- This function gets used by other objects that are subscribed to the networkgotAMessage=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 the network.localupdateTextUI=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);scriptedObject.OnMouseDown(updateTextUI);-- "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.
SetRegionProperty
void SetRegionProperty (string key, string value)
Stores a key/value pair in the Regions semi-permanent memory. Will be erased when all players exit the region, but will persist as long as the region is open. Subject to rate limiting (10/second on the main grid and 20/second on white-label grids)
--this script is a networked and persistent color changing of an object--note that we put the object's GlobalID in the key to make it specific to this objectthisObject = Space.Host.ExecutingObjectcurrentColor =nilOnClick=function() --this writes to the region propertiesif currentColor == Color.Blue then Space.Network.SetRegionProperty(thisObject.GlobalID .."color", "red")else Space.Network.SetRegionProperty(thisObject.GlobalID .."color", "blue")endendSync=function() -- this gets from the region propertieswhiletruedolocal result = Space.Network.GetRegionProperty(thisObject.GlobalID .."color")if result =="red" then currentColor = Color.Red else currentColor = Color.Blueend thisObject.Renderer.Material.SetColor("_Color", currentColor) coroutine.yield(0.1) --to make sure it doesn't run more than 12/secondendendthisObject.AddClickable()thisObject.Clickable.OnClick(OnClick)Space.Host.StartCoroutine(Sync)
GetRegionProperty
string GetRegionProperty (string key)
Retrieves the last set value for 'key' in this region. If you have just joined the region, this may not be populated immedietely.
Parameter
Type
Description
result = Space.Network.GetRegionProperty("testKey")
--this script is a networked and persistent color changing of an object--note that we put the object's GlobalID in the key to make it specific to this objectthisObject = Space.Host.ExecutingObjectcurrentColor =nilOnClick=function() --this writes to the region propertiesif currentColor == Color.Blue then Space.Network.SetRegionProperty(thisObject.GlobalID .."color", "red")else Space.Network.SetRegionProperty(thisObject.GlobalID .."color", "blue")endendSync=function() -- this gets from the region propertieswhiletruedolocal result = Space.Network.GetRegionProperty(thisObject.GlobalID .."color")if result =="red" then currentColor = Color.Red else currentColor = Color.Blueend thisObject.Renderer.Material.SetColor("_Color", currentColor) coroutine.yield(0.1) --to make sure it doesn't run more than 12/secondendendthisObject.AddClickable()thisObject.Clickable.OnClick(OnClick)Space.Host.StartCoroutine(Sync)
SetShardProperty
void SetShardProperty (string key, string value)
Sets a property named 'key' of the Shard to 'value'. Will persist until the Shard is shut down or restarted. (Use SPersistence for longer term storage)
Parameter
Type
Description
local scriptedObject = Space.Host.ExecutingObject;scriptedObject.SubscribeToEvents();local testMsg ="Hello";localupdateTextUI=function() Space.Network.SetShardProperty("testKey", testMsg);endscriptedObject.OnMouseDown(updateTextUI);
GetShardProperty
string GetShardProperty (string key)
Gets a previously set key.
Parameter
Type
Description
local scriptedObject = Space.Host.ExecutingObject;scriptedObject.SubscribeToEvents();local textValue = Space.Host.GetReference("Text");local retrieved;localupdateTextUI=function() retrieved = Space.Network.GetShardProperty("testKey");end-- this method is called every frame, once "retrieved" gets a value, it displays in textlocalonUpdateMethod=function()updateTextUI();if retrieved ~=nilthen textValue.UIText.Text ="Retrieved msg: " .. retrieved ;endendscriptedObject.OnUpdate(onUpdateMethod);
Properties
HasShardProperties
bool HasShardPropertiesget
Returns True upon successful connection to the Region/Shard properties' storage. Region/Shard property operations may not be ready when used during initialization and this function ill help determine readiness.
IsReady = Space.Network.HasShardProperties
--This script starts but launches a coroutine that waits for HasShardProperties before continuingDoWhatYouNeed=function(result)if property ==nilor property =="" thenSpace.Log("connected but Shard doesn't exist")elseSpace.Log("connected and Shard exists. Result: " ..tostring(result))endendInitCoroutine=function()whilenot Space.Network.HasShardProperties docoroutine.yield(0) endlocal property = Space.Network.GetRegionProperty("A Key")DoWhatYouNeed(property)endSpace.Host.StartCoroutine(InitCoroutine)