Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Code optimization/Gideros player crash — Gideros Forum

Code optimization/Gideros player crash

Hello,

So, I am currently working on explosions animations for my project. Before, I was using "MovieClip", but I decided to try "Particles", and I got way better results.




I made a little program to test performances and aesthetics of my 2 first animations. And I noticed two problems:
- the gideros player has suddenly a strong tendency to crash. The program works but pressing "play" or "stop" leads often to crashes that I don't experience with my 3000 lines project.
- I work on a win10 16 gb PC with a gtx 1080.
With 50 millisec interval between explosions, I experience a falloff in performances around the 3rd minute. Around the 4th minute, the fps drop under 30. I kinda expected this because the delay is very short, allowing 20 running explosions on screen per sec. However, I didn't expect a delay of 250 ms to be an issue (only 4 /sec). Here, the falloff started around the 14th minute, reaching 30 fps at 25 mins. I also noticed also that the Gideros player was sometimes using 38% of the GPU.
- I tried to reduce the number of particles. I got better performances at first. But reducing more was not providing significant improvements.

I have seen Gideros working with thousands of sprites, so I guess I am doing something wrong. Is there something to nil to make sure the garbage collector will complete its work? Are the for/next loops a bad way to implement particles?
Please teach me.

Here is the code:
w=application:getContentWidth()
h=application:getContentHeight()
application:setBackgroundColor(0)
 
local random=math.random
 
 
local tex1 = Texture.new("mExplosion2.png")
local tex2 = Texture.new("mExplosion3.png")
local tex3 = Texture.new("mExplosion4.png")
local tex4 = Texture.new("mExplosion5.png")
local tex5 = Texture.new("mExplosion6.png")
local tex6 = Texture.new("mExplosion7.png")
 
texs = {tex1, tex2, tex3, tex4, tex5, tex6} -- for later randomization
 
local life = 10
 
local fps =0
 
local fpsText = TextField.new(nil, math.ceil(fps) )
fpsText:setPosition(50,50)
fpsText:setScale(3)
fpsText:setTextColor(0xffffff)
stage:addChild(fpsText)
 
local interval = 250 --change it!
 
local timer = Timer.new(interval)
 
local timeMsec = 0
local timeSec = 0
local timeText = TextField.new(nil, timeSec)
timeText:setPosition(150,50)
timeText:setScale(3)
timeText:setTextColor(0xffffff)
stage:addChild(timeText)
 
function gomad()
 
	timeMsec += interval
	timeSec = math.ceil(timeMsec/1000)
	timeText:setText(timeSec)
 
	if random(0,1) == 0 then
		explode1(random(w), random(h))
	else
		explode2(random(w), random(h))
	end
 
	--explode1(random(w), random(h))
	--explode2(random(w), random(h))
 
	collectgarbage("collect")
 
	--print(fps, timeSec)  
	fpsText:setText(math.ceil(fps))
 
	if fps < 30 then life -= 1 end
	if life <= 0 then timer:stop() end 
 
end
 
function explode1(x,y)		
 
	local boom=Particles.new()
	boom:setPosition(x,y)
 
	boom:setAlpha(0.1)
	stage:addChild(boom)
 
	local boom2=Particles.new()
	boom2:setPosition(x,y)
 
	boom2:setAlpha(0.1)
	stage:addChild(boom2)
 
	boom:setTexture(texs[2])
	for i = 1,10 do
		boom:addParticles({{x=0,y=0,size=50,alpha=1,ttl=random(25,50),speedAngular=5,speedX=random()-0.5,speedY=random()-0.5,decay=1.04,decayAlpha=0.97}})
	end	
	boom2:setTexture(texs[3])
	for i = 1,10 do
		boom2:addParticles({{x=0,y=0,size=random(25,75),alpha=1,ttl=random(25,100),speedAngular=2,speedX=random()-0.5,speedY=random()-0.5,decay=1.01,decayAlpha=0.97}})
	end	
	local boom4=Particles.new()
	boom4:setPosition(x,y)
	boom4:setAlpha(0.6)
	stage:addChild(boom4)
	boom4:addParticles({{x=0,y=0,size=1,alpha=1,speedGrowth=10,ttl=25,decay=1.05,decayAlpha=0.84}})
 
 
 
