Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Is there a way to kill objects with eventListeners (Scene Manager) — Gideros Forum

Is there a way to kill objects with eventListeners (Scene Manager)

YanYan Member
edited February 2016 in Bugs and issues
Im going rage and mad with this damn thing. When i change scene in scene manager, the previous scene keeps running, WHY AND WHAT A HELL, im doing remove events from previous scene, im doung removing events from world in previous scene, im doing killing objects with events from previous level, AND THIS HELL STILL NOT ENOUGHT, how was this stupid born ? And how to fix this, i hear sounds from previous levels, therefore i sould find and stop all sounds, level management makes me crazy !
vk.com/yan_alex

Comments

  • Why do I should care about event of objects that I want to DESTROY ??? This is megastupidapproach, isnt IT ???
    vk.com/yan_alex
  • Calm down, it takes some practice :) Read this and start from there, it should be straightforward. Just make small changes and you will progress slowly but surely ;)

    http://giderosmobile.com/forum/discussion/480/gideros-game-template/p1
  • I have already platformer with calculated shadows and etc, but when i started including the level manager i found this bug, your answer has nothing about managing objects
    vk.com/yan_alex
  • I use very lot of event listeners for diferent needs, and I really cant remove all of them, because all objects has own listener for different tasks, thi is stupid if I shuld remove All listeners from all objects, this is real heavy and stupid task
    vk.com/yan_alex
  • Enemies has event listeners, doors has event listeners, spikes and so on, do I really should to remove AAAAAAALLLL listeners, before I can damn quit level ???
    vk.com/yan_alex
  • By the way I have timers on my enemies for doing patroling and so on, this is very bad idea to remove all of them, because its heavy task to manage all this operations, instead just killing whole world with all objects, i just need to remove all objects three, thats it. Why this task is so complex and stupid ?
    vk.com/yan_alex
  • hgy29hgy29 Maintainer
    @Yan,

    It looks like you are not very familiar with GC managed engines, like lua, I suggest your read a little more about it and things will become clearer (hopefully).

    Main point is with GC managed objects, you just can't destroy something, it gets destroyed when system discovers that it is no longer used. By "no longer used", I mean that no part of the code (and Gideros) hold a reference to it. You can force a discovery of those stale objects by issuing a 'collect garbage' procedure, but first make sure that no reference still exists to the object your want to destroy.

    You have to watch out for 'hidden' references, for example Sprites used by a MovieClip or event listeners: if you attach a listener to some other object, then that object will hold a reference to the listener (in order to call it later).

    Someone in the forum posted 'leaky.lua', which may be of some help.
  • Thanks, ive tried using collectgarbage("collect").. still doh, seems like it doesnt cares about I wanted to collect, but does this on his own thoughts... im starting to hate LUA just for this thisngs...

    Say me why Garbage Collector doesnt fires up when i do remove world from level and make it as NIL. Seems should work, yeah ? But damn NO, it doesnt mean for engine nothing ... still wondering what a hell inside of lua ..
    vk.com/yan_alex
  • It is actually much better than you think, you just need to be careful not to hold any global or as @hgy29 pointed hidden references:
    http://giderosmobile.com/forum/discussion/comment/9441#Comment_9441

    Since Gidero does support OOP approach, I don't see any problems managing your own references in your own class, like Enemy class. Create a destroy function removing anything you want to remove and call it on scene end.

    But as I said, you are probably hitting some specific case, cause for me most of it works out of the box, and I don't need to remove anything specifically, if I maintain the correct hierarchy of items
  • And in most cases it is completely enough to only remove sprite object and all events will be removed when the object gets garbage collected.

    There is only one specific case found by the @bowerandy that when you add event listener to global object, but pass data as some local object, the local object will not be garbage collected until the global object is not collected.

    Here is more information on this topic: http://www.giderosmobile.com/forum/discussion/2818/a-warning-about-memory-management-complacency#Item_1
  • ar2rsawseen, thanks a lot. Ive really read all similar questions on this forum and whole internet. Dont have any ideas, my ask to you is abnormal, but can you add me to skype, I just want to show you this in real time, because i have no ideas ... i spend the YEAR on this game, and now i stuggled with just this thing, i cant solve this about week, no ideas thousand tries, and nothing ... if u can help, add me please, knifetoface - skype
    vk.com/yan_alex
  • Maybe you need some global classes. If you have many scenes that play sounds then having one global class that manages sound output would seem to be the way to go. That would at least resolve your "hearing sounds from other scenes" issue.

    You could possibly have other global classes for managers that you have duplicated throughout other scenes too.
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Yan so let's make it step by step.

    There should not be any problem with sprites and events (apart from mentioned single situation). If you have some problem regarding events, please try to find small simple example and provide it here.

    if you have some longer sounds, then yes, they will still play even if the scene have changed, because sounds are not tied, nor bound to the scene.
    So in that case as @antix have mentioned, global Sound object would be best solution, as you would be able to stop all the sounds.

    You can check out my Game template as an example:
    https://github.com/ar2rsawseen/GameTemplate

    And same goes for timers. They are not bound to scene, they don't know that scene ended.

    So there are some events for the scenes, so you would know when scene started and ended, and you could execute some code, like stop all timers:
    http://docs.giderosmobile.com/reference/gideros/Timer/stopAll#Timer.stopAll

    The idea is to have each object self contained. Have a class like Enemy, and it manages all its Timers internally (or scene manages timers for group of enemies, etc) so you would know all times and stop them, etc.

    I understand how frustrated you must feel, spending so much time on the project and learning about such quirks only know, but unfortunately I don't think there is more to do, then reorganize your code in a way it would work properly.

    We can start with some specific situations which are not working for you, you can post them on the forum and we all would gladly discuss them and help you out :)

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • I agree that it is really frustrating getting so far with some project and then discover you have to totally rethink or even worse, give up.

    Don't give up though. Redesign it and make it better in the next iteration!
  • YanYan Member
    Thanks to ALL, for help so i have discovered the problem just now.

    Reason - Enemy has timer for starting patroling area, when he didnt see player.
    Solved - Timer.stopAll() on scene exitBegin, unfortunately Timer events didnt removed after sprite been removed !!!

    Likes: hgy29

    vk.com/yan_alex
    +1 -1 (+1 / -0 )Share on Facebook
  • i guess a Sprite:getEventListeners() would be useful to get which functions listen to which events for this sprite. this way it would be easy to know about pending eventlisteners.
    yet i guess it's not hard to do this for yourself just by redefining the addeventlistener method and adding there a line that puts these informations in some table of that sprite.
  • antixantix Member
    I think some sprite manager would have suited the situation better. I mean what kind of performance hit is there when you have 200 sprites each with their own listeners, as opposed to a manager with one listener which processes a table of all 200 sprites?
  • YanYan Member
    One more issue !!!

    If you have some soundStream with COMPLETE event listener IT WOULD NOT BEEN collecet also !!!

    I resolve this by this way - add EVENT listener, if not SoundChannel:isPlaying() do action

    Likes: antix

    vk.com/yan_alex
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.