Does Defold Calculate Frames Per Second?

Hi,

At a point in development where I need a Frames Per Second display.
I looked through the online docs, but did not find anything.
How would I create a FPS counter?
Thanks!

J.

You could use my defold-metrics project as a dependency to show or get fps: https://github.com/britzl/defold-metrics

Another option is to use the on screen profiler.

2 Likes

https://www.mvps.org/directx/articles/fps_versus_frame_time.htm

1 Like

Hi,

Found this code to calculate Frames Per Second, but it won’t compile?:

	local last_time = os.clock()
	local os_dt_table = {}
	local os_dt_sum = 0
	local os_frames_per_second = 0
	local os_seconds_to_track = 2
	local os_expected_fps = 60

	local dt = os.clock() - last_time

	os_dt_sum = os_dt_sum + dt
	table.insert(os_dt_table, dt)
	local len = length(os_dt_table) -- <-- ERROR: attempt to call global 'length' (a nil value)?

	os_frames_per_second = len / os_dt_sum

	if len >= os_seconds_to_track*os_expected_fps then
		os_dt_sum = os_dt_sum - os_dt_table[1]
		splice(os_dt_table, 1, 1)
	end

	last_time = os.clock()

--	local n = gui.get_node("fps_os")
--	gui.set_text(n, "FPS (OS): " .. round(os_frames_per_second))

Any ideas on how to repair?
Thanks!

J.

From this thread? Frames per second - different when using dt versus system clock

The code does not work because it’s part of a larger whole. “length” is not a native thing. But table.length and # are. So that user must have made their own length function and imported it globally or included it higher above in their script from where that is from.

How would I get the current time in hours, minutes, and seconds with this command?

J.

Read this https://www.lua.org/pil/22.1.html

I should probably have asked why you need the FPS counter before providing an answer.

I have a Frames Per Second display in all of the games we made.
Defold HTML5 performance is ridiculously fast.
What’s under the hood? WebGL?

Thanks!

J.

Ok, like a debug thing while developing the game?

Yes, WebGL and Emscripten.

Ok…

We added the following:

    FPS_LastSecond = socket.gettime()
    FPS_FrameCount = 0
    FPS_TenSeconds = {}
    for index = 0, 10 do
        FPS_TenSeconds[index] = 0
    end
    FPS_CurrentSecond = 0
    FPS_Average = 0

    mX = 0
    mY = 0

and

function update(self, dt)
	-- Add update code here
	-- Remove this function if not needed

	FPS_FrameCount = (FPS_FrameCount + 1)
	local currentTime = socket.gettime()
	if ( currentTime > (FPS_LastSecond + 1) ) then
		FPS_TenSeconds[FPS_CurrentSecond] = FPS_FrameCount
		if (FPS_CurrentSecond < 10) then
			FPS_CurrentSecond = (FPS_CurrentSecond + 1)
		else
			FPS_CurrentSecond = 0
		end

		for index = 0, 10 do
			FPS_Average = (FPS_Average + FPS_TenSeconds[index])
		end
		
		FPS_Average = (FPS_Average / 11)
		FPS_Average = math.floor(FPS_Average)
				
		FPS_LastSecond = socket.gettime()
		FPS_FrameCount = 0
	end

	label.set_text(   "#MouseXY", "FPS=" .. tostring(FPS_Average) .. " [" .. tostring(  math.floor( mX * (360/WindowWidthTrue) )  ) .. "," .. tostring(  math.floor( mY * (640/WindowHeightTrue) )  ) .. "]"   )
end

Getting about 66 Frames Per Second?
Is the above code correct?
Was expecting something closer to “60”?

Let me know, thanks!

J.

I would have expected 60 as well. But there’s a few factors that could impact the result, like how you count frames and variable dt setting.

Open the web or on screen profiler (check documentation) and compare!

I would suggest to make it a lot easier. Show the milliseconds per frame instead!

It’s a lot easier to reason about when optimising various parts in the code. Here is one link that explains this.

E.g. think about how to measure how much time a script’s update function takes. How would you explain that in a clear and concise way to someone?
Instead, it’s better to use the actual time: “This function takes X ms to run.”. You can immediately relate the “weight” of that function when you compare it to other functions.

1 Like

Getting too old for these all-nighters…

I prefer an old-fashioned Frames Per Second display.
The one implemented now keeps FPS for the past 11 seconds and does an average of all 11.

It works good, but just don’t understand why I am getting values of 66-67 FPS?
I’ll hit it with a big hammer when I wake up…

P.S. HTML5 and Android versions are running with incredible speed!

J.

2 Likes