wiki.sine.space | sinespace

Difference between revisions of "Scripting/SShared"

From wiki.sine.space
Jump to: navigation, search
(Replaced content with "This page has moved to: https://docs.sine.space/v/scripting/client-scripting/viewer/sshared")
 
Line 1: Line 1:
The SShared class is for communication between scripts and objects within a client. For communication needed between different clients/players please use [[Scripting/SNetwork]].
+
This page has moved to: https://docs.sine.space/v/scripting/client-scripting/viewer/sshared
 
+
=Members=
+
{{ScriptFunction|void|SetGlobal|(string namespace, string key, DynValue value);|Sets a global key to a value. The value can be any object type.|5=
+
'''Space.Shared.SetGlobal'''("com.someNameHere.world", "version", "1.02"); |6=<pre>-- This one script placed on a multiple number of objects  will track the number
+
-- of clicks user has made using a Global Variable
+
-- note: the GetGlobal/SetGlobal functions are client side
+
 
+
thisObject = Space.Host.ExecutingObject
+
namespace = "com.example.shared"
+
key = "clicktracker"
+
 
+
 
+
OnClick = function()
+
 
+
local currentClicks = Space.Shared.GetGlobal(namespace, key)
+
 
+
if currentClicks == nil then
+
  currentClicks = 1
+
else
+
  currentClicks = currentClicks + 1
+
end
+
 
+
Space.Shared.SetGlobal(namespace, key, currentClicks)
+
Space.Log("Total Clicks = " .. currentClicks)
+
 
+
end
+
 
+
 
+
thisObject.AddClickable()
+
thisObject.Clickable.OnClick(OnClick)</pre>}}
+
 
+
{{ScriptFunction|DynValue|GetGlobal|(string namespace, string key);|Retrieves a previously set global variable, or returns nil.|5=
+
local versionValue = '''Space.Shared.GetGlobal'''("com.someNameHere.world", "version")|6=<pre>-- This one script placed on a multiple number of objects  will track the number
+
-- of clicks user has made using a Global Variable
+
-- note: the GetGlobal/SetGlobal functions are client side
+
 
+
thisObject = Space.Host.ExecutingObject
+
namespace = "com.example.shared"
+
key = "clicktracker"
+
 
+
 
+
OnClick = function()
+
 
+
local currentClicks = Space.Shared.GetGlobal(namespace, key)
+
 
+
if currentClicks == nil then
+
  currentClicks = 1
+
else
+
  currentClicks = currentClicks + 1
+
end
+
 
+
Space.Shared.SetGlobal(namespace, key, currentClicks)
+
Space.Log("Total Clicks = " .. currentClicks)
+
 
+
end
+
 
+
 
+
thisObject.AddClickable()
+
thisObject.Clickable.OnClick(OnClick)</pre>}}
+
 
+
{{ScriptFunction|void|RegisterFunction|(string ns, string func, Closure reference);|Makes func into a global function that can be accessed anywhere.|5=
+
function someFunction(name)
+
&nbsp;&nbsp;Space.Log("Hello " .. name);<br>
+
end<br><br>
+
 
+
'''Space.Shared.RegisterFunction'''("com.someNameHere.world", "func", someFunction);
+
|6=<pre>--Script placed on object A will allow other objects to call one of it's registered functions when clicked
+
 
+
--Script in Object A
+
thisObject = Space.Host.ExecutingObject
+
namespace = "com.example.shared"
+
 
+
LogFunction = function(Parameter)
+
Space.Log("I've been called with parameter: " .. Parameter) 
+
end
+
 
+
Space.Shared.RegisterFunction(namespace, "Log", LogFunction)
+
 
+
 
+
 
+
-- Script in Other Objects
+
thisObject = Space.Host.ExecutingObject
+
namespace = "com.example.shared"
+
 
+
OnClick = function()
+
Space.Shared.CallFunction(namespace, "Log", {"Example"})
+
end
+
 
+
thisObject.AddClickable()
+
thisObject.Clickable.OnClick(OnClick)</pre>}}
+
 
