[SOLVED] How I can better performance and FPS of my game?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

[SOLVED] How I can better performance and FPS of my game?

Post by AdrianN » Wed Oct 24, 2018 5:46 am

Hi all, I have a problem about the performace and FPS in my game

Source code of my game https://github.com/AdrianN17/Jellystastic


The source of the problem is the detection of collisions in the love.update(dt) function, I use a lot of iterator for all of my actors
Also I edit the love.run function with this code: (sorry I forget the source thread)

Code: Select all

function love.run()
if love.math then
	love.math.setRandomSeed(os.time())
end

if love.load then love.load(arg) end

-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end

local dt = 0

-- Main loop time.
while true do
	-- Process events.
	local startT = love.timer.getTime()
	if love.event then
		love.event.pump()
		for name, a,b,c,d,e,f in love.event.poll() do
			if name == "quit" then
				if not love.quit or not love.quit() then
					return a
				end
			end
			love.handlers[name](a,b,c,d,e,f)
		end
	end

	-- Update dt, as we'll be passing it to update
	if love.timer then
		love.timer.step()
		dt = love.timer.getDelta()
	end

	-- Call update and draw
	if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled

	if love.graphics and love.graphics.isActive() then
		love.graphics.clear(love.graphics.getBackgroundColor())
		love.graphics.origin()
		if love.draw then love.draw() end
		love.graphics.present()
	end

	if love.timer then
		local endT = love.timer.getTime()
		love.timer.sleep( 1/60 - (endT - startT))
	end
end
 
 
Example:

Code: Select all

for _,col in pairs(base.entidades.colli) do
	if col.l==base.entidades.l and col.ox>x and col.ox<x+h and col.oy>y and col.oy<y+w then
		local dx,dy,colli=0,0,false
		colli,dx,dy=base.entidades.player.body:collidesWith(col.body)--player pared
		--base.entidades.player.ox
		if col.type=="col" and colli then
			if dy<0 and dx==0 then
				base.entidades.player.ground=true
				base.entidades.player.body:move(0,dy/2)
				base.entidades.player.melee:move(0,dy/2)
				base.entidades.player.point:move(0,dy/2)
				base.entidades.player.vy=0
			elseif dy>0 and dx==0 then
					base.entidades.player.vy=base.entidades.player.vy/2
					base.entidades.player.body:move(0,dy*1.2)
					base.entidades.player.melee:move(0,dy*1.2)
					base.entidades.player.point:move(0,dy*1.2)
			elseif dx~=0 then 
				if dx<0 then
					base.entidades.player.moveleft=false
					base.entidades.player.body:move(math.min(dx*1.2,5),0)
					base.entidades.player.melee:move(math.min(dx*1.2,5),0)
					base.entidades.player.point:move(math.min(dx*1.2,5),0)
				elseif dx>0 then
					base.entidades.player.moveright=false
					base.entidades.player.body:move(math.max(dx*1.2,-5),0)
					base.entidades.player.melee:move(math.max(dx*1.2,-5),0)
					base.entidades.player.point:move(math.max(dx*1.2,-5),0)
				end
			end
		elseif not base.entidades.player.ignore2 and col.type=="plat" and colli then
			if dy<0 then
				base.entidades.player.ground=true
				base.entidades.player.isplatformer=true
				base.entidades.player.vy=0
				base.entidades.player.body:move(dx,dy/10)
				base.entidades.player.melee:move(dx,dy/10)
				base.entidades.player.point:move(dx,dy/10)
			end
		end
	end
			
	for _, bu in pairs(base.entidades.bullet) do
		if col.l==bu.l then
			local dx,dy,colli=0,0,false
			colli,dx,dy=bu.body:collidesWith(col.body)--collision bala pared
			if colli and col.type =="col" then
				if bu.damage==4 then
					base.entidades:removeextra(col,"colli")
					HC.remove(col.body)

					base.entidades:addextra(Explosion(bu.ox,bu.oy,bu.l),"explosion")
				end
				bu.hp=bu.hp-1
			end
		end
	end
		...
I tried to minimize only of the camera screen (visible), but it's not enough
Example :

Code: Select all

--x,y,h,w = camera position and dimensions
if col.ox>x and col.ox<x+h and col.oy>y and col.oy<y+w then 
	-- some stuff
I'm thinking in used thread in this case, or courutines, but I don't know is that is better or no
Additional, I used two maps in memory in my game (stage 1 - stage 2), because I save the register of the destruction tiled.

Code: Select all

base.init(self,"assets/level/nivel_1_1_1.lua","assets/level/nivel_1_1_2.lua")
Last edited by AdrianN on Fri Oct 26, 2018 2:43 am, edited 1 time in total.

User avatar
ivan
Party member
Posts: 1614
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How I can better performance and FPS of my game?

Post by ivan » Wed Oct 24, 2018 7:03 am

The only reasonable way to improve performance is through profiling:
viewtopic.php?t=80759

Having said that, you need to learn how locals are used, it will make your code shorter too. For example:

Code: Select all

local player = base.entidades.player
For collisions, Box2D has very good partitioning which is probably far superior to HC.
But again, you need profiling to figure out what is causing the drop in performance.

User avatar
pgimeno
Party member
Posts: 2311
Joined: Sun Oct 18, 2015 2:58 pm

Re: How I can better performance and FPS of my game?

Post by pgimeno » Wed Oct 24, 2018 11:05 am

