Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Send custom events with EventDispatcher
Started by qwidave Aug 19 2014 10:37 PM

* * * * * 1 votes
2 replies to this topic
event dispatcher eventdispatcher listener oop custom event objects
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Pro
  • PipPipPip
  • 32 posts
  • Advanced Member

In order to broadcast messages to event listeners, the event dispatcher is limited to either the global Runtime object or display objects. This limitation places messaging in the two scopes instead of between objects that are supposed to be talking to each other. This just doesn’t feel right to me; so I’m releasing yet another EventDispatcher, perhaps other developers may find it useful too.

Those who came from the good old Flash 5 days may remember FLEM (FLash Event Model). It was the first listener event model for Flash and ActionScript, and was created by Branden Hall and further developed and maintained by me. I’ve adapted the basic event model mechanism found in ActionScript 2/3 to Lua. This EventDispatcher has a similar API as the listener model in Corona SDK, with some extra features thrown in (such as optional extra parameters when dispatching events, and returning status).

EventDispatcher provides custom event broadcaster/listener mechanism to regular Lua objects, it works as regular Lua 5.1 / 5.2 code, in Corona SDK, and likely other Lua-based frameworks.

Basic usage:

local EvtD = require "EventDispatcher"

local listener = {
    eventName = function(event)

local broadcaster = EvtD()

broadcaster:addEventListener( "eventName", listener )
broadcaster:hasEventListener( "eventName", listener )
broadcaster:dispatchEvent( { name="eventName" } )
broadcaster:removeEventListener( "eventName", listener )
More info here: http://swfoo.com/?p=632


Dave Yang - Quantumwave Interactive Inc.

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Pro
  • PipPipPipPipPipPip
  • 415 posts
  • Jedi

As a former Flash developer, this is awesome!



[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Enterprise
  • PipPipPipPipPipPip
  • 271 posts
  • Jedi

Dave this is awesome!! Who needs FSMs when you got event-driven stuff?! I can't wait to really play around with this!!




Also tagged with one or more of these keywords: event, dispatcher, eventdispatcher, listener, oop, custom event, objects