Discuss Scratch

PopsicleSticks
Scratcher
22 posts

How do you make an fps counter?

Title says it all. I've seen many games/tests with fps counters, but I can't figure out how to make one.

Please help!
drmcw
Scratcher
1000+ posts

How do you make an fps counter?

You just need to time your game loop, so in your loop reset the timer then just before you reset again the fps will be 1/timer. If you don't want to reset it then you can store the timer and use 1 / (timer - previous timer)
mathfreak231
Scratcher
1000+ posts

How do you make an fps counter?

In SB:

when gf clicked
reset timer
forever
set [fps v] to ((1) / (timer))
reset timer
end

If you don't want to reset the timer, as drmcw said, use:

when gf clicked
reset timer
set [previous timer v] to [0]
forever
set [fps v] to ((1) / ((timer) - (previous timer)))
set [previous timer v] to (timer)
end
jji7skyline
Scratcher
1000+ posts

How do you make an fps counter?

mathfreak231 wrote:

In SB:

when gf clicked
reset timer
forever
set [fps v] to ((1) / (timer))
reset timer
end

If you don't want to reset the timer, as drmcw said, use:

when gf clicked
reset timer
set [previous timer v] to [0]
forever
set [fps v] to ((1) / ((timer) - (previous timer)))
set [previous timer v] to (timer)
end
Make sure the script is nested inside the main rendering script of your project, otherwise it's not accurate.
PopsicleSticks
Scratcher
22 posts

How do you make an fps counter?

Okay, thanks for helping!
DadOfMrLog
Scratcher
1000+ posts

How do you make an fps counter?

The above will work well if you've got a reasonably stable framerate.

However, if your framerate varies a lot (maybe different objects on-screen change how much work is being done during the loop, for example) then the FPS reporter can flip around its value a bit too much.

If that does turn out to be the case, then it's possible to add a frame counter to the above scripts, and then either calculate FPS after a certain number of frames or after a certain number of seconds have elapsed.

So something like this in your main controlling sprite:

define Initialise FPS
set [previoustime v] to (timer)
set [framecount v] to [0]
set [FPS v] to [0]

define Calculate FPS
change [framecount v] by (1)
if ((timer) > ((previoustime)+(3))) then // report after every three seconds
set [FPS v] to ((framecount)/((timer)-(previoustime)))
set [previoustime v] to (timer)
set [framecount v] to [0]
end

when GF clicked
broadcast [initialise all v] and wait // all sprites should respond by preparing themselves at the start...
Initialise FPS
forever
broadcast [do frame work v] and wait // all sprites & clones will be synchronised to do their stuff for each frame
Calculate FPS
end

(You could even put all the FPS stuff into a completely separate sprite, and just have it respond to the “initialise all” and “do frame work” broadcasts from the main loop script, so avoiding the custom blocks in that script.)

Last edited by DadOfMrLog (Oct. 16, 2013 18:05:57)

jji7skyline
Scratcher
1000+ posts

How do you make an fps counter?

So your script basically calculates a more averaged fps value?
DadOfMrLog
Scratcher
1000+ posts

How do you make an fps counter?

jji7skyline wrote:

So your script basically calculates a more averaged fps value?
Yes, that's really all it's for -helps a bit in the cases where the counter might otherwise appear somewhat erratic.

rifj190
Scratcher
25 posts

How do you make an fps counter?

DadOfMrLog wrote:

The above will work well if you've got a reasonably stable framerate.

However, if your framerate varies a lot (maybe different objects on-screen change how much work is being done during the loop, for example) then the FPS reporter can flip around its value a bit too much.

If that does turn out to be the case, then it's possible to add a frame counter to the above scripts, and then either calculate FPS after a certain number of frames or after a certain number of seconds have elapsed.

So something like this in your main controlling sprite:

define Initialise FPS
set [previoustime v] to (timer)
set [framecount v] to [0]
set [FPS v] to [0]

