wiki.sine.space | sinespace

Scripting/SShared

From wiki.sine.space
Revision as of 07:55, 20 December 2021 by Voidtech (Talk | contribs)

Jump to: navigation, search

The SShared class is for communication between scripts and objects within a client. For communication needed between different clients/players please use Scripting/SNetwork.

Members

SetGlobal

void SetGlobal (string namespace, string key, DynValue value);

Sets a global key to a value. The value can be any object type.

Space.Shared.SetGlobal("com.someNameHere.world", "version", "1.02");


-- 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)

GetGlobal

DynValue GetGlobal (string namespace, string key);

Retrieves a previously set global variable, or returns nil.

local versionValue = Space.Shared.GetGlobal("com.someNameHere.world", "version")


-- 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)

RegisterFunction

void RegisterFunction (string ns, string func, Closure reference);

Makes func into a global function that can be accessed anywhere.

function someFunction(name)

  Space.Log("Hello " .. name);
end

Space.Shared.RegisterFunction("com.someNameHere.world", "func", someFunction);


CallFunction

void CallFunction (string ns, string func, IEnumerable< DynValue > args);

Calls the registered function with the specified arguments.

Space.Shared.CallFunction("com.someNameHere.world", "func",{"Smith"});


RegisterBroadcastFunction

void RegisterBroadcastFunction (string ns, string func, Closure reference);

Makes func into a global function that can be accessed anywhere.

function someFunction(name)

  Space.Log("Hello " .. name);
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);



CallBroadcastFunction

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.

-- Script A: Receiver

function someFunction(status)
  if status == "start"
  then Space.Log("Do receiver 1 procedures.")
  end
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);


-- Script B: Another Receiver
function someFunction(status)
  if status == "start"
  then Space.Log("Do receiver 2 procedures.")
  end
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);


--Script C: Sender
local ball = Space.Host.ExecutingObject;
ball.SubscribeToEvents();

function onDown()
  local queue = Space.Shared.CallBroadcastFunction("com.someNameHere.world", "func", {"start"});
  Space.Log("number in queue: " .. queue);
end


ball.OnMouseDown(onDown);


UnregisterBroadcastFunction

int UnregisterBroadcastFunction (string ns, string func, Closure reference);

Unregister Broadcast Function.

-- Script A: Receiver

function someFunction(status)
  if status == "start"
  then Space.Log("Do receiver 1 procedures.")
  end
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);


-- Script B: Another Receiver
function someFunction(status)
  if status == "start"
  then Space.Log("Do receiver 2 procedures.")
  end
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);


--Script C: Sender
local ball = Space.Host.ExecutingObject;
ball.SubscribeToEvents();

function onDown()
  local queue = Space.Shared.CallBroadcastFunction("com.someNameHere.world", "func", {"start"});
  Space.Log("number in queue: " .. queue);
  Space.Shared.UnregisterBroadcastFunction("com.someNameHere.world", "func", someFunction);
  Space.Log("UnregisterBroadcastFunction");
end


ball.OnMouseDown(onDown);


UnregisterBroadcastFunction

int UnregisterBroadcastFunction (string ns, string func);

Unregister Broadcast Function.

-- Script A: Receiver

function someFunction(status)
  if status == "start"
  then Space.Log("Do receiver 1 procedures.")
  end
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);


-- Script B: Another Receiver
function someFunction(status)
  if status == "start"
  then Space.Log("Do receiver 2 procedures.")
  end
end

Space.Shared.RegisterBroadcastFunction("com.someNameHere.world", "func", someFunction);


--Script C: Sender
local ball = Space.Host.ExecutingObject;
ball.SubscribeToEvents();

function onDown()
  local queue = Space.Shared.CallBroadcastFunction("com.someNameHere.world", "func", {"start"});
  Space.Log("number in queue: " .. queue);
  Space.Shared.UnregisterBroadcastFunction("com.someNameHere.world", "func");
  Space.Log("UnregisterBroadcastFunction");
end


ball.OnMouseDown(onDown);


SetSuperGlobal

void SetSuperGlobal (string ns, string key, DynValue value);

Sets a Super global key to a value. The value can be any object type.

Space.Shared.SetSuperGlobal("com.someNameHere.world", "version", "1.02");


--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")

GetSuperGlobal

DynValue GetSuperGlobal (string ns, string key);

Retrieves a previously set super global variable, or returns nil.

local versionValue = Space.Shared.GetSuperGlobal("com.someNameHere.world", "version");


--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")