# SChat

## Index

### Functions Index

| Function Name                                                                                                                                                                 |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p>void <a href="#onchat"><strong>OnChat</strong> </a>(Closure callback)<br>void <a href="#onchat"><strong>OnChat</strong> </a>(Action< SChatMessage > trackInfoCallback)</p> |
| void [**JoinChat** ](#joinchat)(string name)                                                                                                                                  |
| string [**GetXMPPName** ](#getxmppname)(string name)                                                                                                                          |
| void [**LeaveChat** ](#leavechat)(string name)                                                                                                                                |
| SVoiceZone [**GetVoiceZone** ](#getvoicezone)(string channel)                                                                                                                 |
| void [**JoinVoice** ](#joinvoice)(string channel, int priority=5, int spatial=0)                                                                                              |
| void [**JoinGridVoice** ](#joingridvoice)(string channel, int priority=5, int spatial=0)                                                                                      |
| void [**LeaveVoice** ](#leavevoice)(string channel)                                                                                                                           |

## Functions

### OnChat

void **OnChat** (Closure callback)\
void **OnChat** (Action< [SChatMessage](https://docs.sine.space/scripting/client-scripting-api-reference/types/schatmessage) > trackInfoCallback)

*Bind a function to the OnChat event. This function will be called every time a new chat message is received on the local channel.*

| Parameter | Type              | Description                             |
| --------- | ----------------- | --------------------------------------- |
| callback  | Closure or Action | Invoked when a chat message is received |

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

```lua
Space.Network.Chat.OnChat(OnChatFunction) 
```

{% endtab %}
{% endtabs %}

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

```lua
--this script takes every new chat message and displays it in a UIText 
uiText = Space.Host.GetReference("text").UIText --Add this Text object as reference in Scripting Runtime

OnChatFunction = function(SChatMessage)
uiText.Text = SChatMessage.Sender ..": " .. SChatMessage.Message 
end

Space.Network.Chat.OnChat(OnChatFunction) 
```

{% endtab %}
{% endtabs %}

### JoinChat

void **JoinChat** (string name)

*Join a chat channel.*

| Parameter | Type   | Description          |
| --------- | ------ | -------------------- |
| name      | string | Chat channel to join |

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

```lua
Space.Network.Chat.JoinChat("Test room")
```

{% endtab %}
{% endtabs %}

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

```lua
--this script will make the player join a chat when entering a trigger collider and leave it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 

    Space.Network.Chat.JoinChat("PrivateChatZone")
    
  end

end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then

    Space.Network.Chat.LeaveChat("PrivateChatZone")
    
  end 
  
end


thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)
```

{% endtab %}
{% endtabs %}

### GetXMPPName

string **GetXMPPName** (string name)

*Get XMPP name.*

| Parameter | Type   | Description              |
| --------- | ------ | ------------------------ |
| name      | string | Human-readable chat name |

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

```lua
Space.Network.Chat.GetXMPPName("Test room")
```

{% endtab %}
{% endtabs %}

### LeaveChat

void **LeaveChat** (string name)

*Leave a chat channel.*

| Parameter | Type   | Description           |
| --------- | ------ | --------------------- |
| name      | string | Chat channel to leave |

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

```lua
Space.Network.Chat.LeaveChat("Test room")
```

{% endtab %}
{% endtabs %}

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

```lua
--this script will make the player join a chat when entering a trigger collider and leave it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 

    Space.Network.Chat.JoinChat("PrivateChatZone")
    
  end

end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then

    Space.Network.Chat.LeaveChat("PrivateChatZone")
    
  end 
  
end


thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)
```

{% endtab %}
{% endtabs %}

### GetVoiceZone

SVoiceZone **GetVoiceZone** (string channel)

*Get a reference to a voice zone component for the given channel.*

| Parameter | Type   | Description               |
| --------- | ------ | ------------------------- |
| channel   | string | Voice channel name/handle |

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

```lua
voiceZone = Space.Network.Chat.GetVoiceZone("AChannel")
```

{% endtab %}
{% endtabs %}

### JoinVoice

void **JoinVoice** (string channel, int priority=5, int spatial=0)

*Join a voice chat channel.*

| Parameter | Type   | Description                                    |
| --------- | ------ | ---------------------------------------------- |
| channel   | string | Voice channel name/handle                      |
| priority  | int    | Optional. Priority, default 5                  |
| spatial   | int    | Optional. Spatialization flag (0/1), default 0 |

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

```lua
Space.Network.Chat.JoinVoice("PrivateVoiceZone")
```

{% endtab %}
{% endtabs %}

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

```lua
--this script will make the player join a voice channel when entering a trigger collider and leave it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 

    Space.Network.Chat.JoinVoice("PrivateVoiceZone")
    
  end

end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then

    Space.Network.Chat.LeaveVoice("PrivateVoiceZone")
    
  end 
  
end


thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)
```

{% endtab %}
{% endtabs %}

### JoinGridVoice

void **JoinGridVoice** (string channel, int priority=5, int spatial=0)

*JoinGridVoice is the same as JoinVoice except with a grid wide parameter. (white-label only)*

| Parameter | Type   | Description                                    |
| --------- | ------ | ---------------------------------------------- |
| channel   | string | Voice channel name/handle (grid-wide)          |
| priority  | int    | Optional. Priority, default 5                  |
| spatial   | int    | Optional. Spatialization flag (0/1), default 0 |

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

```lua
Space.Network.Chat.JoinGridVoice("PrivateGridVoiceZone")
```

{% endtab %}
{% endtabs %}

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

```lua
--this script will make the player join a Grid voice channel when entering a trigger collider and leave it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 

    Space.Network.Chat.JoinGridVoice("PrivateGridVoiceZone")
    
  end

end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then

    Space.Network.Chat.LeaveVoice("PrivateGridVoiceZone")
    
  end 
  
end


thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)
```

{% endtab %}
{% endtabs %}

### LeaveVoice

void **LeaveVoice** (string channel)

*Leave a voice chat channel*

| Parameter | Type   | Description            |
| --------- | ------ | ---------------------- |
| channel   | string | Voice channel to leave |

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

```lua
Space.Network.Chat.LeaveVoice("PrivateVoiceZone")
```

{% endtab %}
{% endtabs %}

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

```lua
--this script will make the player join a voice channel when entering a trigger collider and leave it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 

    Space.Network.Chat.JoinVoice("PrivateVoiceZone")
    
  end

end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then

    Space.Network.Chat.LeaveVoice("PrivateVoiceZone")
    
  end 
  
end


thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)
```

{% endtab %}
{% endtabs %}
