Resource Management
Proper resource management prevents memory leaks and ensures smooth performance in your extensions.
The Problem
Creating skins and drawables without cleanup leads to:
- Memory leaks
- Performance degradation
- Resource exhaustion
- Rendering glitches
Solution: Lifecycle Management
Listen for Project Events
class MyExtension {
constructor(runtime) {
this.runtime = runtime;
this.customSkins = new Map();
this.customDrawables = new Map();
// Clean up when project stops
runtime.on('PROJECT_STOP_ALL', () => {
this.cleanup();
});
// Clean up when project starts (optional)
runtime.on('PROJECT_START', () => {
this.cleanup();
});
}
cleanup() {
const renderer = this.runtime.renderer;
// Destroy all drawables first
for (const [name, drawableId] of this.customDrawables) {
renderer.destroyDrawable(drawableId, 'sprite');
}
this.customDrawables.clear();
// Then destroy all skins
for (const [name, skinId] of this.customSkins) {
renderer.destroySkin(skinId);
}
this.customSkins.clear();
}
}