en:vci:script:reference:message

vci.message

You can send a message from one VCI to another VCI, using it as a mean of communication between VCIs.
This feature allows you to control a VCI from a VCI.
There is a similar feature that called Global Sync Variable. For more information about the use of messages and Global Sync Variable, please refer to here.

List of functions

Emit()

  • Sends a message
  • The message will be sent to all VCIs of all users including the sender itself.

On()

  • Receives a message
  • The message can be received by the same VCI that sent the message or the other VCIs.
  • Receives an Emit message with the first argument that matches the first argument of the On.
  • Passes the received message to the function in the second argument.
  • The On must be declared after the receiving function specified in the second argument. Declaring On before the receiving function will result in an error.

Receiver function

  • Receives three arguments.
  • sender: Information about the sender (set by the system)
  • name: The name of the message. The first argument of Emit function.
  • message: Content of the message. The second argument of Emit function.

Example

Transmitter

send.lua
function onUse(self)
    -- The first argument is the type of the message, the second argument is the content of the message
    -- In this example, specified a message that makes the receiving SubItem jump and value that determines the strength of that jump
    vci.message.Emit("message_jump",3000)
end

Receiver

get.lua
local receiver = vci.assets.GetTransform("Cube")
 
    -- sender: Information about the sender (set by the system)
    -- name: The name of the message. The first argument of Emit function.
    -- message: Content of the message. The second argument of Emit function.
function Jump(sender, name, message)
 
    for k, v in pairs(sender) do
        print(k .. ":" .. v)
    end
 
    -- "name" contains type of the message, "message" contains content of the message    
    print(name)
    print(message)
 
    -- Make the SubItem jump with the strength specified by the message
    receiver.AddForce(message*receiver.GetUp())
 
end
 
-- When a ''vci.message.Emit'' with its message name "message_jump" is executed in the studio, the Jump function is called
vci.message.On("message_jump", Jump)

What does the transmitter do?
Just send the desired message by executing Emit().
Of course, you have to decide in advance what messages to use to communicate with the receiver.

What does the receiver do?
1. Register the type of message to receive with vci.message.On().
 When a message is sent by Emit(), you can receive a message that is registered with On().
2. In this example, the message received is passed to Jump().
 The content of the message is given to Jump() as arguments.
3. The content of the Jump function's argument is the message.
 So, by changing the message sent from the transmitter, you can change the behavior of the receiving VCI.

Another example

en/vci/script/reference/message.txt · Last modified: 2022/03/06 23:15 by Ramen

Page Tools