+
{{ScriptFunction|void|CallFunction|(string ns, string func, IEnumerable< DynValue > args);|Calls the registered function with the specified arguments.|5=
+
'''Space.Shared.CallFunction'''("com.someNameHere.world", "func",{"Smith"}); |6=<pre>--Script placed on object A will allow other objects to call one of it's registered functions when clicked
+
 
+
--Script in Object A
+
thisObject = Space.Host.ExecutingObject
+
namespace = "com.example.shared"
+
 
+
LogFunction = function(Parameter)
+
Space.Log("I've been called with parameter: " .. Parameter) 
+
end
+
 
+
Space.Shared.RegisterFunction(namespace, "Log", LogFunction)
+
 
+
 
+
 
+
-- Script in Other Objects
+
thisObject = Space.Host.ExecutingObject
+
namespace = "com.example.shared"
+
 
+
OnClick = function()
+
Space.Shared.CallFunction(namespace, "Log", {"Example"})
+
end
+
 
+
thisObject.AddClickable()
+
thisObject.Clickable.OnClick(OnClick)</pre>}}
+
 
+
{{ScriptFunction|void|RegisterBroadcastFunction|(string ns, string func, Closure reference);|Makes func into a global function that can be accessed anywhere.|5=
+
function someFunction(name)
+
&nbsp;&nbsp;Space.Log("Hello " .. name);<br>
+
end<br><br>
+
 
+
'''Space.Shared.RegisterBroadcastFunction'''("com.someNameHere.world", "func", someFunction);}}
+
 
+
 
+
{{ScriptFunction|int|CallBroadcastFunction|((string ns, string func, IEnumerable< DynValue > args);|Calls every registered broadcast function with the specified arguments, and returns the number of calls queued.|5=
+
''-- Script A: Receiver''<br>
+
function someFunction(status)<br>
+
&nbsp;&nbsp;if status == "start"<br>
+
&nbsp;&nbsp;then Space.Log("Do receiver 1 procedures.")<br>
+
&nbsp;&nbsp;end<br>
+
end<br><br>
+
 
+
Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br><br>
+
 
+
 
+
''-- Script B: Another Receiver''<br>
+
function someFunction(status)<br>
+
&nbsp;&nbsp;if status == "start"<br>
+
&nbsp;&nbsp;then Space.Log("Do receiver 2 procedures.")<br>
+
&nbsp;&nbsp;end<br>
+
end<br><br>
+
 
+
Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br><br>
+
 
+
 
+
''--Script C: Sender''<br>
+
local ball = Space.Host.ExecutingObject;<br>
+
ball.SubscribeToEvents();<br><br>
+
 
+
function onDown()<br>
+
&nbsp;&nbsp;local queue = '''Space.Shared.CallBroadcastFunction'''("com.someNameHere.world", "func", {"start"});<br>
+
&nbsp;&nbsp;Space.Log("number in queue: " .. queue);<br>
+
end<br><br>
+
 
+
 
+
ball.OnMouseDown(onDown);}}
+
 
+
{{ScriptFunction|int|UnregisterBroadcastFunction|(string ns, string func, Closure reference);|Unregister Broadcast Function.|5=
+
''-- Script A: Receiver''<br>
+
function someFunction(status)<br>
+
&nbsp;&nbsp;if status == "start"<br>
+
&nbsp;&nbsp;then Space.Log("Do receiver 1 procedures.")<br>
+
&nbsp;&nbsp;end<br>
+
end<br><br>
+
 
+
Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br><br>
+
 
+
 
+
''-- Script B: Another Receiver''<br>
+
function someFunction(status)<br>
+
&nbsp;&nbsp;if status == "start"<br>
+
&nbsp;&nbsp;then Space.Log("Do receiver 2 procedures.")<br>
+
&nbsp;&nbsp;end<br>
+
end<br><br>
+
 
+
Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br><br>
+
 
+
 
+
''--Script C: Sender''<br>
+
local ball = Space.Host.ExecutingObject;<br>
+
ball.SubscribeToEvents();<br><br>
+
 
+
function onDown()<br>
+
&nbsp;&nbsp;local queue = '''Space.Shared.CallBroadcastFunction'''("com.someNameHere.world", "func", {"start"});<br>
+
&nbsp;&nbsp;Space.Log("number in queue: " .. queue);<br>
+
&nbsp;&nbsp;Space.Shared.UnregisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br>
+
&nbsp;&nbsp;Space.Log("UnregisterBroadcastFunction");<br>
+
end<br><br>
+
 
+
 
+
ball.OnMouseDown(onDown);}}
+
 
+
{{ScriptFunction|int|UnregisterBroadcastFunction|(string ns, string func);|Unregister Broadcast Function.|5=
+
''-- Script A: Receiver''<br>
+
function someFunction(status)<br>
+
&nbsp;&nbsp;if status == "start"<br>
+
&nbsp;&nbsp;then Space.Log("Do receiver 1 procedures.")<br>
+
&nbsp;&nbsp;end<br>
+
end<br><br>
+
 
+
Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br><br>
+
 
+
 
+
''-- Script B: Another Receiver''<br>
+
function someFunction(status)<br>
+
&nbsp;&nbsp;if status == "start"<br>
+
&nbsp;&nbsp;then Space.Log("Do receiver 2 procedures.")<br>
+
&nbsp;&nbsp;end<br>
+
end<br><br>
+
 
+
Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);<br><br>
+
 
+
 
+
''--Script C: Sender''<br>
+
local ball = Space.Host.ExecutingObject;<br>
+
ball.SubscribeToEvents();<br><br>
+
 
+
function onDown()<br>
+
&nbsp;&nbsp;local queue = '''Space.Shared.CallBroadcastFunction'''("com.someNameHere.world", "func", {"start"});<br>
+
&nbsp;&nbsp;Space.Log("number in queue: " .. queue);<br>
+
&nbsp;&nbsp;Space.Shared.UnregisterBroadcastFunction("com.someNameHere.world", "func");<br>
+
&nbsp;&nbsp;Space.Log("UnregisterBroadcastFunction");<br>
+
end<br><br>
+
 
+
 
+
ball.OnMouseDown(onDown);}}
+
 
