Documentation
GitHubTwitter
  • overview
    • What is Lua++?
    • Why Lua++?
  • language improvements
    • Compound Assignments
    • Postfix and Prefix Operators
    • The Continue Statement
    • Constant Variables
    • Type Annotations
      • Function Annotations
    • Classes
      • Constructors
      • Templating
      • Inheritance
      • Encapsulation
    • Events
    • Macros
      • Lenient
      • Comment
      • C-Arrays
  • contributing
    • Installing the Project
Powered by GitBook
On this page

Was this helpful?

  1. language improvements

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
PreviousEncapsulationNextMacros

Last updated 2 years ago

Was this helpful?