The discussion of infinite loops got me tinkering. The following script ended up being a bit of a test-bed for a number of techniques including 'auto-load' of an action and associated code from a single file.
Just add this to your players initialization field:-
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">s=[] execVM "fps.sqf"[/QUOTE]
The should then find that your player has a new action called "Toggle FPS" which turns the counter on and off. I've tested this against FRAPS and it seems to give consistent results.
The original file without the crappy CODE reformatting and _much_ easier to read is here.
Top tip - it looks an awful lot nicer with syntax highlighting turned on. Treating it as a 'C' file seems to give the best results in emacs.
If you can't see the link above, the content (with unreadably mangled format) is below...
FILE: fps.sqf
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
// In-game FPS (Frames Per Second) counter
//
// Version 1.0, 28 dec 06
// Created by sbsmac
//
// To use this script add the following to the player initialization field:-
//
// s=[] execVM "fps.sqf"
//
// Note that 's' is a dummy variable to keep the parser happy.
//
// You should then find that your action menu contains 'Toggle FPS'.
// Select this option to toggle fps counting on and off
if (count _this ==0 ) then {
//This is the code that is run at initialisation. It sets up
//the required variables and functions and adds the 'toggle' action.
fpsState = "stopped"; //global variable to control state of counter
//the counter function- this runs continuously until halted
startFps = {
fpsState="running";
//build in a little bit of smoothing by averaging the last n frames
_numFrames = 10;
_frameIndex=0;
_frameTimes=[];
//clear out the buffer (needs to be one larger than num frames)
for "_i" from 0 to (_numFrames) do {_frameTimes set [_i,0];};
//use a double loop here because of the ArmA limitation that
//loops terminate after 10000 iterations
while {fpsState == "running"} do {
while {fpsState == "running"} do {
_frameTimes set [_frameIndex,time];
_frameIndex = (_frameIndex +1) % (_numFrames+1);
hint format ["fps %1 (over last %2 frames)",
round((_numframes)/(time-(_frameTimes select _frameIndex))),
_numFrames];
//delay a little bit - this forces a wait until
//the next frame
Sleep 0.001;
};
};
hint ""; //clear the hint box
fpsState = "stopped";
};
//function to request that the counter be stopped
stopFps = {
fpsState = "stopping";
};
//having set up the functions, add an action to control them
//the exact string passed to the script is actually irrelevant
player addAction ["Toggle FPS","fps.sqf",["toggle"&# 93;];
} else {
//We've been called from the action menu so toggle fps counting.
//fpsState has 3 states to guard against the (very narrow) race
//condition where a user can hit the toggle action very quickly
//twice in succession. This could cause two instances of the counter
//to run if a simple boolean state was used
switch (fpsState) do {
case "running" : { call stopFps;};
case "stopped" : { call startFps;};
case "stopping" : {}; //do nothing
};
};
[/QUOTE]
Please, no complaints that the double while-loop should be a 'waitUntil'
*Edit* reformatted CODE section following Sickboy's 'no-tabs' suggestion
HOME 
Reply With Quote




