'dispose' in JS is removeChild?

Discussion in 'Learning Javascript' started by Milena, Nov 14, 2015.

    Tags:
  1. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    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?
     
    Last edited by a moderator: Nov 14, 2015
    #1
  2. Shaz

    Shaz Veteran Veteran

    Messages:
    37,348
    Likes Received:
    11,145
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    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.
     
    #2
  3. nio kasgami

    nio kasgami VampCat Veteran

    Messages:
    8,590
    Likes Received:
    2,340
    Location:
    Canada / Quebec
    First Language:
    French
    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.
     
    #3
  4. Another Fen

    Another Fen Veteran Veteran

    Messages:
    519
    Likes Received:
    234
    First Language:
    German
    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.
     
    Last edited by a moderator: Nov 14, 2015
    #4
    Milena likes this.
  5. Iavra

    Iavra Veteran Veteran

    Messages:
    1,797
    Likes Received:
    856
    First Language:
    German
    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).
     
    #5
    Milena likes this.
  6. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    Thanks for all the info. I might need to read more, I'll check on it later how it works.
     
    #6
  7. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    First Language:
    English
    We have concepts of push (SceneManager), pop (SceneManager), addWindow (Scene), addChild(Scene and Sprite) and I believe removeChild (Scene and Sprite)
     
    #7
    estriole likes this.
  8. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,057
    Location:
    Toronto
    First Language:
    English
    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.
     
    #8
  9. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    First Language:
    English
    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.
     
    #9
  10. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    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?
     
    #10
  11. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    First Language:
    English
    Some sprites and assets will stick around at the game or pixis discretion. It might also be the fact that the original programmers got lazy and didn't clean up. But generally when you switch from scene A to scene B the only one, if it is popped off the array of scenes is cleaned up. The same goes for sprites and windows.

    For example, open up the menu, open up items you now have about 2 -3 scenes running. Close the items. GC (garbage collection) has cleaned up the item scene. close menu, same thing. switch maps. same thing for all maps and sprites that were on that map.
     
    #11
  12. Shaz

    Shaz Veteran Veteran

    Messages:
    37,348
    Likes Received:
    11,145
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    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.
     
    #12
  13. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    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 :/
     
    #13
  14. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    First Language:
    English
    removeChild will dispose of the sprite properly. Once its no longer on the scene the GC is called and its cleaned up. 
     
    #14
  15. Shaz

    Shaz Veteran Veteran

    Messages:
    37,348
    Likes Received:
    11,145
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    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.
     
    Last edited by a moderator: Nov 16, 2015
    #15
  16. DarknessFalls

    DarknessFalls Rpg Maker Jesus - JS Dev. Veteran

    Messages:
    1,393
    Likes Received:
    209
    First Language:
    English
    its removed when the scene is changed ...
     
    #16
  17. Shaz

    Shaz Veteran Veteran

    Messages:
    37,348
    Likes Received:
    11,145
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    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.
     
    #17
  18. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    Is there an approach to dispose the bitmap? From what I know, this caused so much problem back in Ace. It might lag in a mobile version if these sprites just stays there for some reason.
     
    #18
  19. Galenmereth

    Galenmereth I thought what I'd do was Veteran

    Messages:
    2,206
    Likes Received:
    1,905
    Location:
    Norway
    First Language:
    English
    Primarily Uses:
    RMMV
    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.
     
    #19
    Heartbreak61 and Milena like this.
  20. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    I'll be using that :) thanks. What I was doing back then was just removing it via the removeChild, and I am having doubts if I was really removing it or just taking it out there, but the bitmaps still on the window.
     
    #20

Share This Page