Events

The event keyword is used to declare an event in a publisher class. The following code segment displays how to declare and raise an event.

class Publisher {
    event SampleEvent: (sender: any, text: string),

    function RaiseSampleEvent(text: string)
        SampleEvent.Invoke(self, text)
    end
}

Events can only be invoked from within a class, so they can be extremely useful when implementing a customizable API or library. One can assign a function to be called whenever the publisher class raises the event by doing the following:

function OnInvoked(sender, text: string)
  print("SampleEvent has been invoked!")
end

local publisher: Publisher = Publisher()

publisher.SampleEvent += OnInvoked

Last updated