Jump to content
Sign in to follow this  
rakowozz

BIS Function That Returns Each Element in Array?

Recommended Posts

I forgot the name... looked everywhere, functions viewer included, of course, and couldn't manage to find it!

What's the name of the function that says in its description it's "similar to count", but returns each element, instead of just the amount of elements?

Thanks in advance!

Share this post


Link to post
Share on other sites

Haha no, it's BIS_fnc_something...

foreach wouldn't help me at the moment.

Share this post


Link to post
Share on other sites

consolidatearray?

Share this post


Link to post
Share on other sites

Why not just do?

hint str(arrayName);

Share this post


Link to post
Share on other sites

Found it! Now, reading my original post again, I realize I wasn't clear at all. Forgot to mention I wanted all elements in the array fitting a certain condition.

I was looking for BIS_fnc_conditionalSelect. Can't believe it took me this long to find it haha. Here's what it does:

/************************************************************
Conditional Select
By Andrew Barron

Parameters: [array, {condition}]

This returns a sub-array of elements that satisfy a specific condition.
The originally passed array is NOT modified!

The function takes, in order, every element of the passed array,
evaluates it in the condition, and, if the condition is true, adds
it to the return array.

In the condition, the currently tested element is assigned to
the variable "_x".

Care must be taken that the condition won't cause errors. For
example, if your condition assumes all elements are numbers,
errors will occur if your array includes strings.

This function is similar to the "count" command, only it returns
the elements of the array that satisfy the condition, rather
than simply returning the count.

Example: [[10,0,8,2,6,4], {_x > 5}] call BIS_fnc_conditionalSelect
Returns: [10,8,6]

Example: [[10,true,8,player], {typename _x == "STRING"}] call BIS_fnc_conditionalSelect
Returns: []

************************************************************/

Share this post


Link to post
Share on other sites

am I missing something or is this

[[10,0,8,2,6,4], {_x > 5}] call BIS_fnc_conditionalSelect

exactly the same as this?

_array1 = [10,0,8,2,6,4];
_array2 = [];
{
if (_x > 5) then {_array2 set [count _array2,_x]}
} foreach _array1;

Share this post


Link to post
Share on other sites

@Grumpy Old Man:

Yep! As a matter of fact, it is almost exactly that (Below is the code for BIS_fnc_conditionalSelect):

/*
...Same header rakowozz posted, but I excluded it...
*/
private ["_cond","_ret","_x"];

_cond = _this select 1;
_ret = []; //to be filled with return array

{
//just call the condition directly
//note the current element is already assigned to _x, and will be accessible in the condition
if(call _cond) then {[_ret, _x] call BIS_fnc_arrayPush};
} foreach (_this select 0);

_ret

Share this post


Link to post
Share on other sites
am I missing something or is this

[[10,0,8,2,6,4], {_x > 5}] call BIS_fnc_conditionalSelect

exactly the same as this?

_array1 = [10,0,8,2,6,4];
_array2 = [];
{
if (_x > 5) then {_array2 set [count _array2,_x]}
} foreach _array1;

yes except the bis function is already in you game and don't need more memory for the server.

Share this post


Link to post
Share on other sites
Would both functions be as CPU intensive ?

Suspected the BIS function would use more cycles as they tend to, and....

http://i.imgur.com/df2jJps.png (138 kB)

Same thing with fnc_selectRandom.

Much, much better to just use

_array select (floor(random(count _array )))

Share this post


Link to post
Share on other sites

so it's better to set up your own function instead of using the BIS function already implemented in the game?

interesting

Share this post


Link to post
Share on other sites

Yeah, from what I gather the standard fnc's just chew up performance.

I was surprised myself in the beginning, but for the few functions I've tested the BIS ones always seem to perform much worse than their equivalents.

Wouldn't think anything of it if their fnc's were slightly worse, but there are massive (over 4X) improvements from using basic substitutions.

Unless there is some sort of inherent benefit of using BIS_Fnc's that I'm missing?

Someone more knowledgeable than me might know better, but it seems like inbuilt fnc's should be avoided when possible.

http://i.imgur.com/WfMVAFF.png (107 kB)

Edited by Rocksteady

Share this post


Link to post
Share on other sites

Sounds like it's related to the ms it takes to do a lookup.

Share this post


Link to post
Share on other sites
Sounds like it's related to the ms it takes to do a lookup.

Agreed. Perhaps the functions are not precompiled on load up like user created functions, thus requiring a search through configs?

At any rate, if we are talking fractions of a millisecond, I think we will all be OK.

Share this post


Link to post
Share on other sites
so it's better to set up your own function instead of using the BIS function already implemented in the game?

interesting

No.

Seriously, don't start optimizing by inlining few function calls, especially if you don't have a performance problem in the first place.

Share this post


Link to post
Share on other sites
;2631045']Agreed. Perhaps the functions are not precompiled on load up like user created functions' date=' thus requiring a search through configs?

At any rate, if we are talking fractions of a millisecond, I think we will all be OK.[/quote']

They probably are, but (guessing) when you use call or spawn the SQF engine has to go out from the local function scope to the global scope and find the variable holding the function.

Share this post


Link to post
Share on other sites

What are the numbers if you define a (local) function in a script and then call this function? Is it the function itself or is it the call command which makes a function slower (which should be faster according to Code Optimisation Hints on the biki)

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
Sign in to follow this  

×