Best way to generate custom events?


#1

I was looking around in the documentation and saw the page on events but it didn’t say anything about how to emit non-predefined events. From the source it looks like all the events are just dynamic functions, but I was hoping for something that uses a subscribe/publish/unsubscribe pattern.

What’s the best way to do this?


#2

Heaps do not have built-in event system like EventDispatcher in OFL as far as I’m aware. You can implement the system yourself (it’s not that hard, honestly) or use other solutions, like msignal library (it requires some fixes if you are using Haxe4). Best way would be the one that suits your case :wink: I personally use both msignal for events that may have multiple listeners and dynamic functions for one that are just callbacks.


#3

Oh this is great, thanks! Can you say what changes msignal requires for Haxe 4?


#4

Just fixing properties if I recall correctly. Msignal uses deprecated function naming instead of get / set. E.g.:

public var listener(get_listener, set_listener):TListener;

will have to be changed to

public var listener(get, set):TListener;

#5

To follow up on this, I just saw that Flixel has its own native typed event system. I may see if I can port this as a Signal thing for Heaps directly. Using an external lib for this feels like a little much, I think it should be built in.