Jump to content
Sign in to follow this  
wiggum2

isNil, empty array and empty group issue

Recommended Posts

I have aproblem with checking if a array isNil and/or if a group is empty/does not exist:

This is not the hole script, if a ; or }; is missiong its most likely a past© issue !

[color="Red"]_grptargets[/color] =[];
_grpToHunt = nil;

{if ((count units _x>0 and side _x==west) && ((leader _x) distance (leader _grphunter) <= (_areasize * _multi)) && (_ReadyToHunt == 0))  then {[color="Red"]_grptargets[/color]=[color="Red"]_grptargets[/color]+[_x]}} forEach allGroups;
sleep 1;

if (!(isNil "[color="Red"]_grptargets[/color]") && (_ReadyToHunt == 0)) then { 
[color="Blue"]_grpToHunt[/color] = [[color="Red"]_grptargets[/color], leader _grphunter] call BIS_fnc_nearestPosition;
sleep 1;
};

if !(isNil "[color="Blue"]_grpToHunt[/color]") then {
if ((([color="Magenta"]leader _grphunter[/color]) knowsAbout (leader [color="Blue"]_grpToHunt[/color]) >= 1.3) && ((leader _grphunter) distance (leader _grpToHunt) <= (_areasize * _multi)) && (_ReadyToHunt == 0) && ({alive _x} count units [color="Blue"]_grpToHunt[/color] > 0)) then {
hint "I have a problem";
};
};

If there is no west group inside the specific distance, _grptargets should be empty i think.

But my "isNil" check dont seems to work because my "isNil" check on _grpToHunt dont works also.

The script always reports:

ERROR: leader: Type array expectet object, group

But it should not get that far i think, anyway...i total confused by this.

I just want to skip the "if (((leader _grphunter) knowsAbout (leader _grpToHunt) >= 1.3)[...]" part if there is no west group found in the specific range.

This should be possible with checking if the group _grpToHunt exists.

If there is a west group inside the specific distance there is no error !

Can someone help me and tell me what i did wrong ?

Edited by Wiggum

Share this post


Link to post
Share on other sites

Tried:

if !(isNil _grptargets) then {
if !(isNil _grpToHunt) then {

Gives me:

ERROR isnil: Type Array expected String, code

Share this post


Link to post
Share on other sites

Well, as

_grptargets =[];

, it isn't nil (non existing) but an empty array.

And

_grpToHunt = nil;

is useless IMHO.

Share this post


Link to post
Share on other sites

As you are declaring _grptargets to be an array, and know that it exists, you can just check if it has anything:

if (count _grptargets > 0) then { do stuff };

Share this post


Link to post
Share on other sites

@ ProfTournesol

I did this because Squint gives errors withou this. ;)

@AZCoder

I tried "count units _grpToHunt == 0" because _grpToHunt should return a group, but this did not work.

What does _grpToHunt actually return ?

It must be a group i think.

Problem was maybe that you cant count units of a group that does not exist, but how would you check if the _grpToHunt does exist ?

Share this post


Link to post
Share on other sites

BIS_fnc_nearestPosition doesn't work with groups...it needs objects for the input....

Something like this works fine:-

_grpTargets = [s1,s2,s3];
_grpToHunt = [_grptargets, player] call BIS_fnc_nearestPosition;

hint format ["%1",_grpToHunt];

s1,s2 and s3 are individual soldiers! I've left your variable name (_grpToHunt) the same...but it needs changing since we are not dealing with a group.

EDIT: Added....

Here's a function to get the nearest groups of a certain side within a specific radius ....if it helps. It currently ignores groups in Air and Sea

vehicles.

Preprocess the file (in your init.sqf):-

groupsNear = compile preprocessFileLineNumbers "fn_findGroupsNear.sqf";

Call with something like this:-

_neargroups = [player,100,EAST] call groupsNear;

...and the actual function....

fn_findGroupsNear.sqf:-

private ["_obj","_rad","_sid","_pos","_list","_group","_i","_dude","_groups"];

_obj = _this select 0;
_rad = _this select 1;
_sid = _this select 2;

if (typename _obj == "OBJECT") then {_pos = getpos _obj} else {_pos = _obj};

_list = [];
_groups = [];

_list = nearestObjects [_pos,["Man","Car","Tank"],_rad];

for "_i" from 0 to ((count _list) - 1) do {
_dude = _list select _i;
_dude = leader group _dude;

if (_sid == side _dude) then {
	_group = group _dude;
	if (not(_group in _groups)) then {_groups set [(count _groups),_group]};
};
};

_groups

EDIT: Fixed the code above...I had somehow pasted it twice!!

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

Thanks for the script twirly, but that was never a problem.

BIS_fnc_nearestPosition works fine for me with a array of groups and gives me the name of the closest one.

Take a look here:

http://forums.bistudio.com/showthread.php?t=87194

Solved my problem with:

[color="Red"]if ((count _grptargets > 0)[/color] && (_ReadyToHunt == 0)) then { 
_grpToHunt = [_grptargets, leader _grphunter] call BIS_fnc_nearestPosition;
sleep 1;
};

// If _grphunter knows about _grpToHunt and is in range then stop patrol and get ready to hunt them
if [color="Red"]!(isNil "_grpToHunt")[/color] then {

I think "count _grptargets > 0" is the importent part, thanks for your help !

Share this post


Link to post
Share on other sites

To check nil: isNil "_variableName"

To check null group or object: isNull _variableName

To verify if something is array: !isNil "_variableName" && typeName _variableName == "ARRAY"

Edited by Sickboy

Share this post


Link to post
Share on other sites
To verify if something is array: !isNil "_variableName" && typeName _variableName == "ARRAY"

Wish it was that simple. That will only work if the variable is defined. SQF doesn't do short circuit evaluation so if the variable is undefined the right side will be nil and the logical-and will also be nil - you never get a false.

Share this post


Link to post
Share on other sites
if (!isNil "_variableName") then
{
if (typeName _variableName == "ARRAY") then

Share this post


Link to post
Share on other sites
Wish it was that simple. That will only work if the variable is defined. SQF doesn't do short circuit evaluation so if the variable is undefined the right side will be nil and the logical-and will also be nil - you never get a false.
I could swear it worked, but might be wrong, kju's the way to go then.

#define IS_ARRAY(VAR) if (isNil #VAR) then { false } else { typeName VAR == "ARRAY" }

if (IS_ARRAY(_myVar)) then

Share this post


Link to post
Share on other sites

Perhaps the CBA macros should work like this too - they don't check for nil-ness. Unless it would break stuff.

Edited by Muzzleflash

Share this post


Link to post
Share on other sites
Perhaps the CBA macros should work like this too. Unless it would break stuff.
Makes me wonder why they don't already.

what does diag_log [(typeName _inExistendVar) == "ARRAY"];

yield? (can't check for now).

Share this post


Link to post
Share on other sites
Makes me wonder why they don't already.

what does diag_log [(typeName _inExistendVar) == "ARRAY"];

yield? (can't check for now).

It yields: [bool]

hint str ((typeName _inExistendVar) == "ARRAY"); ->

does not hint anything.

On a related subject. Wondering if the EXPLODE_n macros should be completely expanded? Currently they redefine private for variables. Meaning EXPLODE_7 would private-ize the first argument 7 times, the second 6 times and so on for at total of 7+6+5+4+3+2+1 privatized variables. Don't know how much processing private statements require but for calling a function in a loop that uses PARAMS_n it could be significant?

Edited by Muzzleflash

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  

×