Skip to content

YaEventAPI

YaEventAPI enables users send events to an event handler. The following code shows how to register an event handler, issue an event and remove an event handler.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
local entityId = script:SelfEntity().EntityId  -- get the id of the entity the script attached to
local option = EventOption.New():SetScope("NewGame"):SetBoundEntityId(entityId)  -- create an option with scope "NewGame" and associate the entity with this option

local h1 = YaEventAPI.OnEventFromLocal("hello", option, function(payload)
    local content = payload:GetContent()       -- get content from the received payload
    local text = table.concat(content, " ")
    print("[hello.handler.1]", text)
end)
local h2 = YaEventAPI.OnEventFromLocal("hello", option, function(payload)
    local content = payload:GetContent()       -- get content from the received payload
    local text = table.concat(content, " ")
    print("[hello.handler.2]", text)
end)

local helloWorldPayload = EventPayload.New({"Hello", "World"})    -- create a payload
YaEventAPI.FireToLocal("hello", option, helloWorldPayload)        -- send "hello" event, two handlers will be invoked
h1:Disconnect()                                                   -- removes the first registered handler
local helloYahahaPayload = EventPayload.New({"Hello", "Yahaha"})  -- create another payload
YaEventAPI.FireToLocal("hello", option, helloYahahaPayload)       -- send "hello" event, only the second handler will be invoked
h2:Disconnect()                                                   -- removes the second registered handler
The output may be:
1
2
3
[hello.handler.2]    Hello World
[hello.handler.1]    Hello World
[hello.handler.2]    Hello Yahaha

Functions

void YaEventAPI.FireToClient(number playerId, string eventName, EventOption option, EventPayload payload)

SERVER ONLY 
The server sends the event to the client identified by playerId.
  • playerId The playerId of the target client.
  • eventName The event name.
  • option The event option.
  • payload The event payload.

void YaEventAPI.FireToAllClients(string eventName, EventOption option, EventPayload payload)

SERVER ONLY 
The server sends the event to all clients.
  • eventName The event name.
  • option The event option.
  • payload The event payload.

void YaEventAPI.FireToServer(string eventName, EventOption option, EventPayload payload)

CLIENT ONLY 
The client sends the event to the server.
  • eventName The event name.
  • option The event option.
  • payload The event payload.

void YaEventAPI.FireToLocal(string eventName, EventOption option, EventPayload payload)

Sends the event to the local handlers.
  • eventName The event name.
  • option The event option.
  • payload The event payload.

EventDisconnector YaEventAPI.OnEventFromServer(string eventName, EventOption option, function handler<EventPayload>)

CLIENT ONLY 
ReturnsAn instance of EventDisconnector.
Registers an event handler to handle events sent from the server.
  • eventName The event name.
  • option The event option.
  • handler The event handler.
    1. payload The event payload.

EventDisconnector YaEventAPI.OnEventFromClient(string eventName, EventOption option, function handler<EventPayload>)

SERVER ONLY 
ReturnsAn instance of EventDisconnector.
Registers an event handler to handle events sent from the client.
  • eventName The event name.
  • option The event option.
  • handler The event handler.
    1. payload The event payload.

EventDisconnector YaEventAPI.OnEventFromLocal(string eventName, EventOption option, function handler<EventPayload>)

ReturnsAn instance of EventDisconnector.
Registers an event handler to handle events sent from the local scripts.
  • eventName The event name.
  • option The event option.
  • handler The event handler.
    1. payload The event payload.

void YaEventAPI.ReleaseEventsByBoundEntityId(number entityId)

Removes all event handlers bounded by entityId.
  • entityId An entity id.