Jump to content

Recommended Posts

Hi, what am i doing wrong here with the selectRandom command? I'm trying to choose one random player and use it with a variable. It seems to skip the !(_player isEqualTo []) for some reason even if players are Incapacitated or not which will give a result of _player undefined variable later on.

private _player = selectRandom (allPlayers select {(_x distance2d _markerpos < 450) && {(lifestate _x != "Incapacitated")}});

if !(_player isEqualTo []) then {
something
};

 

Share this post


Link to post
Share on other sites

is _markerpos defined somewhere before that code? that could be one issue

 

another thing is you have the curly brackets at "{(lifestate _x != "Incapacitated")}" which makes no sense. you should remove those

  • Like 1

Share this post


Link to post
Share on other sites
12 minutes ago, gc8 said:

is _markerpos defined somewhere before that code? that could be one issue 

Yes, forgot to add it.

 

12 minutes ago, gc8 said:

another thing is you have the curly brackets at "{(lifestate _x != "Incapacitated")}" which makes no sense. you should remove those

Thanks for pointing it out, added curly brackets in my if () then {} because of "lazy evaluation". Ended up adding it in my select variables too.

_markerpos = getMarkerpos "Marker1";

While {uiSleep 5; true} do {

private _player = selectRandom (allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "Incapacitated")});

if !(_player isEqualTo []) then {
something
};
};

It still bypasses my !(_player isEqualTo []) even when Incapacitated. Problem only occurs when i'm trying to use selectRandom command in it.

Share this post


Link to post
Share on other sites

try

if(!(isnil "_player")) then 
{ 
something 
};

 

  • Like 2

Share this post


Link to post
Share on other sites

selectRandom [] returns nothing, so selectRandom [] isEqualto []  has no sense (but don't throw error). Use a simple count before selectRandom.

 

 

Share this post


Link to post
Share on other sites
3 hours ago, pierremgi said:

selectRandom [] returns nothing, so selectRandom [] isEqualto []  has no sense (but don't throw error). Use a simple count before selectRandom.

Hm, Error count: Type Object, expected Array.

private _player = count selectRandom (allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "DEAD-RESPAWN") && (lifestate _x != "Incapacitated")});

 

Share this post


Link to post
Share on other sites
5 hours ago, gc8 said:

try


if(!(isnil "_player")) then 
{ 
something 
};

 

This seems to work now.

_markerpos = getMarkerpos "Marker1";

While {uiSleep 5; true} do {

private _player = selectRandom (allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "Incapacitated")});

if (!(isNil _player)) then {
something
};
};

 

Share this post


Link to post
Share on other sites
9 hours ago, Robustcolor said:

Hm, Error count: Type Object, expected Array.


private _player = count selectRandom (allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "DEAD-RESPAWN") && (lifestate _x != "Incapacitated")});

 

Sorry I didn't precise that you need an entire line for that. I never guessed you could use it as is. The very first step to write any code is to comply with the syntax.
 

private ["_players","_player"];
While {true} do {
   uiSleep 5;
   _players = allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "Incapacitated")});
   if (count _players >0) then {
     _player = selectRandom _players;
     <your code with this unit>
   };
};

here !(_players isEqualTo []) in the condition, works also.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks  @gc8 and @pierremgi. It works now.

 

What is the reason for isEqualTo not working when using selectRandom inside this variable but isNil does? So i understand it for further scripting 🙂

private _player = selectRandom (allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "Incapacitated")});
if (!(isNil _player)) then {
};

 

and here isEqualTo is working.

 _players = allPlayers select {(_x distance2d _markerpos < 450) && (lifestate _x != "Incapacitated")});
if !(_players isEqualTo []) then {
_player = selectRandom _players;
};

 

Share this post


Link to post
Share on other sites

It's simple: you can't select at random an array with no element! That doesn't return anything at all. So, you can't compare it, even with an empty array []. Nothing is nothing. On the other hand, if you pay attention for a not-empty array, you can select at random its element(s), even if there is just one of them.

By chance isNil works without throwing error when you check for (isNil "_player") where _player is nothing (selectRandom []).

I don't know why you're writing isNil _player instead of isNil "_player"... You just add an extra error. syntax is not optional.

  • Like 2

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

×