Jump to content
jakkob4682

count how many times a function is called

Recommended Posts

i want to count how many times a function has been fired using a global variable, then when that condition is met I need to call another function.  Do I need to set a variable to true when the count is equal to the set amount or can I use waitUntil{myVariable = _myVariable}?

myVariable = 0;
while{true} do
{
	if(_params call my_fnc)then
	{
		myVariable = myVariable + 1;
	};
	waitUntil{myVariable == 3};
	_params call new_fnc;
};
true

is that how I should I write it?

Edited by jakkob4682
added snippet

Share this post


Link to post
Share on other sites

Well, if you intend to count how many times a function is called you could do that inside the function. For example you could do something like that in your function

/*
 * You function sqf
 */
// Get parameters
params[...];

// Do your thing
do stuff
do stuff
do stuff

// Increase the count
private _funcCallNum = missionNamespace getVariable[JKB_funcCallNum, 0]; // Get current value
missionNamespace setVariable["JKB_funcCallNum", _funcCallNum + 1]; // Set value to current + 1

// Return value
_retVal;

In this way you will increment the count every time the function is called. You could also add a variable and a test to check whether counting is to be used. This could possibly look like

/*
 * You function sqf
 */
// Get parameters
params[..., ["_countCalls", false, [false]]];

// Do your thing
do stuff
do stuff
do stuff

// Increase the count
if(_countCalls) then {
	private _funcCallNum = missionNamespace getVariable[JKB_funcCallNum, 0]; // Get current value
	missionNamespace setVariable["JKB_funcCallNum", _funcCallNum + 1]; // Set value to current + 1
};

// Return value
_retVal;

The only caveat here is that you should keep in mind that you have to explicitly declare you would like to count the call. This means that possibly some calls could be count while some others not. Additionally, you could set the default value of the parameter to true to make sure you will get a count if you forget to set it (if you don't need it you could just ignore the variable JKB_funcCallNum). This could be done like

params[..., ["_countCalls", true, [true]]];

where the ellipses declares all other parameters you may get inside the function.

 

As an extra let me say that you could one-line the parameter set command like


missionNamespace setVariable["JKB_funcCallNum", (missionNamespace getVariable[JKB_funcCallNum, 0]) + 1]; // Set value to current + 1

So, if you follow this approach all you have to do in order to wait for the function count to reach the value you want is

waitUntil {
	// It's good practice to use sleep to save some CPU cycles
	uiSleep 5;

	(missionNamespace getVariable["JKB_funcCallNum", 0]) == 5;
};

Of course I used 5 here as the desirable count but you could either hardcode the one you want or even get it from a variable/parameter/mission parameter etc.

 

One thing you could possibly do differently is to use a global variable instead of storing and retrieving a variable from the mission namespace (or another namespace... I used mission namespace for the sake of demonstration). This wouldn't really change the code much. You would just discard the getVariable commands and just increase by one the global variable.

 

Please note that this is not tested code and should be treated with caution.

 

Let us know if this works out for you or if you have any questions.

  • Thanks 1

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

×