what is the best way to display object ?

hi, 

i don’t know finally what 's the best way to work with multiples object ?

is it better to do like that :

local function createCircle(i) circle={} for i=1, number.circle do circle[i]=display.... circle[i].alpha=1 --- end retunr circle[i] end local function removeCircle(circle,i) display.remove(circle[i]) end 

or like that to prevent the memory due to charge the objects ?

local circle={} for i=1, number.circle do circle[i]=display.... circle[i].alpha=0 --- end end local function showCircle(circle,i) circle[i].alpha=1 end local function hideCircle(circle,i) circle[i].alpha=0 end

After a certain amount of play time on my application I felt a certain lag with the physics.

I think this is due to the number of objects displayed .

I mainly use the tables.
Thank you to tell me what is the best way to avoid this lag.

Hi @espace3d,

What is “number.circle”? I see no context on what that value would (or should) be…

 hi Brent, 

local number={} number.circle=50 --for example

 
is it better to create and destroy the objects each time or just work with the alpha channel ?

I would start with something like this:

local multipleCircles={} local numberCircles=50 local rand=math.random local function createCircles(posXIn,posYIn,radiusIn) local posX=posXIn local posY=posYIn local radius=radiusIn local circle=display.newCircle(posX,posY,radius) circle:setFillColor(rand(),rand(),rand()) return circle end local function removeCircle(varIn) local var=varIn if multipleCircles[var] then multipleCircles[var]:removeSelf() multipleCircles[var]=nil end end for i=1, numberCircles do local randomRadius=rand(10,30) local randomX=rand(1+(randomRadius),display.contentWidth-randomRadius) local randomY=rand(1+(randomRadius),display.contentHeight-randomRadius) multipleCircles[i]=createCircles(randomX,randomY,randomRadius) end local counter=1 timer.performWithDelay(100,function() removeCircle(counter) counter=counter+1 end, numberCircles)

is better do “destroy” the objects than using alpha channels if your not going to use them anymore. even if your going to use them, removing object and creating again is always a good way to go…using alpha channel is the “lasy” root…

I tend to disagree.  I “rarely” (ie: probably never) create and destroy objects while the game is playing.

It might be ok for some games, but the overhead of constantly creating, destroying, and adding/removing physics will definitely impact your game when a lot of objects are on the screen (think bullet hells).  In a bullet hell for example, why create/destroy dozens of bullets over and over again?

I wrote this article (on my blog site) which explains how to utilize object pooling in order to reuse objects you created at the start.

Hope this helps,

–john

in the example i gave it’s easy to prove my point.

if you do a memory check in the program i made you will see a decrease of memory usage while the objects are beeing deleted.

if you change the part of the code where it removes to

 multipleCircles[var].isVisble=false

you will see that the memory mantains. making the object not visible don’t means is not there. when you don’t need the object anymore, hidden an object is the worst route to go. if your going to use it again, it depends if you need it right away or in another scene or at the start of a level, etc. that needs to be studie case by case. i don’t work with physics objects so i never encountered those kinda problems you refered, 99% of my projects are business projects.

@escace3d example didnt use physics, in that case the best approach would be the one i suggested, like in my example to study.

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)

Hi @espace3d,

What is “number.circle”? I see no context on what that value would (or should) be…

 hi Brent, 

local number={} number.circle=50 --for example

 
is it better to create and destroy the objects each time or just work with the alpha channel ?

I would start with something like this:

local multipleCircles={} local numberCircles=50 local rand=math.random local function createCircles(posXIn,posYIn,radiusIn) local posX=posXIn local posY=posYIn local radius=radiusIn local circle=display.newCircle(posX,posY,radius) circle:setFillColor(rand(),rand(),rand()) return circle end local function removeCircle(varIn) local var=varIn if multipleCircles[var] then multipleCircles[var]:removeSelf() multipleCircles[var]=nil end end for i=1, numberCircles do local randomRadius=rand(10,30) local randomX=rand(1+(randomRadius),display.contentWidth-randomRadius) local randomY=rand(1+(randomRadius),display.contentHeight-randomRadius) multipleCircles[i]=createCircles(randomX,randomY,randomRadius) end local counter=1 timer.performWithDelay(100,function() removeCircle(counter) counter=counter+1 end, numberCircles)

is better do “destroy” the objects than using alpha channels if your not going to use them anymore. even if your going to use them, removing object and creating again is always a good way to go…using alpha channel is the “lasy” root…

I tend to disagree.  I “rarely” (ie: probably never) create and destroy objects while the game is playing.

It might be ok for some games, but the overhead of constantly creating, destroying, and adding/removing physics will definitely impact your game when a lot of objects are on the screen (think bullet hells).  In a bullet hell for example, why create/destroy dozens of bullets over and over again?

I wrote this article (on my blog site) which explains how to utilize object pooling in order to reuse objects you created at the start.

Hope this helps,

–john

in the example i gave it’s easy to prove my point.

if you do a memory check in the program i made you will see a decrease of memory usage while the objects are beeing deleted.

if you change the part of the code where it removes to

 multipleCircles[var].isVisble=false

you will see that the memory mantains. making the object not visible don’t means is not there. when you don’t need the object anymore, hidden an object is the worst route to go. if your going to use it again, it depends if you need it right away or in another scene or at the start of a level, etc. that needs to be studie case by case. i don’t work with physics objects so i never encountered those kinda problems you refered, 99% of my projects are business projects.

@escace3d example didnt use physics, in that case the best approach would be the one i suggested, like in my example to study.