Jump to content
Sign in to follow this  
TyrDaishi

undefined variable in expression

Recommended Posts

Hi,

 

I'm trying to write a script but can't solve this puzzle/problem:

 

1. At the start of the script I create a variable e.x. _target

2. I pick a random alive player, the script is working with _target beautifully

3. when all players died and they respawn the variable _target is undefined

I try to catch that information with something similar to this:

if (isNil "_target") then {
    _target = selectRandom playableUnits;
};

Yet the variable remains undefined and I can't figure out why. If anyone wants to look at complete code I can send them a link.

 

cheers,

Daishi

Share this post


Link to post
Share on other sites

Just skip the nil check and assign _target to someone new?  It's either gonna be nil or a dead unit, so you're going to want to assign it again anyway.

Share this post


Link to post
Share on other sites

hm yes assigning a new unit as long as units from blufor that are players are alive is working fine, i'm concerned only about the situation when all players are dead and noone respawned. That is an unlikley scenario yet i would like to cover the probability. And my problem is when I'm alone on the server, die and respawn the function should "repick" me as new target since im the only one on the server right? yet as soon as _target is undefined once it won't be defined again.

 

I'm using this function to assign new targets, which was working with two players on the server:

fn_SelectRandomBlue = {
_unit = objNull;  _unitsArray = []; _actualtarget = objNull;
{  if ((side _x == west ) and (isPlayer _x)) then {
_unitsArray = _unitsArray + [_x];  } ;
}forEach allunits; 

_unit = _unitsArray call BIS_fnc_selectRandom;
_actualtarget = _unit;
};//end function

and this is the if statement to check if assigning a new target is necessary:

	if (!alive _actualtarget || isNil "_actualtarget") then
	{
		[] call fn_SelectRandomBlue;
	};

Share this post


Link to post
Share on other sites

playableUnits only contains alive units.

So if all players are dead you're setting it nil, basically.

Why not use allPlayers instead?

 

Cheers

Share this post


Link to post
Share on other sites

Isn't isNil the wrong check anyway? That'd imply that _target as a variable is non-existant but since it's previously defined, the if statement returns false. I always mix up isNull and isNil though so might just be me. Either way, if target is a soldier, you might as well use !alive if you want to do a check. 

Share this post


Link to post
Share on other sites

Isn't isNil the wrong check anyway? That'd imply that _target as a variable is non-existant but since it's previously defined, the if statement returns false. I always mix up isNull and isNil though so might just be me. Either way, if target is a soldier, you might as well use !alive if you want to do a check. 

the error explicitly stated undefined variable so I checked for isNil, which includes a contentcheck as well if I'm not mistaken?

Share this post


Link to post
Share on other sites

Using allplayers did the trick! i removed the unnecessary function (I understand now why it was wrong) and check only if the actual target is dead. Thanks for the help guys. I learned a lot from this regardless.

  • Like 1

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  

×