Jump to content
Sign in to follow this  
hogmason

count alive unit issue

Recommended Posts

hi i have done a script that spawns 5 pows, and have attempted to create a count of all 5 pows spawned and at the bottom of the code i have a while command that i hoped would decide the end result for the task by 2 ways

1) if all alive POWs were returned to base a tent named

POW_safe

then the task will complete a success

2) if 3 pows were killed = the task failes

//create POWS
//count alive POWs
AlivePOWs = {({alive _x} count _this)};
if (isServer) then {

waituntil {!isnil "bis_fnc_init"};

//set POWs state at start
_POWs_death_Limit = false;
_POWs_rescued = false;

//create POWs 
_units = [];
_POWsArray = [];
  for "_i" from 0 to 5 do 
  {
     _POWman = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;
     _jail = getMarkerPos "jail";
     _POWgrp = createGroup west;
     _POW = createVehicle [_POWman, _jail, [], 0, "CAN_COLLIDE"];
     _POW setcaptive true;
     _POW setBehaviour "CARELESS";
     removeAllWeapons _POW;
     _POW call {[_POW,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     _POW setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;
  _POWsArray set[(count _POWsArray), _POWman];
       if (HOG_debug) then 
       { 
         _handle = [_POW,"pow","ColorRed","mil_dot"] spawn HOGdebugmkrs;
       }; 
    };


while {!_POWs_rescued && !_POWs_death_Limit} do {
  //check to see if 3 POWs have been killed if so fail task	 
  if ((_units call AlivePOWS) == 2) then 
    {
        _POWs_death_Limit = true;
  	      ["Task1","failed"] call SHK_Taskmaster_upd;
      [] call SHK_addTask; 
    };	 
  //check to see if POWs have been returned to base if so complete task
  if (_units distance POW_safe < 20) then 
    {
       _POWs_rescued = true;
       ["Task1","succeeded"] call SHK_Taskmaster_upd; 
       [] call SHK_addTask; 
    };

 };	 


};  

however i am doing something wrong well most likely alot but ive given it a good go and cant do it i do get 1 error in my rpt log ill post it below

if any 1 knows.

if (_units distance POW_safe < 20) then 
{
_POWs_re>
 Error position: <distance POW_safe < 20) then 
{
_POWs_re>
 Error 0 elements provided, 3 expected
File C:\Users\natho\Documents\ArmA 2 Other Profiles\=Mason=\missions\testtakitestttestestestestest.Takistan\missions\scripts\pow_spawn.sqf, line 45
Error in expression < 
[] call SHK_addTask;
};


Share this post


Link to post
Share on other sites

_units sounds like an array of unit pointers. You can use distance only with one unit.

Share this post


Link to post
Share on other sites

Try something like this

if ({(_x distance POW_safe) < 20} count _units  == (count _units)) then 

Share this post


Link to post
Share on other sites

Something like this should work

_obj = POWSafe;
_grp = [POW1,POW2,POW3];
_safe = 0;
_safedistance = 20;

while {true} do {

sleep 1;
{
if ((_x distance _obj) < _safeDistance) then {_safe = (_safe + 1)};
if (!alive _x) then {_grp = _grp - _x;};
} forEach units _grp;

if (count _grp == 0) exitWith (hint "Too many POWs died!");
if (_safe >= count _grp) exitWith (hint "all living POWs made it!");

};

Share this post


Link to post
Share on other sites

Couple of quick things I noticed...

You are spawning 6, not 5 pows

Use the createUnit array, not createVehicle. If you do this, create the POW in the group, then reference the group, like this:

_POW = _POWgrp createUnit [_POWman, _jail, [], 0, "FORM"];

I don't see where you ever added anything to _units[]

Share this post


Link to post
Share on other sites

You don't need the _POWsArray if you add the _POW to the _POWgrp, reference the group. Do you have another group or unit placed on the map of the same side? If not you'll need to create a center

Share this post


Link to post
Share on other sites

yeah ive got 1 unit from each side on map with 0 presence. i have everything spawning fine including pows but just cant get my tasks to end the way i need them by the pows either

to many dead or within 20 meters from the tent lol

---------- Post added at 00:21 ---------- Previous post was at 00:04 ----------

ok so i changed it to createunit and a few more things done now it loads with no rpt error but the hint

all living POWs made it!

keeps flashing in the corner straight after the pows spawn

and i can kill all of the pows and get no

Too many POWs died

hint

seems to me like the powgrp is not being referenced or something

this is the code

//create POWS
//count alive POWs
if (isServer) then {

waituntil {!isnil "bis_fnc_init"};
//create POWs 
   for "_i" from 0 to 4 do 
   { 
  _POWgrp = createGroup west;
     _POWman = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;
     _jail = getMarkerPos "jail";      
     //_POW = createVehicle [_POWman, _jail, [], 0, "CAN_COLLIDE"];
  _POW = _POWgrp createUnit [_POWman, _jail, [], 0, "FORM"];
     _POW setcaptive true;
     _POW setBehaviour "CARELESS";
     removeAllWeapons _POW;
     _POW call {[_POW,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     _POW setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;
         if (HOG_debug) then 
         { 
           _handle = [_POW,"pow","ColorRed","mil_dot"] spawn HOGdebugmkrs;
         }; 
    };

_obj = POWSafe;
//_grp = [POW1,POW2,POW3,POW4,POW5];
_safe = 0;
_safedistance = 20;

while {true} do {

sleep 1;
{
if ((_x distance _obj) < _safeDistance) then {_safe = (_safe + 1)};
if (!alive _x) then {_POWgrp = _POWgrp - _x;};
} forEach units _POWgrp;

if (count _POWgrp == 3) exitWith (hint "Too many POWs died!");
if (_safe >= count _POWgrp) exitWith (hint "all living POWs made it!");

};	 


};  



---------- Post added at 00:41 ---------- Previous post was at 00:21 ----------

THIS IS WERE I GOT TO AND WERE I AM OFFICIALLY STUCK stuck :(

//create POWS
//count alive POWs
if (isServer) then {

waituntil {!isnil "bis_fnc_init"};
_obj = POWSafe;
_safe = 0;
_grp = [POW1,POW2,POW3,POW4,POW5];
_safedistance = 20;
_POWgrp = createGroup west;
  //create POWs 
   for "_i" from 0 to 4 do 
   { 
     _POWman = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;
     _jail = getMarkerPos "jail";      
  _POW = _POWgrp createUnit [_POWman, _jail, [], 0, "FORM"];
     _POW setcaptive true;
     _POW setBehaviour "CARELESS";
     removeAllWeapons _POW;
     _POW call {[_POW,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     _POW setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;
         if (HOG_debug) then 
         { 
           _handle = [_POW,"pow","ColorRed","mil_dot"] spawn HOGdebugmkrs;
         }; 
    };

  while {true} do 
  {

     sleep 1;
     {
        if ((_x distance _obj) < _safeDistance) then {_safe = (_safe + 1)};
        if (!alive _x) then {_POWgrp = _grp - _x;};
     } forEach units _POWgrp;

        if (count _POWgrp == 3) exitWith (hint "Too many POWs died!") then 
	   {
        ["Task1","failed"] call SHK_Taskmaster_upd;
        [] call SHK_addTask;

          };

           if (_safe >= count _POWgrp) exitWith (hint "all living POWs made it!") then 
		  {
            ["Task1","succeeded"] call SHK_Taskmaster_upd; 
            [] call SHK_addTask;
             };

  }; 


}; 

---------- Post added at 00:42 ---------- Previous post was at 00:41 ----------

sorry about caps

Share this post


Link to post
Share on other sites

1. {_POWgrp = _grp - _x;}; should be {_POWgrp = _POWgrp - _x;};

2. I think I've been a bit unclear: _grp has to be an array, it's not going to work when it's referencing a group object

If you put a _grp = [] at the beginning of the script and a _grp = _grp + _POW inside the for loop my code should work.

3. It helps a lot to use stuff like this for debugging:

player sidechat format ["%1,%2",_POWgrp,_grp];

Share this post


Link to post
Share on other sites


Yup.
_POWgrp being a group, this:

count _POWgrp

will not work.

count units _POWgrp

will return the number of units in that group.

Share this post


Link to post
Share on other sites

cheers guys but still can not get it

its strange nothing i try changes the fact that when i load the mission and the pows spawn instantly i get a hint saying

all living POWs made it!

now that should only appear if they are within 20mtrs of the tent named = POWSafe

so this is what im doin

i create the group

_POWgrp = createGroup west;

the above group is called outside the

for "_i" from 0 to 4 do

{

};

then spawn the pows

   for "_i" from 0 to 4 do 
   { 
     _POWman = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;
     _jail = getMarkerPos "jail";      
  _POW = _POWgrp createUnit [_POWman, _jail, [], 0, "FORM"];
     _POW setcaptive true;
     _POW setBehaviour "CARELESS";
     removeAllWeapons _POW;
     _POW call {[_POW,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     _POW setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;
         if (HOG_debug) then 
         { 
           _handle = [_POW,"pow","ColorRed","mil_dot"] spawn HOGdebugmkrs;
         }; 
    };

then i call the conditions

  while {true} do 
  {

     sleep 10;
     {
        if ((_POW distance _obj) < _safeDistance) then {_safe = (_safe + 1)};
        if (!alive _POW) then {_POWgrp = _POWgrp - _POW;};
     } forEach units _POWgrp;

        if (count units _POWgrp == 3) exitWith (hint "Too many POWs died!") then 
	   {
        ["Task1","failed"] call SHK_Taskmaster_upd;
        [] call SHK_addTask;

          };

           if (_safe >= count units _POWgrp) exitWith (hint "all living POWs made it!") then 
		  {
            ["Task1","succeeded"] call SHK_Taskmaster_upd; 
            [] call SHK_addTask;
             };

  }; 


whole thing

if (isServer) then {

waituntil {!isnil "bis_fnc_init"};
_obj = POWSafe;
_safe = 0;
//_grp = [POW1,POW2,POW3,POW4,POW5];
_safedistance = 20;
_POWgrp = createGroup west;
  //create POWs 
   for "_i" from 0 to 4 do 
   { 
     _POWman = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;
     _jail = getMarkerPos "jail";      
  _POW = _POWgrp createUnit [_POWman, _jail, [], 0, "FORM"];
     _POW setcaptive true;
     _POW setBehaviour "CARELESS";
     removeAllWeapons _POW;
     _POW call {[_POW,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     _POW setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;
         if (HOG_debug) then 
         { 
           _handle = [_POW,"pow","ColorRed","mil_dot"] spawn HOGdebugmkrs;
         }; 
    };

 player sidechat format ["%1",_POWgrp];

  while {true} do 
  {

     sleep 10;
     {
        if ((_x distance _obj) < _safeDistance) then {_safe = (_safe + 1)};
        if (!alive _x) then {_POWgrp = _POWgrp - _x;};
     } forEach units _POWgrp;

        if (count units _POWgrp == 3) exitWith (hint "Too many POWs died!") then 
	   {
        ["Task1","failed"] call SHK_Taskmaster_upd;
        [] call SHK_addTask;

          };

           if (_safe >= count units _POWgrp) exitWith (hint "all living POWs made it!") then 
		  {
            ["Task1","succeeded"] call SHK_Taskmaster_upd; 
            [] call SHK_addTask;
             };

  }; 


};

sorry to be a pest but this has got me stuffed for quite a while


Share this post


Link to post
Share on other sites

In your second block here:

if (!alive _POW) then {_POWgrp = _POWgrp - _POW;};

_POWgrp is initially a group. Then you substract an object from a group, as if it was an array. But it's not. So immediately after the first iteration of that loop, i'm not quite sure what _POWgrp has become, but it's definitely not what you want.

Now let's see things with a larger scope:

 {
        if ((_POW distance _obj) < _safeDistance) then {_safe = (_safe + 1)};
        if (!alive _POW) then {_POWgrp = _POWgrp - _POW;};
     } forEach units _POWgrp;

In a forEach loop, you refer to each value of the array with _x. Here it just performs the same operation 5 times, dealing with the same POW, which is the last one you created. Basically you can mess all you want with the 4 others, won't change zip.

Then, this:

 count units _POWgrp

(used twice),

will probably return some stupid stuff, being that _POWgrp is not a group anymore. For some reason it might return 0, and thus display your victory hint.

Note that even all that is not really logic and i have missed some stuff.

But you don't need to substract anything from a group : if you want to count the alive unitsin a group, go this way:

{alive _x} count units groupname

You'll probably have an easier time setting this up with two triggers.

Edited by BlackMamb

Share this post


Link to post
Share on other sites

what about this

//create POWS
//count alive POWs
if (isServer) then {

waituntil {!isnil "bis_fnc_init"};
_POWgrp = createGroup west;
_jail = getMarkerPos "jail";
[color="#FF0000"] _POWs_death_Limit = false;
_POWs_rescued = false;[/color]

  //create POWs 
     _POWman = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;  
  POW = _POWgrp createUnit [_POWman, _jail, [], 0, "FORM"];
     POW setcaptive true;
     POW setBehaviour "CARELESS";
     removeAllWeapons POW;
     POW call {[POW,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     POW setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;



  //create POWs 
     _POW1man = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom; 
  POW1 = _POWgrp createUnit [_POW1man, _jail, [], 0, "FORM"];
     POW1 setcaptive true;
     POW1 setBehaviour "CARELESS";
     removeAllWeapons POW1;
     POW1 call {[POW1,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     POW1 setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;		  


  //create POWs 
     _POW2man = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom; 
  POW2 = _POWgrp createUnit [_POW2man, _jail, [], 0, "FORM"];
     POW2 setcaptive true;
     POW2 setBehaviour "CARELESS";
     removeAllWeapons POW2;
     POW2 call {[POW2,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     POW2 setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;



  //create POWs 
     _POW3man = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;    
  _POW3 = _POWgrp createUnit [_POW3man, _jail, [], 0, "FORM"];
     _POW3 setcaptive true;
     _POW3 setBehaviour "CARELESS";
     removeAllWeapons POW3;
     _POW3 call {[POW3,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     _POW3 setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;



  //create POWs 
     _POW4man = ["US_Delta_Force_EP1","US_Delta_Force_AR_EP1","US_Delta_Force_SD_EP1"] call BIS_fnc_selectRandom;    
  POW4 = _POWgrp createUnit [_POW4man, _jail, [], 0, "FORM"];
     POW4 setcaptive true;
     POW4 setBehaviour "CARELESS";
     removeAllWeapons POW4;
     POW4 call {[POW4,20,3] execVM "missions\scripts\pow_positions.sqf"};	 
     POW4 setVehicleInit "this addAction ['Rescue Him!', 'missions\scripts\rescue.sqf'];";
     processInitCommands;









[color="#FF0000"]_grp = [POW,POW1,POW2,POW3,POW4];
_obj = POWSafe;
_safedistance = 20;
_safe = 0;



player sidechat format ["%1",_grp];

while {!_POWs_rescued && !_POWs_death_Limit} do {

        if (count units _grp == 3) exitWith (hint "Too many POWs died!") then  // if pow count falls to 3 or below then task failed
	   {
        //["Task1","failed"] call SHK_Taskmaster_upd;
        //[] call SHK_addTask;

          };
           if (count units _grp distance POW_safe < 20) exitWith (hint "all living POWs made it!") then  // if all alive pows are within 20mtrs from POW_safe then task succeeded
		  {
            //["Task1","succeeded"] call SHK_Taskmaster_upd; 
            //[] call SHK_addTask;
             };

};	[/color]	  


};  


---------- Post added at 12:53 ---------- Previous post was at 12:33 ----------

so now im getting an error in my rpt

Error in expression <death_Limit} do {

if ({alive _x} count units _grp == 3) then 
{
hint "Too many >
 Error position: <units _grp == 3) then 
{
hint "Too many >
 Error units: Type Array, expected Object,Group
File C:\Users\natho\Documents\ArmA 2 Other Profiles\=Mason=\missions\testtakitestttestestestestest.Takistan\missions\scripts\pow_spawn.sqf, line 87


on this code

if (count units _grp == 3) then 

//this is grp
_grp = [POW,POW1,POW2,POW3,POW4];



Share this post


Link to post
Share on other sites

Other way around. _grp is not a group but an array, so you need to use count and not count units.

The first for loop was fine, and i don't think you need to break it down in several blocks like you did.

I'd create a group _grpPOW with all five POWs.

Set one trigger:

Condition: {alive _x} count units _grpPOW =< 3

Statement: hint "Too many POWs died!"; POWs_death_Limit = true; publicVariable "POWs_death_Limit";

And another, on your safe position, radius 20:

Condition: {_x in thisList; alive _x} count units _grpPOW == {alive _x} count units _grpPOW

Statement: hint "all living POWs made it!"; POWs_rescued = true; publicVariable "POWs_rescued";

Those triggers would have to be created by script after the group has been spawned, or created in the editor in a way they don't trigger right at the beginning: either by having one of the POWs set in the editor, or by adding a condition relying on a public variable defined onmly after they've spawned.

The two public variables I have set up would be used to execute further code (basically calling Shuko' taskmaster on the server only).

Edited by BlackMamb

Share this post


Link to post
Share on other sites

ok so i got the check for tomany deaths working using

      if (count units _POWgrp <3) then 
	   {

	    hint "Too many POWs died!";
        ["Task1","failed"] call SHK_Taskmaster_upd;
        [] call SHK_addTask;
	   _POWs_death_Limit = true;

          };


but now i cant get the check to see all alive members of POWgrp within 20meters of POW_safe tent working

this is what i have got

	 if (_POWgrp distance POWSafe <20) then 
	   {
	     _POWs_rescued = true;
		 hint "all living POWs made it!";
         ["Task1","succeeded"] call SHK_Taskmaster_upd; 
         [] call SHK_addTask;
          };


Share this post


Link to post
Share on other sites

That's the same mistake as in the beginning, distance only works with single objects not groups or arrays. You need to use a loop with forEach

Share this post


Link to post
Share on other sites

Yup. And regarding your other issues, don't group them, actually. Just have them in an array as Wolf said.

So you'll be able to have them join your group without messing up the count.

Plus they won't try and regroup if you have them scattered across the map.

Share this post


Link to post
Share on other sites

Thanks for all the help guys i got going with all your help

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  

×