en:vci:sample:message:no2

Exchange variables between multiple VCIs

Example data

Unitypackage

tutorial_message_send.zip

From VCI v0.17, vci.message is implemented which allows transmission and reception of messages in VCI.
This allows for the exchange of variables between multiple VCIs.

In this example, we will create two VCIs, transmitter and receiver, to demonstrate the communication of messages.

onUsing the transmitter VCI will send a message, which triggers a process in the receiving side if the set condition is met.
In this example, the receiving cylinder VCI will jump with upward velocity when it receives a message.

For the detailed information on vci.message, refer to:
VirtualCast1.5.4a VCI更新

send.lua
function onUse(self)
    -- Message name: MSG_NAME
    -- Content of the message: 1
    vci.message.Emit("send_box_state","addforce_on")
end
 
function onUnuse(use)
    -- Message name: MSG_NAME
    -- Content of the message: 1
    vci.message.Emit("send_box_state","addforce_off")
end
get.lua
local sub = vci.assets.GetTransform("get")
 
function onMessage(sender, name, message)
    -- The sender is in a table structure The information of the VCI that executed the vci.message.Emit function
    -- {
    --   type: "vci"
    --   name: "name of the vci"
    -- }
    -- In the future, the sender will have new messages implemented
    for k, v in pairs(sender) do
        print(k .. ":" .. v)
    end
 
    print(name)
    print(message)
 
    if name=="send_box_state" and message=="addforce_on" then
        print("forceon")
        sub.AddForce (1000*sub.GetUp())
    end
end
 
-- When ''vci.message.Emit'' is executed in studio, the onMessage function is called
vci.message.On("send_box_state", onMessage)
en/vci/sample/message/no2.txt · Last modified: 2023/08/28 20:15 by pastatto

Page Tools