Can my game throw runtime events?

Hi,

I’ve read about events in several blog posts and I have a scenario and a question:

To decouple my game component, I want to register them for various custom events and when I need to trigger them, throw those events.

What I understood Corona suggests is table listeners, in other words, meaning that you throw an event on a table/object or any amount that you want.

It doesn’t suit my need because A) I have to remember that which objects needs this event and B) I don’t want to store reference of those table listeners in every scenario that I want to throw event.

So I thought of Runtime events and leads to my question:

Is there any way that I can register my tables/objects to Runtime events, such as we normally do with “touch” event and whatnot, and then when my game needs, I throw those events at Runtime and each of the objects that registered themselves for Runtime, grab the event and handle it?

Example use case:

When one of objects gets hit by a bullet, I throw a “MonsterGotHitWithABullet” and as I previously registered my UI, SFXmanager, FoodSpawner, etc to this event, they all receive this event and each respond accordingly.

Thanks.

Yes, you can generate your own events and listen for them specifically: http://docs.coronalabs.com/daily/api/type/EventListener/dispatchEvent.html

Be aware that if you try to simulate touch or tap events that they will  not propagate down through the display hierarchy.

Thanks, for future reference this can be done:

local function invIncreased(event) print("handle inventory increase from" .. event.target) end local image = display.newImage("image.png") Runtime:addEventListener("MyInventoryHasIncreased", invIncreased) local event = {name="MyInventoryHasIncreased", target=image} Runtime:dispatchEvent(event)

One more thing, at end of the game that all objects are destroyed, how should I remove these listeners?

(Because we know that Runtime event listeners are never removed automatically)

Like this?

Runtime:removeEventListener("FoodWentOutOfTheScreen", ComboManager)

Yes

Yes, you can generate your own events and listen for them specifically: http://docs.coronalabs.com/daily/api/type/EventListener/dispatchEvent.html

Be aware that if you try to simulate touch or tap events that they will  not propagate down through the display hierarchy.

Thanks, for future reference this can be done:

local function invIncreased(event) print("handle inventory increase from" .. event.target) end local image = display.newImage("image.png") Runtime:addEventListener("MyInventoryHasIncreased", invIncreased) local event = {name="MyInventoryHasIncreased", target=image} Runtime:dispatchEvent(event)

One more thing, at end of the game that all objects are destroyed, how should I remove these listeners?

(Because we know that Runtime event listeners are never removed automatically)

Like this?

Runtime:removeEventListener("FoodWentOutOfTheScreen", ComboManager)

Yes