Adding Entities From A Script


(Zouhair Elamrani Abou Elassad) #1

I’m creating a Game where to player has to move and dodge obstacles, I have to add to my world more than 500 entities, so i decided to use a script that contains the coordinates of every object, after all the entities are added to the stage i found that the memory use is about 300M, when my player fails i restart the world (FP.world = new …), the script is being read again, the problem is that at a certain point the Memory is almost 550M, and sometimes is 700M. I used FP.world.removeAll(), it’s working great, but there is a moment where it’s clear that nothing is on stage (blank stage) then all the entities are added. I’m wondering how to better use the memory while reading from the script ?


(rostok) #2

I think that it may relate to entities’ graphics (either Image or Spritemap) that is being duplicated. Are you using single instance of Image for mutliple entities?

There is also a recycle angle here. Since those entities are being initialized every time you could save the initial parameters (position, state, etc) and instead of deleting them from current world you could move them to the next world and reinit them. This way you would get new level instantly and have constant memory usage.


(Zouhair Elamrani Abou Elassad) #3

I’m using a class for every object on my stage, every class has its own Image or Spritemap, i don’t understand why when i do FP.world = new … there is a lost of memory because i replace my whole current world, but when i do FP.world.removeAll() before replcaing the world everything is Ok


(rostok) #4

Adding new world doesn’t mean that the old one is deleted right away with all its entities. If you have a reference to it somewhere it will be still kept alive and ommited by the garbage collector. Also note that garbage collector clears memory on certain conditions and intervals.


(Zouhair Elamrani Abou Elassad) #5

I see, i think that’s it, there some entities where i use reference of the world, is there any way to delete the old world once i call FP. world even if there is a reference of it somewhere ?? Thank you


(rostok) #6

Do as you said earlier. Just call removeAll() from current world and make sure there is no other reference to it before changing to the new one. You could also force garbage collector cleanup System.gc() but if I remember correctly it is not advisable.


(Jean) #7

http://www.draknek.org/misc/fp-docs/net/flashpunk/World.html#end()

Probably you will want to call removeAll here?


(rostok) #8

@DarkHyudrA this should be approached carefully. If one keeps menu as separate world then removaAll in end would complicate things.


(Zouhair Elamrani Abou Elassad) #9

Actually removeAll in the end function did the trick, i used to call it just before changing worlds, but i’ve noticed that there is a moment where everything disappears from the stage, and the stage becomes BLANK before showing the new world, but using it inside the end function solved, Thank You.