hmmm what i mean is something like this :
sortbubble.sqf :
by bn880
I removed parameter count check and added "_unit" to keep
numeric value enumeration scalar.
private ["_unit", "_array","_eval","_numArray","_j","_k","_tempVal","_tempNum","_element"];
_array = _this select 0;
_eval = _this select 1;
_unit = _this select 2;
_count = count _array;
_numArray = [];
_numArray resize _count;
// ACQUIRE NUMERIC VALUES FROM EVALUATION
_j = 0;
while "_j < _count" do
{
_element = _array select _j;
_numArray set [_j,call _eval];
_j = _j + 1;
};
_j = 0;
// SORT
while "_j < _count -1" do
{
_k = _j + 1;
while "_k < _count" do
{
if (_numArray select _j > _numArray select _k) then
{
_tempNum = _numArray select _j;
_numArray set [_j,(_numArray select _k)];
_numArray set [_k,_tempNum];
_tempVal = _array select _j;
_array set [_j,(_array select _k)];
_array set [_k,_tempVal];
};
_k = _k + 1;
};
_j = _j + 1;
};
init.sqs :
bubblesort = preProcessFile "sortbubble.sqf"
now here is a script I run on a unit.
[this, "SOMEID", trigger] exec "near.sqs"
you can use this inside mission editor at unit init row.
the trigger provides units list.
best way would be to pass "this" as _id, but it would
crash the script couse of whitespaces.
I found another way to enum a valid unique unit id.
itÂ's not the best way i think, but it works
and it works just as long as you use only ONE unit list trigger:
unique_id.sqf :
private ["_units", "_trigger", "_unit", "_i", "_j", "_c"];
_unit = _this select 0;
_trigger = _this select 1;
_units = list _trigger;
_id = "X";
_i = 0;
_j = count _units;
while ("_i<_j") do {
_c = _units select _i;
if (_c == _unit) then { _id = _i; _i = _j; } else { _i = _i + 1; };
};
if( format["%1", _id] != "X" ) then { format ["%1",_id] } else { hint "UNIQUE ID ERROR" }
this will return a simple integer. so I used a all units list loop script to start
near.sqs:
[((list _trigger) select _i), [((list _trigger) select _i), _trigger] call uid, _trigger] exec "near.sqs"
where uid = preProcessFile "unique_id.sqf" ; // at init.sqs
now the main script
near.sqs :
_unit = _this select 0
_id = _this select 1
_trigger = _this select 2
_nearestrefresh = 1
; // variable variable string here
_near_array = "GLB_NEAR_"
; // this is why i added "_unit" to bn880 sortbubble code
_eval = "_element distance _unit"
; // variable variable init
; // will set "GLB_NEAR_1 = []" if _id is 1 ...
call format["%1%2 = []", _near_array, _id]
#loo
?!(alive _unit): goto "exit"
_units = list _trigger
~0.1
_i = 0
_catched = []
; // prefilter to sort units
while "_i < count _units" do { _c = _units select _i; if (("Man" countType [_c]) == 1 and ((_unit knowsAbout _c) > 0) and (_c != _unit) and (alive _c) and !(_c in units (group _unit)) ) then { _catched = _catched + [_c]; }; _i=_i+1; }
~0.1
?((count _catched) < 1): goto "skip"
; // sort
[_catched, _eval, _unit] call bubblesort
~0.1
; // set nearunits to variable global var
call format["%1%2 = _catched", _near_array, _id]
; // now any other script can find nearest unit to _unit with "GLB_NEAR_1 select 0"
; // if _id is 1, second nearest unit is ... select 1 [...]
; // just pass _id to other scripts to keep all this stuff scalar. :)
~0.1
#skip
?((call format["count %1%2", _near_array, _id]) < 1): _unit globalChat format ["%1: iÂ'm alone here !", name _unit]
; // update every 1 - 2 seconds
_randnext = (random _nearestrefresh) + _nearestrefresh
~_randnext
goto "loo"
#exit
_unit globalChat format ["%1: near exit", name _unit]
exit
and it works
thanks Dinger !