When I was using the built-in FPS counter it just occured to me that game designer may want to provide his game with a special option to show frames per second (FPS) speed. Using the internal one is possible but there are two events when you can't:
1. Using built-in FPS counter (Debug(4,1);) is possible only if debug mode is turned on. But it should be disabled for finally released game.
2. You want to use another font/color for your game's fps textstring.
NOTE: You can't use GetGameSpeed() as it only returns currently set game speed not actual one.
So here is it:
GetFPS();
Returns current FPS speed.
Next the source script & installation instructions:
Place the next lines into the main global script:
// main global script file
int var_fps=0;
int var_fpslast=0;
int var_prevtime=0;
function GetFPS() {
return var_fpslast;
}
function FPS_Repeatedly() {
var_fps++;
int curtime = GetRawTime();
if (curtime-var_prevtime>=1) {
var_fpslast = var_fps;
var_prevtime = curtime;
var_fps=0;
}
}
Place the next line into repeatedly_execute() function:
// main global script file
function repeatedly_execute() {
FPS_Repeatedly();
}
And finally into the script header part:
import function GetFPS();
Now you may use it to display FPS speed using TextOverlays or something else:
repeatedly execute the next code:
...
string FPS_string;
StrFormat(FPS_string, "Game speed (FPS): %d", GetFPS());
//SetTextOverlay (int overlay_id, int x, int y, int width, int font, int color, string text)
SetTextOverlay (id, 5, 170, 150, 0, 15, FPS_string);
//note: you have to create TextOverlay first: id = CreateTextOverlay(...);
...
or just use GUI labels.
That's all. Have fun ;)
EDIT:Since the GetFPS function calculates the number of calls (for each second) of repeatedly_execute() function it will not update the FPS value if the script is blocked. Just for reference.
-Cheers
Edit by strazer:
To make it update during blocking sequences, you can use repeatedly_execute_always now.
In order to get this script working in 3.2.1, had to make a few changes, Im sharing them in case anyone gets stuck trying to get it to work like I was:
change
int curtime = GetRawTime();
to
DateTime *dt = DateTime.Now;
int curtime = dt.RawTime;
and change
StrFormat(FPS_string, "Game speed (FPS): %d", GetFPS());
SetTextOverlay (int overlay_id, int x, int y, int width, int font, int color, string text)
SetTextOverlay (id, 5, 170, 150, 0, 15, FPS_string)
to
FPS_string = String.Format("Game speed (FPS): %d", GetFPS());
textOverlay = Overlay.CreateTextual(30, 70, 200, Game.NormalFont, 15, FPS_string );