Chat

Description

Supported Script Types:

  • Interface Scripts
  • Client Entity Scripts
  • Avatar Scripts
The Chat API lets you use chat in world.

Methods

Name Return Value Summary
addCommand ChatCommand

Adds a command to chat.

getCommand ChatCommand

Get one command for chat.

getCommands Array.<ChatCommand>

Get all commands for chat.

removeCommand None

Removes a command from chat.

sendMessage None

Sends the given message to all other users in world.

showMessage None

Shows the given message locally.

Signals

Name Summary
messageReceived

Triggered when a chat message is received.

messageSent

Triggered when the user sends a message.

Method Details

(static) addCommand( command, description ) → {ChatCommand}
Returns: The command added.

Adds a command to chat.

Parameters

Name Type Description
command string

The callable command string.

description string

The command description.

Example

Add new `/scale 0.5` command

var command = Chat.addCommand("scale", "updates your avatars scale");
command.running.connect(function (params) {
    var scale = Number(params);
    if (scale > 0) {
        MyAvatar.scale = scale;
        Chat.showMessage("Updated avatar scale to " + scale);
    } else {
        Chat.showMessage("Invalid scale!");
    }
});

// run your command if you want to
command.run("0.5");

Script.scriptEnding.connect(function () {
    Chat.removeCommand(command);
})
(static) getCommand( command ) → {ChatCommand}
Returns: The command.

Get one command for chat.

Parameters

Name Type Description
command string

The callable command string.

(static) getCommands( ) → {Array.<ChatCommand>}
Returns: All available commands.

Get all commands for chat.

(static) removeCommand( command )

Removes a command from chat.

Parameters

Name Type Description
command ChatCommand

The command to remove.

(static) sendMessage( message, meopt, ttsopt )

Sends the given message to all other users in world.

Parameters

Name Type Attributes Default Value Description
message string

The text message to send.

me bool <optional>
false

Speaks in third person.

tts bool <optional>
false

Send the message with text to speech.

(static) showMessage( message )

Shows the given message locally.

Parameters

Name Type Description
message string

The text message to show.

Signal Details

messageReceived( data, senderID )
Returns: Signal

Triggered when a chat message is received.

Parameters

Name Type Description
data object

The message received, usually {message: "This is a message!"} but can also include extras like:

  • {message: "This is a local announcement", local: true} from Chat.showMessage
  • {message: "Hello world!", tts: true}
  • {message: "is thinking", me: true}
senderID Uuid

The UUID of the sender: the user's session UUID if sent by an Interface or client entity script, the UUID of the entity script server if sent by a server entity script, or the UUID of the assignment client instance if sent by an assignment client script.

Example
Chat.messageReceived.connect(function (data, senderID) {
    if (data.local) {
        console.log(data.message);
    } else {
        var avatar = AvatarList.getAvatar(senderID);
        console.log(
            avatar.displayName + 
            (data.me ? " " : ": ") + 
            data.message
        );
    }
});
messageSent( message )
Returns: Signal

Triggered when the user sends a message.

Parameters

Name Type Description
message string

The message sent

Example
var huggiesSound = SoundCache.getSound("https://files.tivolicloud.com/maki/sounds/huggies.wav");
Chat.messageSent.connect(function (message) {
    if (message.toLowerCase().indexOf("huggies") > -1 && huggiesSound.downloaded) {
        Audio.playSound(huggiesSound, {
            position: MyAvatar.position,
            volume: 0.25
        });
    }
});