pairs is slow, and blocks JIT optimization.

If your tables are sequences (i.e. tables with numeric indices starting at 1 and without holes), ipairs works as a drop-in replacement that is much faster, but looping over numbers is even faster than ipairs.

If they are not sequences, consider converting them to be.

MissDanish
Citizen
Posts: 65
Joined: Wed Mar 07, 2018 11:21 pm

Re: How I can better performance and FPS of my game?

Post by MissDanish » Wed Oct 24, 2018 2:56 pm

There's a lot of things you can do to optimize games in general:
-As the others mentioned use ipairs, or preferably numerical loops
-Use local variables as much as possible and you can easily make functions local if they are only used within the same file
-Avoid redundant checks, instead of having two of the same if checks merge them
-Use elseif when possible, it can help improve performance as it will skip checks undernearth it if the one above is true
-Don't draw things that aren't visible on screen
-If possible only calculate things only once (ie the image offset of an object)
-Remove unused transparent space in images
-Removing any old unused legacy code (can become an issue in big projects)

There's more but those are some of the most effective things you can do to optimize your game.

User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: How I can better performance and FPS of my game?

Post by AdrianN » Wed Oct 24, 2018 4:35 pm

Thanks for their replies guys.

Previously I used local for shorter code,but I thought otherwise about it

I sended all collision logic for entidades file

Code: Select all

--gamestate nivel_1_1.lua example
	if base.entidades.player then 
		--some stuff
	end
	
	-- entidades.lua
	if self.player then 
		--some stuff
	end
About ipairs, I didn't think there would be a difference.
I preffer used HC instead of Box2d in that proyect (I 'm fine with only collision detection ), but I thinking about in that for the next game.

I have managed to optimize my game a little more with the sugerences
old game fps aproximate: 50 - 30
new game fps aproximate: 60 - 40

grump
Party member
Posts: 638
Joined: Sat Jul 22, 2017 7:43 pm

Re: How I can better performance and FPS of my game?

Post by grump » Thu Oct 25, 2018 7:39 am

I'd like to add that you should not waste time doing premature micro-optimizations (pairs, local variables, etc.) until you have found the real bottlenecks in your code. Most tips in this threads are micro-optimizations, and they won't save a badly performing game.

The most important advices here are a) use a profiler, so you actually know what's eating performance instead of just guessing (because you will guess wrongly), and b) don't draw things that aren't on screen, which is a logical extension of "don't process things that don't need processing". You can save the micro-optimizations for later.

User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: How I can better performance and FPS of my game?

Post by AdrianN » Thu Oct 25, 2018 3:26 pm

grump wrote:
Thu Oct 25, 2018 7:39 am
I'd like to add that you should not waste time doing premature micro-optimizations (pairs, local variables, etc.) until you have found the real bottlenecks in your code. Most tips in this threads are micro-optimizations, and they won't save a badly performing game.

The most important advices here are a) use a profiler, so you actually know what's eating performance instead of just guessing (because you will guess wrongly), and b) don't draw things that aren't on screen, which is a logical extension of "don't process things that don't need processing". You can save the micro-optimizations for later.
Yes, previously I used the b tip in my code (draw,update,collisions)

Code: Select all

for i, e in ipairs(self.enemies) do
	if e.l==self.l and e.ox>self.x and e.ox<self.h and e.oy>self.y and e.oy<self.w then --and self.y and self.y then
		e:draw()
	end
end

for i, e in ipairs(self.enemies) do
	if e.l==self.l  and e.ox>self.x and e.ox<self.h and e.oy>self.y and e.oy<self.w then
		e:update(dt)
	end
end
I had not used profile because, I tested singly the iterator for reach actor in the game, and if I delete that code the game run to 60fps.
But also I'm starting to use, maybe there is something that lowers the performance without me noticing.

Thanks sir.

User avatar
ivan
Party member
Posts: 1614
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How I can better performance and FPS of my game?

Post by ivan » Thu Oct 25, 2018 3:40 pm

FPS is not a good measure of performance because sometimes you have slow code that executes infrequently so the game stutters occasionally while maintaining 60FPS the rest of the time. So yea, profiling is the way the go so that your game runs smoothly all the time.

User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: How I can better performance and FPS of my game?

Post by AdrianN » Thu Oct 25, 2018 4:21 pm

ivan wrote:
Thu Oct 25, 2018 3:40 pm
FPS is not a good measure of performance because sometimes you have slow code that executes infrequently so the game stutters occasionally while maintaining 60FPS the rest of the time. So yea, profiling is the way the go so that your game runs smoothly all the time.
Sure, but I have some troubles for implement the profile in my game

When I run the game with it, is very lag (0-1 fps, and CG activated).

viewtopic.php?t=80759

Is it a secondary effect of the library?

User avatar
ivan
Party member
Posts: 1614
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How I can better performance and FPS of my game?

Post by ivan » Thu Oct 25, 2018 4:29 pm

Sure, your program will be slower when profiling - you should only use the profiler when debugging.
This is because all function calls are hooked and tracked by the profiler.

Apart from loading, each frame of your game should never take longer than 10-30 milliseconds to execute.
Even if your game is not playable, you can still check the profiler report to see WHY your code is not playable.
Just print the report to the console or save it to a log file.

Post Reply

Who is online

Users browsing this forum: FloatingBanana, pgimeno and 23 guests