Back in RGSS3, there's a function in array where you can do dispose:
class Game_Temp attr_accessor :r_effect_sprites alias r_effect_initialize initialize def initialize r_effect_initialize @r_effect_sprites = [] end def dispose_r_effect @r_effect_sprites.each{|sprite| sprite.dispose} @r_effect_sprites = [] endendI noticed that it disposed the sprites being pushed, is it the same with removeChild?
This line of code in particular:
@r_effect_sprites.each{|sprite| sprite.dispose}How well do you translate it in JavaScript?
Looking through the default scripts, I don't see any images being disposed of. I suspect it's relying on the garbage collector, which gets rid of anything that is no longer referenced.
I'm curious to see if there's a "right" way.
actually...dispose concept just not exist in MV because it's take times to load so they need to keep in memory once they are loaded. it's the browser issue sadly.
because if each time you dispose a picture you erase it and in MV that's would not work wells
the ways of "dispose" a sprite in a ways is to setup the bitmap like this :
this.sprite.bitmap = ImageManager.loadsomething('');that's would stop getting the picture though it's also necessary to make it invisible for avoid any can't get noname.png picture error by setting like this :
this.sprite.visible = false;or setup method who STOP the system from drawing the bitmap.
like this method I done in my bust script :
//----------------------------------------------------------------------------// ○ new function: drawMessageBust//----------------------------------------------------------------------------Window_Message.prototype.drawMessageBust = function() { if ($gameMessage.bustName() != ""){ this.spritebust.bitmap = ImageManager.loadBust($gameMessage.bustName() + '_' + $gameMessage.bustIndex()); this.spritebust.visible = true; }else { this.spritebust.bitmap = ImageManager.loadBust(""); this.spritebust.visible = false; }}; but material are auto "removed" but keep in memory at the end of the scene.
but yeah this.removeChild(varname); is the way to erase totally from the system memory.
Though disposing in MV would provoke lags use "visible" for make something invisible.
First, I don't have any expertise on this matter and just looked through the plugin system a bit, so what I'm telling might be completely false:
In the previous makers, disposing a sprite was necessary in order to remove the sprite from the internal "to draw" list. Otherwise the sprite would continue to appear on the screen until the garbage collector cleaned it up, which could take a while.
In MV, sprites have to be added to the current scene instead (using the addChild method). Once a scene is replaced the sprites attached to it are no longer drawn to the screen, so you don't have to dispose them explicitely.
MV uses canvas to render images, which internally are just ordinary <img> html tags. They are cached on the user's pc, just like any other image on any website you visit (at least that's how i see it, WebGL might be a bit different).
Thanks for all the info. I might need to read more, I'll check on it later how it works.
We have concepts of push (SceneManager), pop (SceneManager), addWindow (Scene), addChild(Scene and Sprite) and I believe removeChild (Scene and Sprite)
I've been told that WebGL objects are not disposed unless you explicitly dispose them, so even if the sprite itself is disposed, the underlying webgl object will stay around.
But I have not checked this myself.
usually garbage collection will kick in when you call "remove" or a "dispose" function ... if it stuck around that would (or could) cause performance issues.
How about new sprites defined? For example, I noticed on Scene_Gameover that the sprite being called there was never, 'removed' or 'disposed'. If I create a new sprite in the title screen or in the menu, should I still do the same thing like back in RGSS where I have to remove it during termination, or just leave them like that?
I don't think it's because they got lazy and didn't clean up. From the looks of it, they've pretty much just converted the same logic in Ace from Ruby to Javascript - classes are the same, method names are similar, most of the logic seems to be similar. So to NOT dispose of a sprite in Javascript, when it was specifically done in Ruby, seems to be a deliberate decision, and I assumed that decision was that JS handles garbage collection differently and they chose to hand over that responsibility to the JS garbage collector.
I'm not saying the way they've done it is right - reports of lagging after changing scenes, and memory leaks, might indicate that it's not.
Even though a scene is cleaned up, it doesn't mean the resources that were used by that scene are cleaned up - they're still in the cache, unless there's something that removes them.
That is my worry Shaz, because in a plugin I am writing, there's like 17 custom sprites running, and I don't find any way to remove or dispose them correctly, only by removeChild and that's it :/
It is still in the cache, which means it won't be removed.
Rather ... the sprite will be removed, but the bitmap that was attached to the sprite will still be in the cache, so you're really not getting much back, as that's what's taking up all the memory.
its removed when the scene is changed ...
The bitmap in the cache? Where is it removed? I'm not seeing it.
The only thing that removes cached images is ImageManager.prototype.clear, and that's not called from anywhere.
Just remove your references to the sprite object, so that the reference to its bitmap instance is also removed. Using removeChild will remove it from its parent's update queue, but if you have other references to it -- like anoter variable -- you want to make sure they're cleared too. Clearing a variable reference would be something like
_mySpriteReference = null; Then there's no reference to the bitmap object, and its instance is cleaned up by the GC. Of course the bitmap will still be in the cache -- we want that, for faster loading when/if we want to instantiate instances of it again.