end
 
function explode2(x,y)		
	local boom=Particles.new()
	boom:setPosition(x,y)
 
	boom:setAlpha(0.3)
	stage:addChild(boom)
 
	local boom2=Particles.new()
	boom2:setPosition(x,y)
 
	boom2:setAlpha(0.1)
	stage:addChild(boom2)
 
	local boom3=Particles.new()
	boom3:setPosition(x,y)
 
	boom3:setAlpha(0.2)
	stage:addChild(boom3)
 
	local boom4=Particles.new()
	boom4:setPosition(x,y)
	boom4:setAlpha(0.5)
	stage:addChild(boom4)
	boom4:addParticles({{x=0,y=0,size=1,alpha=1,speedGrowth=10,ttl=50,decay=1.05,decayAlpha=0.91}})
 
	boom:setTexture(texs[4])
	for i = 1,15 do --for i = 1,15 do
		boom:addParticles({{x=0,y=0,size=50,alpha=1,ttl=random(25,50),speedAngular=0,speedX=random()-0.5,speedY=random()-0.5,decay=1.04,decayAlpha=0.97 }})
	end	
	boom2:setTexture(texs[3])
	for i = 1,7 do --for i = 1,7 do
		boom2:addParticles({{x=0,y=0,size=150,alpha=1,ttl=random(25,100),speedAngular=0, speedGrowth=1,speedX=random()-0.5,speedY=random()-0.5,decay=1.01,decayAlpha=0.97}})
	end	
	boom3:setTexture(texs[1])
	for i = 1,20 do --for i = 1,20 do
		boom3:addParticles({{x=0,y=0,size=random(20,50),alpha=1,ttl=random(25,50),speedAngular=5,speedX=random()-0.5,speedY=random()-0.5,decay=1.05,decayAlpha=0.98}})
	end	
 
end
 
 
 
function onEnterFrame(event)
    fps = 1/event.deltaTime
	--print(fps, timeSec)  	
end
 
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame, self)
timer:addEventListener(Event.TIMER, gomad, timer)
timer:start()
explosions.gif
300 x 250 - 1M

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    Hi @Dafb,
    In your code you keep adding Particles sprites to stage, but you never remove them! It seems that gideros starts to lag when you reach more than 10k sprites.
    You should reuse your particle sprites: create them once and for all for each texture, then just add new particles to them and let gideros remove the particles based on the ttl.
  • rrraptorrrraptor Member
    edited August 2020 Accepted Answer
    hgy29 said:

    create them once and for all for each texture, then just add new particles to them and let gideros remove the particles based on the ttl.



    Graph shows min, current and max fps, interval is 1ms :)


    Project attached
    zip
    zip
    ParticlesTest2.zip
    4K
    zz.png
    735 x 565 - 128K

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • DafbDafb Member
    Amazing!

    I really appreciate how you corrected my tentative "work". I learned several stuff by reading the new code.

    The performances are just amazing and entirely stable. Now nothing will prevent me to blow things up! :D
    hgy29 said:

    Hi @Dafb,
    In your code you keep adding Particles sprites to stage, but you never remove them! It seems that gideros starts to lag when you reach more than 10k sprites.
    You should reuse your particle sprites: create them once and for all for each texture, then just add new particles to them and let gideros remove the particles based on the ttl.

    This is right. The worse thing is I wanted to design the code like this at first, and then, based on a stupid feeling, I changed my mind. Getting older does not help!

    Thank you for your help guys. That was really appreciated.
    +1 -1 (+3 / -0 )Share on Facebook
Sign In or Register to comment.