Jump to content
sgtelis

Detect last Group alive

Recommended Posts

So I am making a small scale BR styled missions for me and my friends to play and I've got it working so far with out groups, aka last man standing using:

if(({alive _x} count ([] call BIS_fnc_listplayers)) < 2) then {

and it works for that!

 

Now, I've added in functionality for group play into the mission and I now want to change this code to instead detect the last group that has alive players in it so that the last standing group does not have to kill their teammates for the game to be over. I then assume that this would work in the regular solo play to since every unit should be in a group by them selves.

 

I've tried searching for it but only find things about Player Unknowns Battle Royale (Big supprise) and how to move groups in vehicles and stuff.

 

Thanks in advance!

Share this post


Link to post
Share on other sites
waituntil {{({alive _x} count units _x > 0)} count allgroups isequalto 1};
_lastAliveGroup = allgroups select {{alive _x} count units _x > 0} select 0; 

waituntil returns true if there's only one group with alive members left and returns the _lastAliveGroup.

 

 

Cheers

Share this post


Link to post
Share on other sites

Hmm, needs some more testing but can't seem to get it to work.

Im using Dynamic Groups as my way of allowing the players to create groups and so far I've only been able to try it out my self and when I am solo in a group (aka the only one in a group and alive) it's not turning true.

Am I missunderstanding this and that it require more than 1 group or more than 1 player in the groups?

Share this post


Link to post
Share on other sites

Hmm, needs some more testing but can't seem to get it to work.

Im using Dynamic Groups as my way of allowing the players to create groups and so far I've only been able to try it out my self and when I am solo in a group (aka the only one in a group and alive) it's not turning true.

Am I missunderstanding this and that it require more than 1 group or more than 1 player in the groups?

Should work just fine and return true if there's only one group with alive members in it.

Groups created during runtime shouldn't affect this.

Did you try it in empty editor?

 

Cheers

Share this post


Link to post
Share on other sites

Ok, so I did some testing.

 

Create a empty mission and spawn in one unit.

Place code in init.sqf

Works.

 

Placed more groups.

Killed zed units in zed groups.

Does not work.

 

There is our problem.

Share this post


Link to post
Share on other sites

Ok, so I did some testing.

 

Create a empty mission and spawn in one unit.

Place code in init.sqf

Works.

 

Placed more groups.

Killed zed units in zed groups.

Does not work.

 

There is our problem.

No problem at all, working fine here.

Are you sure there are no other groups on the map with alive members in it?

Also putting waituntil or sleep in the init.sqf is bad practice, even for testing purposes.

 

Cheers

Share this post


Link to post
Share on other sites

Ok, now it never want's to be true.

Did this in a completly new mission:

 

init.sqf:

execVM "test.sqf";

test.sqf:

hint "Before";
waituntil {{({alive _x} count units _x > 0)} count allgroups isequalto 1};
hint "After";

Placed one unit, Only hints "Before".

Placed two units and killed the other one, Only hints "Before".

Placed one extra unit in every group and killed the other group, Only hints "Before".

 

 

EDIT:

Changed the test.sqf to this for some more testing:

hint "Before";
while {{({alive _x} count units _x > 0)} count allgroups > 4} do {
	hint "During";
	sleep 1;
};
hint "After";

and then it hints "After" but only if I have "allGroups > 4", if I have it anything below (aka > 3, > 2, etc) it does not exit the while loop even though I only have one unit (the player) in the whole mission.

 

 

 

EDIT 2:

By doing this:

hint format ["%1", count allGroups];

it hints "20", which means there are 20 groups in the mission? Even though there is just a single unit in the mission?

Share this post


Link to post
Share on other sites

A thought is that maybe I have a mod installed that screws around with the groups?

So maybe if I checked if all living players are in the same group or not and if they are I continue with the script.

Share this post


Link to post
Share on other sites

That's what I meant.

Do you have some game logics placed?

You could further specify the condition to only count groups of a certain side:

_getLastAliveGroup = [west] spawn {

    params ["_side"];
    systemchat "Started groupcheck!";

    waituntil {systemchat format ["Current alive groups: %1",allgroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side}];sleep 3;{({alive _x} count units _x > 0) AND side _x isEqualTo _side} count allgroups isequalto 1};
    
    _lastAliveGroup = allgroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side} select 0;
    systemchat format ["Last alive group: %1",_lastAliveGroup];
};

