Event is a great pattern in the sdk.
as the doc depicted:
ClassA = gideros.class(EventDispatcher)
ClassB = gideros.class(EventDispatcher)
function ClassA:funcA(event)
print("funcA", self, event:getType(), event:getTarget())
end
local a = ClassA.new()
local b = ClassB.new()
b:addEventListener("myevent", a.funcA, a) -- when b dispatches an "myevent" event,
-- a.funcA will be called with 'a'
-- as first parameter
b:dispatchEvent(Event.new("myevent")) -- will print "funcA"
However,when it comes to make some common component,such as a score bar,I think broadcast event is more useful.
For example,when some object dispatch an event "addScore" with a parameter 10 without care about who will handle it,
just call
event = Event.new("addScore")
event.bonus = 10
b:dispatchEvent(event)
and on the receive side
self:addEventListener("addScore", self.funcA, self)
function ClassA:funcA(event)
print("funcA", self, event:getType(), event:getTarget(), event.bonus)
end
So ClassA and ClassB are indepent,without reference each other,just focus on the event.
Comments
If I do this, dispatching the event from gHud, it works:
Am I missing something?
Also note that you can register multiple functions for a single event and remove them individually giving you a greater degree of control over what gets executed during a particular event. (Maybe you want to register 2 listeners for a single event but at some point remove one of them. Might be a hastle to do with if statements.)
Also I have a class for global broadcasting of events that has the method Broadcaster.lua which I use. You have to add a listener object via
The class is pretty simple and holds objects in weak tables (don't add to memory reference count of object) and can be extend to maybe add "channels" so that events can be sent to certain groups of objects. Attached is the class.
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
Thank you so much for sharing your broadcaster class. It looks like a clean pub/sub model. My next step was going to be to write something similar, so this will save some typing, testing, and head scratching.
I'm pretty new to both Lua and Gideros, you just saved me a lot of heartache. Now to get back to game logic...
Changes:
1)Removed self.events table which was a leftover from a previous iteration
2)Table keys are now weak and objects can only be registered once (Previous version allowed mistakenly adding same object multiple times)
3)Performance improvement when adding lots of objects(No longer cycles through all numeric indeces to find a free one)
4) Can register virtually unlimited amounts of objects
Note: The class inherits from EventDispatcher but doesn't actually use any of its methods or functionality directly on itself. It's up to you if you want it to be a bare bones class or inherit if you want to add listeners directly to it.
Likes: bali001
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova