Jump to content
prototype1479

Real-time FPS counting script (Automatic script/loop optimizer)

Recommended Posts

You are free to use it on your own will : 

_v = -1
_t = 0.99999
_j = 1000
_a = [0]
_b = []
FPS_NOW = 0
FPS_AVG = 24
FPS_MAX = 48

#loop
_t0 = _time
~0.001
_t1 = _time
_v = _v + 1
_a set [_v , 1 / (_t1 - _t0) * acctime]
_k = 0
_i = 0
{if (_i <= _j) then {_k = _k + _x} else {_a set [_i , objnull]} ; _i = _i + 1} foreach _a
_a = _a - [objnull]
_k = _k / count _a
? FPS_MAX < count _a : FPS_MAX = count _a
if (FPS_MAX >= _k - _k % 1) then {FPS_NOW = _k - _k % 1} else {FPS_NOW = FPS_MAX + 1}
? _t < _time : _j = _v ; _v = -1 ; _t = _time + 0.99999 ; _b = _b + [count _a] ; _c = 0 ; {_c = _c + _x} foreach _b ; FPS_AVG = _c / count _b
player groupchat format ["FPS_NOW : %1 , FPS_MAX : %2 , FPS_AVG : %3" , FPS_NOW , FPS_MAX , FPS_AVG]
goto "loop"

 

FPS_NOW = Shows your monitor's FPS realtime

FPS_MAX = Maximum FPS that your monitor can achieve (Works only if it has reached that much FPS)

FPS_AVG = The average amount of FPS your monitor had from the start

 

This script is great for optimizing loop's by increasing your delays or making the unit move with setPos command the way you wanted even though it gets slowed down in a loop.

 

I have done this on my climb script and AI take cover script but those were showing FPS per second instead of showing it real-time.

 

For example lets say that you got a loop like this : 

 

#loop

~1.5
some code...

goto "loop"

 

And if you do this to your delay like this :

 

#loop

~1.5 * FPS_MAX / FPS_NOW
some code...

goto "loop"

 

Your loop with this script will get optimized.

 

Now how does this work - let's say that FPS_MAX is 60 and FPS_NOW is 60

 

Then that means : 

 

1.5 * 60 / 60 = 1.5 * 1 = 1.5

 

So that means loop's delay wont change it will be the same since the computer has calculated that this is the maximum amount of frames that you can achieve.

 

But what if FPS_NOW is 30 then this will happen :

 

1.5 * 60 / 30 = 1.5 * 2 = 3.0

 

So your loop got optimized depending on the FPS.

 

Here's another example - when you want to make some movement with setPos on a loop like this : 

 

#loop

~0.016
player setvelocity [0 , 0 , 0]
player setpos [getpos player select 0 , getpos player select 1, (getpos player select 2) + 0.001]

goto "loop"

 

And if your frame rates drop then the setPos movement you made will slow down too.

 

But if you add this at the 3rd element of setPos array :

 

#loop

~0.016
player setvelocity [0 , 0 , 0]
player setpos [getpos player select 0 , getpos player select 1, (getpos player select 2) + 0.001 * FPS_MAX / FPS_NOW]

goto "loop"

 

It will fix this issue.

 

So this means that if you have started to lag and this will automatically fix your setPos movement script depending on your frame rate.

 

By the way FPS_NOW might go over 60 FPS even though your monitor can achieve 60 FPS and can't go beyond that.

 

This is the work of V-sync it's going to correct itself slowly or when you ALT + TAB

 

Here's a video on this script in action :

 

 

If you look closely then you will see that if my screen is aiming towards the tanks then not only I will start to lag the FPS counter will also decrease

 

That's it if you find bugs please let me know here.

  • Like 1

Share this post


Link to post
Share on other sites

This might and probably the last time I update this script but if there's any bugs I might need to update again

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×