Removing custom event listeners on instances

0 favourites
  • 3 posts
From the Asset Store
Customize the animation of character when item changed
  • I have been using custom instance events to make code more modular, and reusable. and adding event listeners has worked well, but I have a use case now where I need to stop listening to the custom event dynamically and calling the removeEventListener function on the instance does not remove the event.

    I have tried several ways, the signature of removeEventListener needs the event name and what seems to be the callback function reference.

    does the callback function need to the exact same reference of the function that was added? is there a way to remove the event without passing the same reference? I have attached an example project that demonstrates the events firing, and the listeners reacting to it, but when I try and remove the listener it does not find it in the listenerSet map and never removes it. even though it's there

    https://drive.google.com/file/d/1raLxb-q0xprDKsxlneSON7MDikwXh8ee/view?usp=sharing

    Tagged:

  • does the callback function need to the exact same reference of the function that was added?

    Yes. Two functions with the same code still count as different functions. So in your project the attempt to remove the event listener will do nothing as you have not passed the original function.

    is there a way to remove the event without passing the same reference?

    There isn't built-in support, but you could write a wrapper "disposable" class that accepts a list of events to add, and has a release method that removes all of them. This wrapper class would store the function references internally so the rest of your code doesn't have to worry about keeping function references. C3's code uses this pattern.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • that is a great idea, thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)