Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Scene Manager: what is garbage collected and what isn't when transistioning in out of of scenes — Gideros Forum

Scene Manager: what is garbage collected and what isn't when transistioning in out of of scenes

scurvyratscurvyrat Member
edited September 2012 in General questions
I'm using the Scene Manager to move between various scenes in my app and am confused on what is garbage collected and deleted from memory and what isn't.

I've been explicitly adding and removing event listeners when going in between scenes. Is this necessary?

Bitmaps do not appear to be removed/garbage collected (ie if I place one on and transition back its still there). So does that mean on the enterBegin handler for that scene I should not re-add them if I'm going back and forth after the first time?

UIKit.mm elements are not automatically removed and need to be explicitly removed with removeFromRootView.

Is the sprite defined at the top of a scene effectively the stage?

scene1 = gideros.class(Sprite);

because when I do a self:addChild I don't seem to also do a scene1:removeChild but have to use stage:removeChild.

Thanks!

Comments

  • Hello,
    well objects are not removed from memory in the same instance, it takes time to garbage collect them

    Bitmaps do not appear to be removed/garbage collected (ie if I place one on and transition back its still there).
    Is it possible that you simply put it back on scene init?
    or are you sure you are adding image to scene?
    can you post some code, so we could comment on it ;)

    UIKit.mm elements are not automatically removed and need to be explicitly removed with removeFromRootView.
    Plugin items are completely different issues and can't be handled inside scenemanager, cause they are not part of Sprite hierarchy.


  • scurvyratscurvyrat Member
    edited September 2012
    Thanks for the quick reply!

    Here is some code:
    function scene1:onTransitionInBegin()	
      image1:setPosition(10,180)
      self:addChild(image1)
    end
    So if I then do:
    function buttonClick1(event)
      sceneManager:changeScene("scene2", 0.5, SceneManager.crossfade, easing.linear)
    end
    and then transition back to scene1 from scene2.

    Should image1 be added back to scene1 with addChild because it been removed automatically?

    Or is it already there and I'm now adding it twice?

    It appeared sometimes and then others not if I tried to do a check with
    if not stage:contains(image1) then
    maybe the garbage collection hadn't happened yet for some of the time and others it did?
  • DaleDale Member
    edited September 2012 Accepted Answer
    I share some of your confusion about the SceneManager.

    I thought each scene would have the ability to be persistent, and switched between, pausing and unpausing but not being destroyed and reinstantiated on scene changes. The latter seems to be the case; from what I've seen in the code, it creates a new Scene2 each time you switch to Scene2. Scene1 is kept around by reference until it is replaced by Scene2 as the "from" Scene (I think).

    I expected to be able to have Scene objects initially created, then notified when a transition is occurring, that they could retain their state between scene changes.

    For some Scenes it may make sense to build/destroy them on every transition, but for the core gameplay scene, for example, destroying it and rebuilding it on every transition to or from it, is awkward and inefficient at times.

    The Scenes themselves are really Sprites, that get addChild'd to the SceneManager object (a Sprite itself), and removed when transitioned from. Your Scene is a sprite and should just add objects to self in the :init() method.

    Or, in the enterBegin event. If you're about to be transitioned into, that's a good time to add things to self. You want them there to be visible during the transition.

    On enterEnd, is where I found it best to add the event listeners for touch, mouse, ENTER_FRAME, etc.. After your transition is done, then respond to events.

    On exitBegin, we're about to transition away. This is the parallel to enterEnd, so this is where I unhook all my event handlers. You shouldn't respond to UI touches during a transition.

    On exitEnd, we've been transitioned away from, and we can removeChild any sprites we created (or let them be garbage collected eventually).

    (The source to SceneManager:ChangeScene, around line 256, is quite simple, take a look to see what it's doing.)

    Now another beef with SceneManager. It dispatches the various events, and they don't seem to happen right away (enterBegin and exitBegin are actually dispatched in an ENTER_FRAME handler). So the Scene manager is able to initiate a transition (whose ENTER_FRAME event occurs) before the enterBegin event ever gets dispatched. This happened to me, and my initialization code in the enterBegin handler took a fraction of a second, which ended up taking long enough to miss animating the whole transition!

    I added onEnterBegin, etc., callbacks to the SceneManager code for my own use; these are called apporopriately such that enterBegin can perform it's work *before* the frames of the transition start to be displayed. If there's any interest I'll post the diffs. (Perhaps just moving the enterBegin/exitBegin dispatch out of the frame handler and into changeScene might be sufficient.)

    I would still like to enhance SceneManager further so it keeps (or has the option to keep) a scene persistent between multiple transitions.

    -d
  • @Dale, Thanks for that explanation. Could you pls post the diffs to scene manager. I've seen some issues with transistions where not everything on the new scene is there when it transistions over.
Sign In or Register to comment.