# SShared

## Index

### Functions Index

<table><thead><tr><th width="363.0187893383594">Function Name</th></tr></thead><tbody><tr><td>void <a href="#setglobal"><strong>SetGlobal</strong></a>(string ns, string key, DynValue value)</td></tr><tr><td>void <a href="#setsuperglobal"><strong>SetSuperGlobal</strong></a>(string ns, string key, DynValue value)</td></tr><tr><td>DynValue <a href="#getglobal"><strong>GetGlobal</strong></a>(string ns, string key)</td></tr><tr><td>DynValue <a href="#getsuperglobal"><strong>GetSuperGlobal</strong></a>(string ns, string key)</td></tr><tr><td>void <a href="#registerfunction"><strong>RegisterFunction</strong></a>(string ns, string func, Closure reference)</td></tr><tr><td>void <a href="#registerbroadcastfunction"><strong>RegisterBroadcastFunction</strong></a>(string ns, string func, Closure reference)</td></tr><tr><td><p>void <a href="#unregisterbroadcastfunction"><strong>UnregisterBroadcastFunction</strong></a>(string ns, string func, Closure reference)</p><p>void <a href="#unregisterbroadcastfunction"><strong>UnregisterBroadcastFunction</strong></a>(string ns, string func)</p></td></tr><tr><td>void <a href="#callfunction"><strong>CallFunction</strong></a>(string ns, string func, IEnumerable&#x3C; DynValue > args)</td></tr><tr><td>void <a href="#callbroadcastfunction"><strong>CallBroadcastFunction</strong></a>(string ns, string func, IEnumerable&#x3C; DynValue > args)</td></tr></tbody></table>

## Functions

### SetGlobal

void **SetGlobal**(string ns, string key, DynValue value)

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

| Parameter | Type     | Description |
| --------- | -------- | ----------- |
| ns        | string   |             |
| key       | string   |             |
| value     | DynValue |             |

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

### SetSuperGlobal

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

*Function Description*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
example 2
```

{% endtab %}
{% endtabs %}

### GetGlobal

DynValue **GetGlobal**(string ns, string key)

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

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

### GetSuperGlobal

DynValue **GetSuperGlobal**(string ns, string key)

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

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

### RegisterFunction

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

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

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
function someFunction(name)
  Space.Log("Hello " .. name);
end

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

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

### RegisterBroadcastFunction

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

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

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
function someFunction(name)
  Space.Log("Hello " .. name);
end

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

{% endtab %}
{% endtabs %}

### UnregisterBroadcastFunction

void **UnregisterBroadcastFunction**(string ns, string func, Closure reference)\
void **UnregisterBroadcastFunction**(string ns, string func)

*Function Description*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
-- 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);
```

{% endtab %}
{% endtabs %}

### CallFunction

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

*Calls the registered function with the specified arguments.*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

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

{% endtab %}
{% endtabs %}

### CallBroadcastFunction

void **CallBroadcastFunction**(string ns, string func, IEnumerable< DynValue > args)

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

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
function someFunction(name)
  Space.Log("Hello " .. name);
end

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

{% endtab %}
{% endtabs %}
