Jump to content
sabot10.5mm

help with returning a value from script

Recommended Posts

_weaps = nearestObjects [(getPos player),["StaticWeapon", "StaticATWeapon","StaticCannon"],50]; 
_crew = fullCrew [_weaps select 0, "gunner", true];  
_crew apply {if (isNull (_x select 0)) exitWith {true}; 
   if !(alive (_x select 0)) exitwith {true};
   false; 
   };

I made this with the intention to find static weapons. it works but I was wondering, when returning false it returns false as an array like [false]. im sure I am missing something here, but why is it returning an array with false in it? also 1 side note. why does fullcrew return dead crew members?

Share this post


Link to post
Share on other sites

The command apply takes an array and applies the given code to the element, so when you exitWith true or just exit with false the resulting array will just be [false, true, false, ect] (runs for each member of the crew). The reason it is returning this array is because it is the last line of code in your function, also it does not automatically override the given array unless you assign it again such as _crew = _crew apply {true}.

Share this post


Link to post
Share on other sites

1.

6 hours ago, sabot10.5mm said:

_crew apply {if (isNull (_x select 0)) exitWith {true}; if !(alive (_x select 0)) exitwith {true}; false; };

->

_crew apply {isNull (_x select 0) || !(alive (_x select 0))};

But alive check on nullObject returns false. So you don't need to check isNull.

_crew apply {!(alive (_x select 0))};

2.

 

I have no idea what you are trying to do, as you didn't say that and the code is quite confusing. So I'm just gonna guess and assume you want to check if any of the gunner crew in the vehicle is alive.

 

private _hasLivingGunner = _crew findIf {alive (_x select 0)} != -1

 

Share this post


Link to post
Share on other sites

Since you are checking gunner in a static weapon it is enough to use gunner command 

 

isNull gunner _veh - no gunner

!alive gunner _veh - no gunner or dead gunner

 

since you are checking multiple weapons it is unclear if you want results in an array or a single result for the 1st found weapon only.

  • Thanks 1

Share this post


Link to post
Share on other sites
4 hours ago, killzone_kid said:

 

 

since you are checking multiple weapons it is unclear if you want results in an array or a single result for the 1st found weapon only.

When i wrote  that, i didnt know if i wanted to return an array of all empty or just one. Sorry for confusion. To answer that i just want one returned.

5 mins after writing the thread, i did away with apply. 

Spoiler

params ["_staticWp"];

_crew = fullCrew [_staticWp, "gunner", true];

if (isNull ((_crew#0)#0)) exitwith {true};

if !(alive ((_crew#0)#0)) exitwith {true}; 

false;

 

!alive gunner _staticWp

 Easier to read. There are many different ways of writing code. then there are better ways

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

×