define Calculate FPS
change [framecount v] by (1)
if ((timer) > ((previoustime)+(3))) then // report after every three seconds
set [FPS v] to ((framecount)/((timer)-(previoustime)))
set [previoustime v] to (timer)
set [framecount v] to [0]
end

when GF clicked
broadcast [initialise all v] and wait // all sprites should respond by preparing themselves at the start...
Initialise FPS
forever
broadcast [do frame work v] and wait // all sprites & clones will be synchronised to do their stuff for each frame
Calculate FPS
end

(You could even put all the FPS stuff into a completely separate sprite, and just have it respond to the “initialise all” and “do frame work” broadcasts from the main loop script, so avoiding the custom blocks in that script.)
@DadOfMrLog, I have used this technique of FPS calculation in a few of my projects but at some of the leggiest moments, my FPS shoots up to 45, then back down to ~5. Do you know why this is happening? Is there a way to fix it?

Thanks.
resh123
Scratcher
9 posts

How do you make an fps counter?

This is my method:
when green flag clicked
forever
set [frames v] to [0]
reset timer
repeat until <(timer) > [0.99]>
change [frames v] by (1)
end
set [fps v] to (frames)
end
It records the fps each time it checks and averages it after.
RedDragonNinja
Scratcher
18 posts

How do you make an fps counter?

mathfreak231 wrote:

In SB:

when gf clicked
reset timer
forever
set [fps v] to ((1) / (timer))
reset timer
end

If you don't want to reset the timer, as drmcw said, use:

when gf clicked
reset timer
set [previous timer v] to [0]
forever
set [fps v] to ((1) / ((timer) - (previous timer)))
set [previous timer v] to (timer)
end

I tried the first one and it kept coming up as 1000 or Infinity.
PopsicleSticks
Scratcher
22 posts

How do you make an fps counter?

Thank you for all the input, everyone. The game i was going to use an FPS Counter for was discontinued, though. I will leave this thread open so that you may discuss among yourselves what the best design is.

Last edited by PopsicleSticks (Feb. 15, 2014 16:31:10)

someone223
Scratcher
8 posts

How do you make an fps counter?

Try this:


when green flag clicked
reset timer
forever

set [ FPS] to (round ((1) / (timer)))
wait (1) secs
reset timer
end

Last edited by someone223 (Oct. 23, 2014 23:04:12)

juttajxwa
New Scratcher
2 posts

How do you make an fps counter?

This is my first time to browser the website, I had the same problem,Thank you for everyone's help!
Theassasin36
Scratcher
15 posts

How do you make an fps counter?

None of your does work, it says Infinity
Vetpetmon
Scratcher
1000+ posts

How do you make an fps counter?

resh123 wrote:

This is my method:
when green flag clicked
forever
set [frames v] to [0]
reset timer
repeat until <(timer) > [0.99]>
change [frames v] by (1)
end
set [fps v] to (frames)
end
It records the fps each time it checks and averages it after.

Well- I found out in turbo mode the FPS count goes about 35,000 to 50,000 FPS for some reason, that's why your game ‘Falls apart’ when you put it into Turbo mode.
Larckson3
Scratcher
62 posts

How do you make an fps counter?

Vetpetmon wrote:

resh123 wrote:

This is my method:
when green flag clicked
forever
set [frames v] to [0]
reset timer
repeat until <(timer) > [0.99]>
change [frames v] by (1)
end
set [fps v] to (frames)
end
It records the fps each time it checks and averages it after.

Well- I found out in turbo mode the FPS count goes about 35,000 to 50,000 FPS for some reason, that's why your game ‘Falls apart’ when you put it into Turbo mode.


That is because it runs the script faster in turbo mode, without increasing the amount of time it takes to do so.
juttajxwa
New Scratcher
2 posts

How do you make an fps counter?

My problem has solved ,thank you. Marquis Flowers

Powered by DjangoBB