FPS (frames-per-second) is a value that shows the current frame rate. Showing the current frame rate can be useful for debugging projects that may affect Scratch's performance.
Making an FPS Variable
Timer in Use Method
If the timer is being used, the following script can be used:
when flag 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
How it Works
When the flag is clicked, the timer is reset, a variable, in this example, "Previous Timer," is set to 0 and and a forever loop starts. In this loop, another variable, in this example, "FPS," is always set to 1 divided by the current value of the timer subtracted by the value of the "Previous Timer" variable, and the "Previous Timer" variable is set to the current timer. After this block's script is run, the timer is reset. The value of the "FPS" variable is roughly the value of the current FPS.
Note: | A round () block can be used to display the counter as an integer. |
Timer Not in Use Method
Caution: | The following script given cannot be used if the timer is already used for other purposes since this could break the project, as it constantly resets the timer. |
The current FPS can be shown with the following script if the timer is NOT being used:
when flag clicked reset timer forever set [FPS v] to ((1)/(timer)) reset timer end
How it Works
When the flag is clicked, the timer is reset and and a forever loop starts. In this loop, a variable, in this example, "FPS," is always set to 1 divided by the current value of the timer. After this block's script is run, the timer is reset. The value of the "FPS" variable is roughly the value of the current FPS.
DeltaTime
DeltaTime is a method to "smoothen out" a project based on the delay between the current and the last frame. An example of a use for this is when a project meant to run in 30 fps is being run at 60 fps, this usually just doubles the speed at which the code is run and thus the game operates twice as fast, deltatime fixed this problem by using the fps to smoothen the project.
Caution: | Make sure the custom block is NOT set to run without screen refresh! Also make sure that the calc deltatime function is run in it's own forever loop, otherwise it will cause pauses between movements because of the wait 0 seconds block. |
define Calculate DeltaTime and FPS (last frame) (reg fps) wait (0) seconds //this block is used to wait one frame since when the function was called set [DeltaTime v] to(((reg fps)*(86400))*((days since 2000)-(last frame))) set [FPS v] to (round((reg fps)/(DeltaTime))) when flag clicked forever Calculate DeltaTime and FPS (days since 2000) (30) when flag clicked forever move ((10)*(DeltaTime)) steps
How it works
It uses the Days Since 2000 reporter, which is a more accurate indicator of time than the timer block, to calculate how long it has been since the last frame and uses that data to calculate the DeltaTime, you can then multiply the DeltaTime variable with how much you want to move a sprite by, for example, to move the sprite that much in the regular fps you provided.