Jump to content
M1ke_SK

[Performance] Get random unit from all units

Recommended Posts

Hi all,

 

I am trying to get random "_dog" from "all dogs", that have variable "dog == true". Here is what I made. Is it performance friendly? If not, how can I improve it / refactor it? Write your suggestions.

 

 

init.sqf

[] spawn 
{
    while {true} do 
    {
        _dogs = [];
        {
            _isDog = _x getVariable ["dog", false];
            if(_isDog) then {
                _dogs = _dogs + [_x];
            };
        } forEach allUnits - allPlayers;
        
        _dog = _dogs call BIS_fnc_selectRandom;

        sleep 10;
    };
};

Share this post


Link to post
Share on other sites

Best way would be to add them to a global array, instead of setting a variable and iterating through allUnits in a loop.

In the script where you setVariable ["dog",true] replace this with "MyDogsArray pushback _dog".

 

That said, exchange BIS_fnc_selectRandom with the new engine command selectRandom _dogs.

 

Cheers

Share this post


Link to post
Share on other sites
_dog = selectRandom ((allUnits - allPlayers) select {getVariable ["dog", false]});
Should work, try it.

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

×