Quintus icon indicating copy to clipboard operation
Quintus copied to clipboard

passing arguments to event handlers

Open wirthmi opened this issue 12 years ago • 4 comments

Hello,

I've noticed that in Core Quintus Basics chapter of Guide accessible on Quintus website is written that up to 3 arguments can be passed to event handlers when triggering some event. It doesn't work. According to implementation of trigger() method in Evented class only one argument can be passed to handlers. IMHO possibility to pass up to 3 arguments would be nice and handy.

wirthmi avatar Jan 15 '14 13:01 wirthmi

The whole documentation needs an update...

jerone avatar Jan 15 '14 22:01 jerone

trigger: function(event) {
      // First make sure there are any listeners, then check for any listeners
      // on this specific event, if not, early out.
      if(this.listeners && this.listeners[event]) {
         var args = [].slice.call(arguments, 1);
        // Call each listener in the context of either the target passed into
        // `on` or the object itself.
        for(var i=0,len = this.listeners[event].length;i<len;i++) {
          var listener = this.listeners[event][i];
          listener[1].apply(listener[0], args);
        }
      }
    },

I think if the trigger method is changed to above code than any number of arguments can be passed.

qsrahman avatar Apr 27 '14 13:04 qsrahman

In this implementation the strain on the GC is too much - trigger is called a lot (thousands of times a second in many games), so calling slice (which creates a new array with the result each time) will result in lots and lots of objects being created (even if [].slice was rewritten as Array.prototype.slice)

cykod avatar Apr 27 '14 13:04 cykod

you are right :) thanks for the info.

qsrahman avatar Apr 27 '14 13:04 qsrahman