Jump to content
Sign in to follow this  
thegunnysgt

player join group based on distance

Recommended Posts

I'm trying to create a script that will check for a players distance to another player, if they are close enough to each other it joins them into a group. I want it to do this until all players on the map are within the same group, as they find each other spread about the area, and when they all get together after joining through their own little squads have one group leader whenever they come across that player (the 1st playable unit in the list when you select your unit at the start of the mission).

Here is what I have so far, pretty rough, but I'm learning. I know I need a loop, but not sure what to put for the condition. Perhaps something to check if all players are within the same group, I don't know??

while { } do {
{
 if ((player distance _x) <= 100) then {
  player joinSilent _x
 };
} foreach playableUnits;
sleep 10;
};

Share this post


Link to post
Share on other sites

I didn't test it, so if you can get a friend to help you, give this a go:

if isserver then {
 [] spawn {
   waituntil { {_x in units group (playableunits select 0) } count playableunits == (count playableunits) };
   playersRegrouped = true;
   publicvariable "playersRegrouped";
 };
};
if !isdedicated then {
 while {isnil "playersRegrouped"} do {
   {
     if ( (group _x != group player) && (_x distance player < 100) ) then {
       if (playableunits find _x < playableunits find player) then {
         [player] joinsilent _x;
       } else {
         [_x] joinsilent player;
       };
     };
   } foreach playableunits;
   sleep 1;
 };
};
waituntil {isnil "playersRegrouped"};
hint "grouped";

Share this post


Link to post
Share on other sites

Wow thats a lot, I figured it would be something small, I'll test it out.

Could you enlighten me and explain a bit of what's going on, I can tell sort of by going through it, but I'm not familiar with some of whats there..

Share this post


Link to post
Share on other sites

if isserver then {
 [] spawn {
   waituntil { {_x in units group (playableunits select 0) } count playableunits == (count playableunits) };
   playersRegrouped = true;
   publicvariable "playersRegrouped";
 };
};

This is run only on the server. It waits for all players to be in the group of the first unit in playableunits (highest role in the role selection screen). Once that's done it tells all clients that players have regrouped.

if !isdedicated then {
 while {isnil "playersRegrouped"} do {
   {
     if ( (group _x != group player) && (_x distance player < 100) ) then {
       if (playableunits find _x < playableunits find player) then {
         [player] joinsilent _x;
       } else {
         [_x] joinsilent player;
       };
     };
   } foreach playableunits;
   sleep 1;
 };
};

This is run on all machines except dedicated server. It runs a loop until the players have regrouped. It waits for player to be within 100m of another unit that is not already in the same group. It will then check their position in the playableunits array, where the lower (==left) you are, the higher "rank" you have. Based on that, it decides which unit should join the other one's group.

waituntil {isnil "playersRegrouped"};
hint "grouped";

This is not required, but it just displays a hint when players have regrouped. It's an example condition you can use in any other script etc.

Share this post


Link to post
Share on other sites

Thanks shk, I'm pretty good with scripting, but I would have never figured this out without help.

Do I need to do anything particular with the units in the editor, like give them ranks or anything, mainly the squad leader?

Share this post


Link to post
Share on other sites

I really wouldnt know, as I said, I wasn't able to test it. :) It was just the first idea that came to my mind.

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  

×