Will only check west sided groups.

 

Or this to check for predefined groups, probably the solution you need:

_myGroups = [group player, blufor1, blufor2, whatever];

_getLastAliveGroup = [_myGroups] spawn {
params ["_myGroups"];
systemchat "Started groupcheck!";
waituntil {systemchat format ["Current alive groups: %1",_myGroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side}];sleep 3;{({alive _x} count units _x > 0) AND side _x isEqualTo _side} count _myGroups isequalto 1};
_lastAliveGroup = _myGroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side} select 0;
systemchat format ["Last alive group: %1",_lastAliveGroup];
};

Cheers

Share this post


Link to post
Share on other sites

Using the first piece of code provided showed me that every side has a HQ, and prob. a bunch of logics. Think it has to do with MCC and maybe ACE 3.

 

This seems to be steps in the right direction though.

I did this:

_myGroups = [group p1, group p2, group p3, group p4, group p5, group p6, group p7, group p8, group p9, group p10]; 
 
_getLastAliveGroup = [_myGroups, west] spawn { 
	params ["_myGroups", "_side"]; 
	systemchat "Started groupcheck!"; 
	waituntil {systemchat format ["Current alive groups: %1",_myGroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side}];sleep 3;{({alive _x} count units _x > 0) AND side _x isEqualTo _side} count _myGroups isequalto 1}; 
	_lastAliveGroup = _myGroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side} select 0; 
	systemchat format ["Last alive group: %1",_lastAliveGroup]; 
};

And tried it with AI, if two players happen to be in the same group the group gets counted as two (or as many that are in the group).

So for example, two players are in "Alpha 1-1" then that will say "Groups alive: B Alpha 1-1, B Alpha 1-1" and not end.

Im using Dynamic Groups btw so not predefined.

Share this post


Link to post
Share on other sites

I wouldnt recommend doing what has been suggested, having a waituntil without a sleep in it running on server is gonna suck juice.
 

//Find Last Alive player group

_groups = []; 
_loop = true; 
 
{  
  
 if (_x == leader group _x) then {_groups pushback group _x};  
   
} foreach allplayers;  
  
while {_loop} do {  
  
 {  
   
  if (count units _x == 0) then {_groups = groups - [_x]};  
  
 } foreach _groups; 
   
 if ((count _groups) == 1) then {_loop = false;};  
 
 sleep 10;
 
};  
  
last_group = _groups select 0;  
  
last_group;

Edit: Wrote this before you guys continued so this mightve already been solved.

Share this post


Link to post
Share on other sites
{  
  
 if (_x == leader group _x) then {_groups pushback group _x};  
   
} foreach allplayers; 

That is what I needed! ^^

Was trying to figure out how to make just that.

Still going to need to test this with my friends when they come online but I believe that should do the trick!

 

Thanks for the help guys!

I'll be back if I have any issues after propper testing.

 

 

End result:

_myGroups = [];
 
_getLastAliveGroup = [_myGroups, west] spawn { 
	params ["_myGroups", "_side"]; 
	
	{
		if(_x == leader group _x) then {
			_myGroups pushback group _x;
		}
	}forEach allPlayers;
	
	systemchat "Started groupcheck!"; 
	waituntil {systemchat format ["Current alive groups: %1",_myGroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side}];sleep 3;{({alive _x} count units _x > 0) AND side _x isEqualTo _side} count _myGroups isequalto 1}; 
	_lastAliveGroup = _myGroups select {{alive _x} count units _x > 0 AND side _x isEqualTo _side} select 0; 
	systemchat format ["Last alive group: %1",_lastAliveGroup]; 
};

Share this post


Link to post
Share on other sites

Remember to add a sleep to the start of that waituntil otherwise that code is gonna run as often as possible and hog resources.

Eg: waituntil {sleep 10; systemchat format ....yada yada};

Edit: Blind

Share this post


Link to post
Share on other sites

Ohhh right in the middle lol, Ok nevermind

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

×