   In game Event Manager. (JS)
  I am working on an in game event system, and I was hoping someone out there could let me know if I am onto the correct path.I haven't formally studied event systems in school yet, I just came up with the idea one day while I was working on my 'kill 10 rats' paradigm.  Then after a bit of research, I saw that a lot of hardware events work in a similar way.The way I am handling it, is I call a 'registerListener' function, passing in the event string name, the object creating the listener, the Callback, and a boolean if it is a one time event or if it is to be repeated.registerListener(String _eType, Object _obj, Callback _callback, bool _ot);When I unregister the listener, I call unregisterListener() and I pass in the event type, the object and the callback.The listener 'hashmap' uses the event type as the keys, and then uses an array to store the Objects listening for the event.  In &#106avascript you do not have access to the memory reference, otherwise I would use the memory address.  However, Javscript Array objects CAN distinguish the difference between two identical objects within an Array, so this is why I set it up like this.When you raise an event, I call raiseEvent() passing in the event type (a string) and any data associated with the event.So now what happens is if the event type is a key in the listeners hashmap, for-each object in the array, I call object.callback(data);I have ran through NUMEROUS 'what if' situations, and it works out quite well, I just wanted to see if this was close to what other people have done, or if I should be trying to go about this a better route, or....?Please let me know.   
  I've had great success using an event-based system in c++. I have to use libs like boost signals and boost bind to go about it. Have you considered eliminating the object parameter through use of of closures a la a anonymous function?   ;  It's usual just to take a callable object of some sort for the callback. The dispatch code just calls it with no arguments, and if the user wants to use some object method, s/he can just write something like 'this.method.bind(this)' for that parameter. That way, the user also has the flexibility *not* to use an object method :)   ;  Thats pretty good,here is a link to an example done in Lua/C++. maybe you can find something in there useful or comparable. It is a tiny bit more simple where it doesnt pass the objects along, but it is certainly scalable (you can easily add that in).   ;  Ok here is my final - rough draft. :p[source lang = "javascript"]function EventManager(){var listeners = {}; //JS version of hash map//helper function to make sure array is emptyfunction isNull(_element, _index, _array){return _element == null;}//register a listener for an object 'action' is both the event//to listen for, AND the callback function :pthis.registerListener = function(_obj, _action){if(!listeners[_action]){listeners[_action] = [];}if(listeners[_action].indexOf(_obj) == -1){listeners[_action].push(_obj);return;}}//unregisters the listenerthis.unregisterListener = function(_obj, _action){if(listeners[_action]){try{listeners[_action].splice(listeners[_action].indexOf(_obj), 1);}catch(e){}if(listeners[_action].every(isNull)){ //where i use the helper functiondelete listeners[_action];}}}//raises an event obj is the object raising the event, action is the event//which doubles as the callback, and data is any optional data you might wantthis.raiseEvent = function(_obj, _action, _data){if(_action in listeners){var i1 = listeners[_action].length-1;do {//((arrayName.length-1)-i)listeners[_action][((listeners[_action].length-1)-i1)][_action](_data);}while (i1--);}}}What do you guys think?  I read that a reverse do-while loop is faster than a standard for-each loop in &#106avascript, so that is why I am using it.<br><br>Now I can raise an event, without having to pass in an object if I dont want to (just pass in 'null').   