Skip to content

API > LuaEvents


LuaEvents⚓︎

In game event system handler. Check out tutorial on event handlers.

Members⚓︎

Static Methods⚓︎

Name Description
LuaEvents.Add(eventName, handler) Static fieldMethod Adds the handler function to the event with name provided on eventName. The eventName doesn't need to be unique, or defined before. Throws error if handler is not a callable type. Each handler will receive a unique key (LuaEventId) which can used to remove the handler.
LuaEvents.AddLocal(script, eventName, handler) Static fieldMethod Like Add(), however this method adds the handler to the Local Event. Local event is an event only defined for individual Game Object and Script combination.
LuaEvents.GetNumberOfEvents() Static fieldMethod Returns the total number of events registered. Can be used by simplified operator: #LuaEvents;
LuaEvents.Invoke(evnetName, optional_arguments) Static fieldMethod Invokes the event which is specified in eventName. You can pass any amount of arguments.
LuaEvents.InvokeForAll(evnetName, optional_arguments) Static fieldMethod Invokes the event which is specified in eventName for all players in the session joined to the world including the sender.
LuaEvents.InvokeForMaster(eventName, optional_arguments) Static fieldMethod Invokes the event which is specified in eventName for master client and the sender.
LuaEvents.InvokeForMasterOnly(eventName, optional_arguments) Static fieldMethod Invokes the event which is specified in eventName for master client , excluding the sender.
LuaEvents.InvokeForOthers(evnetName, optional_arguments) Static fieldMethod Invokes the event which is specified in eventName for all players in the session joined to the world excluding the sender.
LuaEvents.InvokeLocal(script, eventName, optional-arguments) Static fieldMethod Like the Invoke() but invokes a local event.
LuaEvents.InvokeLocalForAll(script, eventName, optional-arguments) Static fieldMethod Like InvokeForAll() but invokes the local event that defined in the exact similar game object and Lua Script in all the clients. This requires for that game object to be existed during the build or instantiated using network methods.
LuaEvents.InvokeLocalForMaster(script, eventName, optional-arguments) Static fieldMethod Like InvokeForMaster() but invokes the local event that defined in the exact similar game object and Lua Script in all the master client. This requires for that game object to be existed during the build or instantiated using network methods.
LuaEvents.InvokeLocalNetwork(script, key, target, players[], args) Static fieldMethod Invoke a local (script+gameObject specific) event over the network.
LuaEvents.InvokeNetwork(key, target, players[], args) Static fieldMethod Invoke an event over the network.
LuaEvents.Remove(eventId) Static fieldMethod Removes a handler.

Example⚓︎

let's imagine we have two scripts. In one of them, we attach a function as a handler for an event and Invoke it in another script In script A, we create a function myEventHandler and attach it to the event OnMyEvent. script A:

do
   local scriptA = LUA.Script;

   -- define that handler function 
   function myEventHandler(message)
        Debug.Log("The Evnet handled "..message);  
   end


   function scriptA.start()
        -- attach the handler to the event. 
        eventId = LuaEvents.Add("OnMyEvent", myEventHandler);  
   end

end
In script B, we can invoke the event.

Script B:

do
    local scriptB = LUA.Script;

    function scriptB.start()
        -- invoke the event when appropiate 
        LuaEvents.Invoke("OnMyEvent", "Hello");
    end

end
You can remove the handler as well.

Script A:

do
   local scriptA = LUA.Script;

   -- define that handler function 
   function myEventHandler(message)
        Debug.log("The Evnet handled "..message);  
   end


   function scriptA.start()
        -- attach the handler to the event. 
        eventId = LuaEvents.Add("OnMyEvent", myEventHandler); 

        -- remove the handler 
        LuaEvents.Remove(eventId);
   end

end