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.
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:
[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.
playerIdThe playerId of the target client.eventNameThe event name.optionThe event option.payloadThe event payload.
void YaEventAPI.FireToAllClients(string eventName, EventOption option, EventPayload payload)
SERVER ONLY
The server sends the event to all clients.
eventNameThe event name.optionThe event option.payloadThe event payload.
void YaEventAPI.FireToServer(string eventName, EventOption option, EventPayload payload)
CLIENT ONLY
The client sends the event to the server.
eventNameThe event name.optionThe event option.payloadThe event payload.
void YaEventAPI.FireToLocal(string eventName, EventOption option, EventPayload payload)
Sends the event to the local handlers.
eventNameThe event name.optionThe event option.payloadThe event payload.
EventDisconnector YaEventAPI.OnEventFromServer(string eventName, EventOption option, function handlerEventPayload)
CLIENT ONLY
ReturnsAn instance of EventDisconnector.
Registers an event handler to handle events sent from the server.eventNameThe event name.optionThe event option.handlerThe event handler.payloadThe event payload.
EventDisconnector YaEventAPI.OnEventFromClient(string eventName, EventOption option, function handlerEventPayload)
SERVER ONLY
ReturnsAn instance of EventDisconnector.
Registers an event handler to handle events sent from the client.eventNameThe event name.optionThe event option.handlerThe event handler.payloadThe event payload.
EventDisconnector YaEventAPI.OnEventFromLocal(string eventName, EventOption option, function handlerEventPayload)
ReturnsAn instance of EventDisconnector.
Registers an event handler to handle events sent from the local scripts.eventNameThe event name.optionThe event option.handlerThe event handler.payloadThe event payload.
void YaEventAPI.ReleaseEventsByBoundEntityId(number entityId)
Removes all event handlers bounded by entityId.
entityIdAn entity id.