I’m a professional software developer by day (mainly .net) and a hobbyist developer at any other time i can… I thought the best way to introduce myself would be to come bearing gifts of code
So here it is AppEvents - a very small framework which lets you simply define events and use them in a flexible way.
For those who aren’t sure what Events are, they are something which allows us to specify a function to call when something is about to happen, is happening or has happened in code, like a button being pressed, or externally like the user touching the screen.
You can create single events or collections of events as fields on your classes which are then consumable in a global context or a more limited/isolated scope.
ExampleEventObject = class() function ExampleEventObject:init() -- globally tracked event self.exampleEvent = AppEvents.define("example_event",self) -- limited scope / local event self.exampleEventLocal = Event("Example_event_local",self) -- globally tracked events collection (which can contain multiple events) self.events = AppEvents.track(self) self.exampleEvent1 = self.events:define("example_event") -- in this context (self.exampleEvent == self.exampleEvent1) = true -- limited scope / local events collection self.localEvents = Events(self) end
function setup() obj = ExampleEventObject() obj.exampleEvent:bind(example_event) obj.events:bind("example_event",example_event) end -- handler / callback args can be a fixed number or dynamic function example_event(sender,...) -- called when "example_event" is raised / triggered end
You can also use events in a similar way to the Events class in Cargo-Bot (which was partly my inspiration for this).
ExampleEventObject = class() function ExampleEventObject:init() end function ExampleEventObject:bindEvents() AppEvents.Global.bind("play",ExampleEventObject.play,self) end function ExampleEventObject:play(sender,val) end
I’ve commented the code quite extensively (perhaps even excessively in places) and there lots of examples in Main and ExampleEventsObject so I encourage you to take a look for some more detailed examples!
Edit: Fixed issue where runtime error messages would be suppressed inside event handlers. Altered to xpcall and print stacktrace if things go wrong :). Also added global to Event to turn off using dispatchers.