Revisit Of Old Project-Frames Per Second Display Not Working?(SOLVED)

Hi,

I am working on an old project.
I noticed our Frames Per Second counter is not working.

We have a “Module” below which takes care of FPS counter display:
FPS counter stays locked at whatever “set_update_frequency” is set to.
(even when I know the game is not running full speed like on a Raspberry Pi)

Code is below. hope someone knows how to fix it:
Thanks!

Jesse

local UFPSAMXY = {}

function UFPSAMXY.UpdateFPSandMouseXY(dt)
	local frames = {}
	local frame_sum = 0
	local frame_index = 1
	local MAX_SAMPLES = 10
	UFPSAMXY.average_ms = 0
	UFPSAMXY.raw_average_time = 0
	UFPSAMXY.fps = 0
	
	if frames[frame_index] then frame_sum = frame_sum - frames[frame_index] end
	frame_sum = frame_sum + dt
	frames[frame_index] = dt
	frame_index = math.fmod(frame_index + 1, MAX_SAMPLES)
	UFPSAMXY.raw_average_time = frame_sum / MAX_SAMPLES
	UFPSAMXY.average_ms = math.floor(UFPSAMXY.raw_average_time * 10000) / 100
	UFPSAMXY.fps = math.floor(1.0 / UFPSAMXY.raw_average_time * 100) / 1000 + 1
		
	if (SecretCode[0] == 2 and SecretCode[1] == 7 and SecretCode[2] == 7 and SecretCode[3] == 7) then
		label.set_text(   "#MouseXY", "FPS=" .. tostring( math.floor(UFPSAMXY.fps) ) .. " [" .. tostring(  math.floor( mX * (360/WindowWidthTrue) )  ) .. "," .. tostring(  math.floor( mY * (640/WindowHeightTrue) )  ) .. "]"   )	
	end
end

return UFPSAMXY

If it’s an old project it could be a vsync issue. Have a look in game.project under ‘Display’ and change the setting for ‘Vsync’.

Vsync is already off

Sadly actual FPS calculations based on dt do not work due to the way the engine is currently setup. I thought it was working at some point, but somewhere there was a change that broke it. If a user’s vsync is messed up it will also still display 60FPS even if animations are going a few hundred FPS. And in your case vsync is also probably still messed up but since hardware is slow you see slower animations but the engine wants to say it’s going at 60FPS because of the consistent dt value.

You will have to get the actual system time to do time diffs on.

Does current Defold have a command to get current system time?
Thanks!

Saw above time functions, but they are in seconds?
(need milliseconds)

You can use socket.gettime() which returns a Unix timestamp with milliseconds. Then you subtract the previous from the current timestamp to get the actual ms time between frames.

2 Likes
local UFPSAMXY = {}

function UFPSAMXY.UpdateFPSandMouseXY(dt)
	FPS_FrameCount = FPS_FrameCount + 1
	local currentTime = socket.gettime()
	if ( currentTime > (FPS_LastSecond+1) ) then
		FPS_LastSecond = socket.gettime()
		FPS_TenSeconds[FPS_CurrentSecond] = FPS_FrameCount
		FPS_FrameCount = 0
		if (FPS_CurrentSecond < 9) then
			FPS_CurrentSecond = FPS_CurrentSecond + 1
		else
			FPS_CurrentSecond = 0
		end

		FPS_Average = 0
		local index = 0
		for index = 0, 9 do
			FPS_Average = FPS_Average + FPS_TenSeconds[index]
		end
		FPS_Average = (FPS_Average / 10)
	end

	if (SecretCode[0] == 2 and SecretCode[1] == 7 and SecretCode[2] == 7 and SecretCode[3] == 7) then
		label.set_text(   "#MouseXY", "FPS=" .. tostring( math.floor(FPS_Average) ) .. " [" .. tostring(  math.floor( mX * (360/WindowWidthTrue) )  ) .. "," .. tostring(  math.floor( mY * (640/WindowHeightTrue) )  ) .. "]"   )	
	end
end

return UFPSAMXY