+
{{ScriptFunction|void|SetSuperGlobal|(string ns, string key, DynValue value);|Sets a Super global key to a value. The value can be any object type.|5=
+
Space.Shared.SetSuperGlobal("com.someNameHere.world", "version", "1.02"); |6=<pre>--these two scripts are in two different regions but script B will know that you came from A
+
 
+
--REGION A script
+
username = Space.Scene.PlayerAvatar.Username
+
region = Space.Scene.Name
+
Space.Shared.SetSuperGlobal (username, "Last Location", region)
+
 
+
 
+
 
+
--REGION B script
+
username = Space.Scene.PlayerAvatar.Username
+
region = Space.Shared.GetSuperGlobal (username, "Last Location")
+
Space.Dialogues.SendLocalChat ("You have arrived from ".. region, "Last Location")</pre>}}
+
 
+
{{ScriptFunction|DynValue|GetSuperGlobal|(string ns, string key);|Retrieves a previously set super global variable, or returns nil.|5=
+
local versionValue = Space.Shared.GetSuperGlobal("com.someNameHere.world",  "version");|6=<pre>--these two scripts are in two different regions but script B will know that you came from A
+
 
+
--REGION A script
+
username = Space.Scene.PlayerAvatar.Username
+
region = Space.Scene.Name
+
Space.Shared.SetSuperGlobal (username, "Last Location", region)
+
 
+
 
+
 
+
--REGION B script
+
username = Space.Scene.PlayerAvatar.Username
+
region = Space.Shared.GetSuperGlobal (username, "Last Location")
+
Space.Dialogues.SendLocalChat ("You have arrived from ".. region, "Last Location")</pre>
+
}}
+
 
+
 
+
{{Scripting Navbox}}
+

Latest revision as of 05:01, 19 September 2022

This page has moved to: https://docs.sine.space/v/scripting/client-scripting/viewer/sshared