# SRegionScript

**成员** 23

|      |                                                                 |                                                                                                                                                                                 |
| ---- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 函数 | [Log](#log)                                                     | Log a message to console. (only works on preview grid)                                                                                                                          |
| 函数 | [SubscribeToNetwork](#subscribetonetwork)                       | Subscribe to network messages on "key".                                                                                                                                         |
| 函数 | [UnsubscribeFromNetwork](#unsubscribefromnetwork)               | Unsubscribe from network messages on "key".                                                                                                                                     |
| 函数 | [SendMessageToAllClientScripts](#sendmessagetoallclientscripts) | Sends a network message to all client scripts.                                                                                                                                  |
| 函数 | [SendMessageToClientScripts](#sendmessagetoclientscripts)       | Sends a network message to a client script belonging to a specific player                                                                                                       |
| 函数 | [StartCoroutine](#startcoroutine)                               | Runs the given function as a coroutine.                                                                                                                                         |
| 属性 | [ScriptID](#scriptid)                                           | Returns a unique identifier of this script runtime. (can be found in "/list\_scripts" window on preview.)                                                                       |
| 属性 | [InstanceID](#instanceid)                                       | Returns a unique identifier for the instance of the region.                                                                                                                     |
| 属性 | [RegionID](#regionid)                                           | Returns a unique identifier for the region. (Same ID seen in curator)                                                                                                           |
| 属性 | [Scene](#scene)                                                 | Access to the SScene class methods and properties.                                                                                                                              |
| 属性 | [Database](#database)                                           | Access to the SDatabase class methods and properties.                                                                                                                           |
| 属性 | [Math](#math)                                                   | Access to the SMath class methods and properties.                                                                                                                               |
| 属性 | [Shared](#shared)                                               | Access to the SShared class methods and properties.                                                                                                                             |
| 属性 | [WebServices](#webservices)                                     | Access to the SWebservice class methods and properties.                                                                                                                         |
| 属性 | [RuntimeType](#runtimetype)                                     | Returns "Server" if this script is a Server script                                                                                                                              |
| 属性 | [InEditor](#ineditor)                                           | Returns true if in Editor. (this will return false on server scripts)                                                                                                           |
| 属性 | [TimeSinceAwake](#timesinceawake)                               | Time, in seconds, since this script runtime has been awake.                                                                                                                     |
| 属性 | [DateTimeUTC](#datetimeutc)                                     | Returns a string of the current Date/Time in UTC                                                                                                                                |
| 属性 | [LocalTimeUnix](#localtimeunix)                                 | Returns the current Unix time (in seconds) (same as ServerTimeUnix)                                                                                                             |
| 属性 | [ServerTimeUnix](#servertimeunix)                               | Returns the current Unix time (in seconds) (same as LocalTimeUnix)                                                                                                              |
| 事件 | [OnAvatarJoin](#onavatarjoin)                                   | An event function which will be called whenever an Avatar joins and will also contain the Avatar's ID as a parameter.                                                           |
| 事件 | [OnAvatarLeave](#onavatarleave)                                 | An event function which will be called whenever an Avatar leaves and will also contain the Avatar's ID as a parameter.                                                          |
| 事件 | [OnScriptServerMessage](#onscriptservermessage)                 | An event function which will be called whenever the server script receives a Network Message, and will contain the network message key String and message Table as a parameter. |

## 函数

### Log

void Log(string message)

Log a message to console. (only works on preview grid)

参数

| 名称    | 类型   | 默认值 |
| ------- | ------ | ------ |
| message | string | –      |

返回值

`void`

示例

**Lua**

```lua
Space.Log("Log")

```

**Lua**

```lua
--Clicking the object will make the server script log "Test Message" on all clients

--server script
function OnScriptServerMessage(key, table) 
  if key == "testKey" then
    Space.Log("Message:" .. table[1])
  end
end


--client script
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"Test Message"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### SubscribeToNetwork

void SubscribeToNetwork(string key)

Subscribe to network messages on "key".

参数

| 名称 | 类型   | 默认值 |
| ---- | ------ | ------ |
| key  | string | –      |

返回值

`void`

示例

**Lua**

```lua
Space.SubscribeToNetwork("thekey")

```

### UnsubscribeFromNetwork

void UnsubscribeFromNetwork(string key)

Unsubscribe from network messages on "key".

参数

| 名称 | 类型   | 默认值 |
| ---- | ------ | ------ |
| key  | string | –      |

返回值

`void`

示例

**Lua**

```lua
Space.UnsubscribeFromNetwork("thekey")

```

### SendMessageToAllClientScripts

void SendMessageToAllClientScripts(string messageName, Table message)

Sends a network message to all client scripts.

参数

| 名称        | 类型   | 默认值 |
| ----------- | ------ | ------ |
| messageName | string | –      |
| message     | Table  | –      |

返回值

`void`

示例

**Lua**

```lua
Space.SendMessageToAllClientScripts("thekey",{"themessage"})

```

**Lua**

```lua
--Clicking the object by a single client will send a message to the server which triggers a message response to all clients

--server script--
function OnScriptServerMessage(key, table) 
  if key == "testKey" then
  Space.SendMessageToAllClientScripts("ServerResponse",{"message response to all client scripts in region that are subscribed to this key"})
  end

end

--client script--
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"Test Message"})  
end

OnReceiveFunction = function(SNetworkMessage)
  Space.Log(SNetworkMessage.Key)
  Space.Log(SNetworkMessage.Message[1])
end

Space.Network.SubscribeToNetwork("ServerResponse", OnReceiveFunction)
thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### SendMessageToClientScripts

void SendMessageToClientScripts(ulong playerID, string messageName, Table message)

Sends a network message to a client script belonging to a specific player

参数

| 名称        | 类型   | 默认值 |
| ----------- | ------ | ------ |
| playerID    | ulong  | –      |
| messageName | string | –      |
| message     | Table  | –      |

返回值

`void`

示例

**Lua**

```lua
Space.SendMessageToClientScripts(playerID,"thekey",{"themessage"})

```

**Lua**

```lua
--Clicking the object by a single client will send a message to the server which triggers...
--a message response only to that specific client


--server script--


function OnScriptServerMessage(key, table) 
  if key == "testKey" then
    local sender = table[2]
    Space.SendMessageToClientScripts(sender,"ServerResponse",{"a message response to the clicker's client script in region that is subscribed to this key"})
  end
end


--client script--
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  id = Space.Scene.PlayerAvatar.ID
  Space.Network.SendNetworkMessage("testKey",{"Test Message", id})  
end

OnReceiveFunction = function(SNetworkMessage)
  Space.Log(SNetworkMessage.Key)
  Space.Log(SNetworkMessage.Message[1])
end

Space.Network.SubscribeToNetwork("ServerResponse", OnReceiveFunction)
thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### StartCoroutine

void StartCoroutine(DynValue coroutine, DynValue parameter = default(DynValue))

void StartCoroutine(string name, DynValue parameter = default(DynValue))

Runs the given function as a coroutine. A parameter is optional.

参数

| 名称      | 类型     | 默认值            |
| --------- | -------- | ----------------- |
| coroutine | DynValue | –                 |
| parameter | DynValue | default(DynValue) |
| name      | string   | –                 |

返回值

`void`

示例

**Lua**

```lua
Space.StartCoroutine(AFunctionName)

```

**Lua**

```lua
--Clicking the object on client will trigger a 3 second countdown on server script using coroutine


--server script--
function OnScriptServerMessage(key, table) 
  if key == "testKey" and table[1] == "Timer" then
    Space.StartCoroutine(TimerCoroutine)
  end
end

function TimerCoroutine()
  local time = 0

  while time < 3 do
    Space.Log(3-time)
    time = time + 1
    coroutine.yield(1)
  end

end

--client script--
thisObject = Space.Host.ExecutingObject

function OnClickFunction()
  Space.Network.SendNetworkMessage("testKey",{"Timer"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

## 属性

### ScriptID

string ScriptID {get; }

Returns a unique identifier of this script runtime. (can be found in "/list\_scripts" window on preview.)

示例

**Lua**

```lua
scriptID = Space.ScriptID

```

**Lua**

```lua
--Clicking the object on client will make the server script log it's ScriptID

--server script--
function OnScriptServerMessage(key, table) 
  if key == "testKey" and table[1] == "ScriptID" then
    Space.Log(Space.ScriptID)
  end
end


--client script--
thisObject = Space.Host.ExecutingObject

function OnClickFunction()
  Space.Network.SendNetworkMessage("testKey",{"ScriptID"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### InstanceID

uint InstanceID {get; }

Returns a unique identifier for the instance of the region. Useful in the case of multiple shards/instances being used. InstanceID does not necessarily equal to the RegionID, even if there's only one instance running.

示例

**Lua**

```lua
instance = Space.InstanceID

```

**Lua**

```lua
--Clicking the object on client will make the server script log only...
--if it's instanceID matches with client's instanceID

--server script--
function OnScriptServerMessage(key, table) 
  local instanceID = Space.InstanceID
  if key == "testKey" and table[1] == "ScriptID"  and table[2] == instanceID then
    Space.Log(instanceID)
  end
end



--client script--
thisObject = Space.Host.ExecutingObject

function OnClickFunction()
  local instanceID = Space.Scene.InstanceID
  Space.Network.SendNetworkMessage("testKey",{"ScriptID", instanceID})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### RegionID

ulong RegionID {get; }

Returns a unique identifier for the region. (Same ID seen in curator)

示例

**Lua**

```lua
region = Space.RegionID

```

**Lua**

```lua
--Clicking the object on client will make the server script log only...
--if it's RegionID matches with client's RegionID

--server script--
function OnScriptServerMessage(key, table) 
  local RegionID = Space.RegionID
  if key == "testKey" and table[1] == "ScriptID"  and table[2] == RegionID then
    Space.Log(RegionID)
  end
end



--client script--
thisObject = Space.Host.ExecutingObject

function OnClickFunction()
  local RegionID = Space.Scene.RegionID
  Space.Network.SendNetworkMessage("testKey",{"ScriptID", RegionID})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### Scene

[SScene](/docs/zh-Hans/scripting/server-scripting-api-reference/region/sscene.md) Scene {get; }

Access to the SScene class methods and properties.

示例

**Lua**

```lua
Space.Scene

```

### Database

[SDatabase](/docs/zh-Hans/scripting/server-scripting-api-reference/storage/sdatabase.md) Database {get; }

Access to the SDatabase class methods and properties.

示例

**Lua**

```lua
Space.Database

```

### Math

[SMath](/docs/zh-Hans/scripting/server-scripting-api-reference/library/smath.md) Math {get; }

Access to the SMath class methods and properties.

示例

**Lua**

```lua
Space.Math

```

### Shared

[SShared](/docs/zh-Hans/scripting/server-scripting-api-reference/network/sshared.md) Shared {get; }

Access to the SShared class methods and properties.

示例

**Lua**

```lua
Space.Shared

```

### WebServices

[SWebservice](/docs/zh-Hans/scripting/server-scripting-api-reference/network/swebservice.md) WebServices {get; }

Access to the SWebservice class methods and properties.

示例

**Lua**

```lua
Space.WebServices

```

### RuntimeType

string RuntimeType {get; }

Returns "Server" if this script is a Server script

示例

**Lua**

```lua
type = Space.RuntimeType

```

### InEditor

bool InEditor {get; }

Returns true if in Editor. (this will return false on server scripts)

示例

**Lua**

```lua
IsInEditor = Space.InEditor

```

### TimeSinceAwake

float TimeSinceAwake {get; }

Time, in seconds, since this script runtime has been awake.

示例

**Lua**

```lua
SecondsAwake = Space.TimeSinceAwake

```

**Lua**

```lua
--Clicking the object will make the server script log it's TimeSinceAwake on all clients

--server script
function OnScriptServerMessage(key, table) 
  if key == "testKey" and table[1] == "TimeSinceAwake" then
    Space.Log(Space.TimeSinceAwake)
  end
end


--client script
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"TimeSinceAwake"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### DateTimeUTC

string DateTimeUTC {get; }

Returns a string of the current Date/Time in UTC

示例

**Lua**

```lua
DateAndTime = Space.DateTimeUTC

```

**Lua**

```lua
--Clicking the object will make the server script log it's DateTimeUTC on all clients

--server script
function OnScriptServerMessage(key, table) 
  if key == "testKey" and table[1] == "DateTimeUTC" then
    Space.Log(Space.DateTimeUTC)
  end
end


--client script
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"DateTimeUTC"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### LocalTimeUnix

int LocalTimeUnix {get; }

Returns the current Unix time (in seconds) (same as ServerTimeUnix)

示例

**Lua**

```lua
unixtime = Space.LocalTimeUnix

```

**Lua**

```lua
--Clicking the object will make the server script log it's LocalTimeUnix on all clients

--server script
function OnScriptServerMessage(key, table) 
  if key == "testKey" and table[1] == "LocalTimeUnix" then
    Space.Log(Space.LocalTimeUnix)
  end
end


--client script
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"LocalTimeUnix"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

### ServerTimeUnix

int ServerTimeUnix {get; }

Returns the current Unix time (in seconds) (same as LocalTimeUnix)

示例

**Lua**

```lua
unixtime = Space.ServerTimeUnix

```

**Lua**

```lua
--Clicking the object will make the server script log it's ServerTimeUnix on all clients

--server script
function OnScriptServerMessage(key, table) 
  if key == "testKey" and table[1] == "ServerTimeUnix" then
    Space.Log(Space.ServerTimeUnix)
  end
end


--client script
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"ServerTimeUnix"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```

## 事件

### OnAvatarJoin

Action< int > OnAvatarJoin

An event function which will be called whenever an Avatar joins and will also contain the Avatar's ID as a parameter.

示例

**Lua**

```lua
function OnAvatarJoin(playerID)
end

```

**Lua**

```lua
function OnAvatarJoin(id)
  local numOfAvatars = Space.Scene.ConnectedAvatars
  Space.Log("Avatar with ID " .. id .. "has joined")
  Space.Log("Number of avatars connected: " .. numOfAvatars)
end

```

### OnAvatarLeave

Action< int > OnAvatarLeave

An event function which will be called whenever an Avatar leaves and will also contain the Avatar's ID as a parameter.

示例

**Lua**

```lua
function OnAvatarLeave(playerID)
end

```

**Lua**

```lua
function OnAvatarLeave(id)
  local numOfAvatars = Space.Scene.ConnectedAvatars
  Space.Log("Avatar with ID " .. id .. "has left")
  Space.Log("Number of avatars connected: " .. numOfAvatars)
end

```

### OnScriptServerMessage

Action< string, Table > OnScriptServerMessage

An event function which will be called whenever the server script receives a Network Message, and will contain the network message key String and message Table as a parameter.

示例

**Lua**

```lua
function OnScriptServerMessage(key, table) 

theKeyString = key
theSNetworkMessageTable = table

end

```

**Lua**

```lua
--Clicking the object will make the server script log "Test Message" on all clients

--server script
function OnScriptServerMessage(key, table) 
  if key == "testKey" then
    Space.Log("Message:" .. table[1])
  end
end


--client script
thisObject = Space.Host.ExecutingObject

OnClickFunction = function()
  Space.Network.SendNetworkMessage("testKey",{"Test Message"})  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClickFunction)

```
