Using custom events to make game objects communicate

I just wanted to drop a quick mention of the “object:dispatchEvent()” API into this thread, whether or not it relates directly to what you want to do. This API can be an unsung hero, but few people seem to even know it exists:

https://docs.coronalabs.com/api/type/EventListener/dispatchEvent.html

The reason I like this is because it’s extrememly customizable. You can basically make anything into an “event” and pass any number of custom parameters along with it, then conditionally check those parameters in the listener function. I urge you to check it out, experiment with it, and see how useful it can truly be.

Brent

Hi Brent,

I did see that in my search, but unless I am mistaken, it requires an object to be the sender, look at the last line in the example.

The example is also a bit confusing, because the sender is the same object that is the listener, sure it works, but i think normally it would be a *different* object… but anyways…

That solution by that roaminggamer guy does not require an object to be the sender. You can imaging that oft times, you would want to send an event to several objects, but the sending of the message is not related to a display object. Does it make sense? 

why don’t you just steal his code and put it in the next release.

 

Don’t forget that you can also dispatch events to the Runtime, and have function then do what is necessary, e.g. by going over a table containing all your enemy objects and deleting them.

For your information: I have wasted tons of time by trying to have objects and events all communicate with eachother directly without keeping tracks of them in lists. It was, in the end, much better to keep track of things in lists and table, and work with those.

I’ve used this method to implement event dispatching for my Corona client mods, but I’m pretty sure it’s specific to display objects. I basically set up an empty group and use it as a dispatcher.

Now, if could we have a real Emitter class to work with, that would be the best. We have high level GPU rendering possibilities, but no ‘real’ Emitter class (non-graphic).  :ph34r:

I’m not a huge fan of using the Runtime listener, but it’s sometimes the only option internally.

Anyway, I use this all the time now.  https://github.com/daveyang/EventDispatcher

Perhaps it will be helpful to others.

Cheers.

What do you mean Emitter class? Are you talking about particle emitter or “event” emitters? If it’s the first, why not create your own using some light OOP?

I actually prefer the Runtime listeners. It allows me to keep most of my event-based functions in one module that contains the code to communicate with all of the other objects.

It’s interesting to see how these talks wind and weft.

I’ve yet to investigate Chris’ suggestion, but for anyone reading this, my code above is basically a short-hand wrapper for the Runtime:dispatchEvent() and related features.

I prefer it because it is shorter and clearer:

listen( "eventName", obj ) -- vs Runtime:addEventListener( "eventName", obj ) ignore( "eventName", obj ) -- vs Runtime:removeEventListener( "eventName", obj ) post( "eventName", { more = "data", evenMore = 1 } ) -- vs Runtime:dispatchEvent( { name = "eventName", more = "data", evenMore = 1 } ) 

Hi,

Semi-relevant still I think…I put together this mod called Eventable.  It’s like a chat room for your tables and mods. Any testing would be appreciated. It’s pretty alpha though.

http://develephant.github.io/Eventable/

Cheers.