what is the best way to display object ?

Hi both,

If i good understand, it’s a bad pratice to create/destroy physics objects but it’s encouraged when you don’t use physics object ?

In this post (number 8) they encourage to destroy and create objects :

https://forums.coronalabs.com/topic/15165-tips-optimization-101/

Thanks for yours advices

espace3d,

It is really up to you how you want to handle the management of your objects, regardless of whether they use physics or not.  If you Google “Object pooling”, you will see countless of articles on it.  If you look on the Unity Asset Store, for example, you will see about a dozen Object Pooling libraries as well.

Creation/destruction of objects enabling/disabling physics all have overhead.  Creating the same object over and over, especially when in a fast moving or object heavy game, will eventually lag the system since it has to allocate/deallocate memory, etc, while your game logic is doing what it should do.

As carloscosta mentions, the downside of creating your objects early is that it does use more memory.  For me, I would rather allocate a bit more memory, and keep it that way, than to subject the user to the lag caused by memory allocation and garbage collection while playing a game.

#8 in the post you link to is for the eventual destruction of object.  When the game is over, or you leave the scene, you should destroy all created objects.  

For Object Pooling, I would use isVisible = false instead of alpha = 0.

Best I can say is try what you feel is best.

Thanks a lot schizoid2k for these explanation. 

I have also 2 questions :

  1. In your blog what’is the editor used to display your code ? It look great.

  2. Could you give me a practical example to how to nil out the game objects ? is it the code explained by carlocosta ?

    local function removeCircle(varIn)     local var=varIn     if multipleCircles[var] then         multipleCircles[var]:removeSelf()         multipleCircles[var]=nil     end end

When done, don’t forget to clean up! Cycle through the pool to remove all event listeners and physics, and don’t forget to nil out the game objects.

thanks 

there are different ways to remove objects. the easiest way is doing:

local function removeCircle(varIn) local var=varIn display.remove(multipleCircles[var]) -- it checks internally if it exists or not ​ multipleCircles[var]=nil end

the other metod was the one i puted in my code.

if you want to delete all objects at the end you should remove them all in another function:

local function removeAllCircle() for i=#multipleCircles, 1, -1 do if multipleCircles[i] then multipleCircles[i]:removeSelf() ​ multipleCircles[i]=nil end end multipleCircles=nil end

or: 

local function removeAllCircle() for i=#multipleCircles, 1, -1 do display.remove(multipleCircles[i]) ​ multipleCircles[i]=nil end multipleCircles=nil end

just remember to use backwards cicle when you are deleting tables.

if your objects have more things attach to them i usually remove them too, before the last step (removeSelf()). for example if they have touch events, i remove first the touch event for that object then i remove the object.

If i good understand, it’s a bad pratice to create/destroy physics objects but it’s encouraged when you don’t use physics object ?

depends when you want to destroy them and the number of objects to delete or create. my first way of thinking is always to clean what is not showned anymore. but if that process creates overhead, just hidden them is a better aproach in that case. complex objects that are “hard” to create, if your constant needing them, hidden them is one way to go.

just remember that hidden objects don’t remove them from memory.

remember too, that creating and removing objects uses more cpu power than just hidden them.

when you create and remove objects creates lag on your app, maybe the problem is not removing or creating the objects…are in the rest of the code, but that really depends on when and how many are you deleting and creating.

espace3d,

  1. I use Wordpress for my blog and for the code inserts I use Crayon plugin.

  2. Yes, carlocosta explained it, but here’s some pseudocode:

    for i = #objectsInPool, 1, -1 do physics.removeBody(objectsInPool[i]) – if you have physics on the object display.remove(objectsInPool[i]) objectsInPool[i] = nil end objectsInPool = nil

Why do you do twice the boucle for ? 

1, -1 

Just the “-1” is part of the iterations parameters.

http://lua-users.org/wiki/ForTutorial

http://www.lua.org/pil/4.3.4.html

Do you mean that  -1 is for an infinite boucle?

for cicle is divided in 3 arguments.

first is the inicial number, second is the finish number, third is the scale number. the scale number is the number that will be added to the first number till he reachs the finish number. if you start with a large number to a little number (ex. 10, 1) you need to subtract not to add, so your 3 argument will be negative (-1). it will be (10, 9, 8, 7, 6,…till 1)

the 3 argument can be different from -1 if you put -2, the result will be (10, 8, 6, 4, 2)