Replies: 3 comments 2 replies
-
Alternatively, (and I'm not keen on this either) is for the event handler to check the object type of the scope and automatically add an on destroy callback on the script to remove the subscription. However, this relies on the scope being passed and extra bespoke logic on the event handler. |
Beta Was this translation helpful? Give feedback.
-
I believe regardless of solution, it is an education issue, as destroying resources to clean up is a known concepts in realtime applications. Unfortunately in engine event emitter does not return event handlers when subscribed. Which would simplify this: let evt = app.on('event', callback);
// ...
evt.off(); I would start from modifying default script and add this: MyScript.prototype.destroy = function () {
// called when script is destroyed
// remove related resources and references
// and unsubscribe from external events
}; Something as we do with |
Beta Was this translation helpful? Give feedback.
-
Thanks to @Maksims, this is now supported by the engine, so we can probably close this. |
Beta Was this translation helpful? Give feedback.
-
There have been many forum posts over the years of users changing scenes or destroying entities that have scripts subscribed to global events such as mouse or touch input but forgetting or not knowing that they have to unsubscribe to them.
ie: https://forum.playcanvas.com/t/screen-to-world-undefined-when-switching-scenes/16592/7
I was thinking maybe there should be a script API that can handle subscribing to these events and automatically unsubscribe if the script is destroyed.
e.g
I don't like that it adds another way to subscribe to an event though. Any ideas on how we can this more simpler to use and less error prone?
Beta Was this translation helpful? Give feedback.
All reactions