Events.
LadyCailinBot opened this issue ยท 5 comments
CMDHELPER-2948 - Reported by Lildirt
I'm unaware if this is planned (or even possible), but I'd like to see the ability to add in customized events that can be freely registered and triggered.
For example, something like register_event('myCustomEvent', array('possible', 'metadata', 'array')) and trigger_event('myCustomEvent', array('possible', 'metadata', 'array')).
I can already think of many possibilities for this, especially considering how much easier this could make life for developers using MethodScript.
Note: I am looking at this in a Minecraft scope, but this could definitely be useful outside of that, in a standalone environment.
Comment by Lildirt
I'll elaborate on how I had it thought out.
My idea was to have three functions. These would be register_event(STRING, ARRAY), trigger_event(STRING, ARRAY), and unregister_event(STRING). The "register_event" function would work incredibly similar to the "bind" function.
When you used register_event, this would add in an event similar to how I've witnessed in Java plugins (look at MobArena, for instance, it has ArenaStartEvent .. or something of that nature). These events would be entirely kept within CH code and not actually be registered events on the server.
I'll create an example.
Say you have a "minigame"-style script you'd like to use. Okay, well, say that game could have multiple endings to it, so I would use the register_event function to register an event for the game ending, which would probably be done at compile time in this scenario.
This would allow you to simply use the trigger_event function to trigger that event you registered before, once again, optionally filling in the metadata array with information for the event to process.
Here's a code example: http://paste.thezomg.com/13939/15262139/
Does this make any sense, at all?
Comment by PseudoKnight
Give an example where a simple procedure wouldn't work.
http://paste.thezomg.com/13949/25678139/
I believe the only reason custom events exist in plugins like MobArena are for use as an API. So an actual case might be like this: http://paste.thezomg.com/13950/13995271/
Perhaps not the best example. Essentially, events might be useful if there's a need for one script A to not know about script B, but script B wants to do something when something in script A happens. (without having to use an interval) Script A author could use events for anyone to use.
I was actually looking at the proposed [Routines|http://wiki.sk89q.com/wiki/CommandHelper/Staged/Routines] months ago, and the only use case I could think of was something along the lines of an event system by waiting for a lock to change.
Comment by PseudoKnight
It's like a procedure that has zero or more definitions that all run when called and don't need to be included.
Comment by LadyCailin
Since you can't hook into native events, to have a true event system, you would need a polling loop of some sort, which is terribly inefficient. Additionally, you can already do this in pure mscript, without a native function set, and with very little code:
proc(_register_event, @name, @closure){
@events = import('events');
if(!is_array(@events), @events = array());
if(!is_array(@events[@name]), @events[@name] = array());
@events[@name][] = @closure;
export('events', @events);
}
proc(_trigger_event, @name, @data){
@events = import('events');
if(!is_array(@events), @events = array());
if(array_contains(@events, @name) && is_array(@events[@name])){
foreach(@closure in @events[@name]){
execute(@data, @closure);
}
}
}
There's the basis of an event system, but the question is, what events are you going to trigger? Either it's a poll loop (which is inefficient) or it's going to be based on an existing event, in which case you might as well go ahead and use that.
Assuming those functions DID exist however, what is the actual use case you have in mind? What would actually trigger the listeners?