Jump to content
Sign in to follow this  
drunken officer

Extract array postion of highest value in array

Recommended Posts

Hello everybody.

I create a group on serverside and set it public. When the players join to the server, they join to the group named as “Tango31â€

So far, so good.

But there is a problem to set the right leader. If the player with the rank "captai"n joins a bit later, the captain is not the leader but have the highest rank.

I set in my initlocalplayer.sqf to all connecting player a value:

Player setVariable [“DOF_RANKâ€,4,true]

4 is an example. Value between 1-14

In my initserver-sqf, I have a small spawn script to control the group.

[] spawn 
{
private ["_cnt", “_grparrayâ€, " _valuearray", "_value_highest"];
_cnt = -1;
_grparray = [];
while {true} do 
{
waitUntil {sleep 3; count units tango31 != _cnt};	
_grparray = [];
_valuearray = [];
 // ----- mutiarray { _grparray = _grparray + [_x,(_x getVariable "DOF_RANK") ] } count units tango31;
       { _grparray = _grparray + [_x] } count units tango31;
{ _valuearray = _ valuearray + [(_x getVariable "DOF_RANK")] } count units tango31;
_value_highest = [_valuearray, 1] call BIS_fnc_findExtreme;
player sideChat format ["%1, %2, Hoch: %3", _grparray, _valuearray , _value_highest];
//tango31 selectLeader player;
_cnt = count units tango31;
};
};

What I need is, that the player with the highest value set as leader of tango31.

BIS_fnc_findExtreme (modus 1) give me the highest value of an array. How can I extract the array-position ?

With this position I can set the leader

Tango31 selectleader (_grparray select (postion of highest value))

Share this post


Link to post
Share on other sites

try foreach

_highestRank = 0;
{
 if ((_x getVariable "DOF_RANK") >  _highestRank ) then
 {
 _highestRank = (_x getVariable "DOF_RANK");
 Position of highest player = _foreachindex;
 };
}foreeach tango31;

Edited by Squeeze

Share this post


Link to post
Share on other sites

private ["_index", "_highest"];
_index = -1;
_highest = -1;

{
if(_x getVariable ["DOF_RANK", -1] > _highest) then {
	_highest = _x getVariable ["DOF_RANK", -1];
	_index = _forEachIndex;
};
} forEach units tango31;

_highest would be your highest rank, _index would be index of unit with this rank in the array

Edited by SaMatra

Share this post


Link to post
Share on other sites

Does 'foreach <groupname>' really go through units in the group? I didn't know that. :confused:

Or did you mean forEach (units tango31) ?

Share this post


Link to post
Share on other sites

Thx a lot.

Here my code. it works in editor. Maybe i've to call the selectleader line via BIS_BNC_MP for dedicate server.

[] spawn 
{
private ["_cnt"];
_cnt = 0;
_grparray = [];
while {true} do 
{
waitUntil {sleep 3; count units tango31 != _cnt};	
_grparray = [];
private ["_index", "_highest"];
	_index = -1;
	_highest = -1;
{ _grparray = _grparray + [_x] } count units tango31;
	{
		if(_x getVariable ["DOF_RANK", -1] > _highest) then {
			_highest = _x getVariable ["DOF_RANK", -1];
			_index = _forEachIndex;
		};
	//player sideChat format ["INDEX: %1 Name: %2", _index, (_grparray select _index)];

			} forEach units tango31;

	tango31 selectleader (_grparray select _index);
_cnt = count units tango31;
};

Share this post


Link to post
Share on other sites
Does 'foreach <groupname>' really go through units in the group? I didn't know that. :confused:

Or did you mean forEach (units tango31) ?

Ah, I was expecting it to be an array of units, forEach (units tango31) indeed then.

Share this post


Link to post
Share on other sites

You do not need to iterate through group members every time. All you need to know is if the unit that joined has a higher rank than the existing leader.

This should be executed everywhere upon player joining

[color="#FF8040"]KK_fnc_allocateLeader [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#7A7A7A"]"_grp"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_grp[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]group[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#8B3E2F"][b]![/b][/color][color="#191970"][b]local[/b][/color] [color="#1874CD"]_grp[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color]
	[color="#000000"]_this[/color] [color="#191970"][b]getVariable[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"rank"[/color][color="#8B3E2F"][b],[/b][/color] [color="#FF0000"]-1[/color][color="#8B3E2F"][b]][/b][/color] [color="#8B3E2F"][b]>[/b][/color] 
	[color="#8B3E2F"][b]([/b][/color][color="#191970"][b]leader[/b][/color] [color="#1874CD"]_grp[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]getVariable[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"rank"[/color][color="#8B3E2F"][b],[/b][/color] [color="#FF0000"]-1[/color][color="#8B3E2F"][b]][/b][/color]
[color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]then[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
	[color="#1874CD"]_grp[/color] [color="#191970"][b]selectLeader[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

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  

×