Jump to content
Sign in to follow this  
luckyhendrix

find variable in an array

Recommended Posts

Hello,

I've searched a lot but i haven't found if it is possible to check if a variable is contained in an array . exemple:

myarray1 = [man1,man2,man3]

myarray2 = [woman1,woman2,woman3]

if( player == myarray1)exitwith{hint " you're a man !"};

if( player == myarray2)exitwith{hint " you're a woman !"};

Also how can I have an array that contains all the aerial vehicle of a mission ?

Share this post


Link to post
Share on other sites

if( player in myarray1)exitwith{hint " you're a man !"};
if( player in myarray2)exitwith{hint " you're a woman !"};

Also how can I have an array that contains all the aerial vehicle of a mission ?

Slightly more difficult. You could make a trigger that covers the entire island, set activation to ANYBODY present, and use this on act:

airVehs = []; {if(_X isKindOf "Air")then{airVehs = airVehs + [_X]}} forEach thislist

Share this post


Link to post
Share on other sites

gosh it was so simple , thx.

I'll try with a trigger.

---------- Post added at 09:20 PM ---------- Previous post was at 07:34 PM ----------

I've got a hard time for having this array , here's my vehiclelist.sqf:

AirVehicles = [];

trig = createTrigger ["EmptyDetector",getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition")];

trig setTriggerType "NONE";

trig setTriggerActivation ["ANY", "PRESENT", true];

trig setTriggerArea [30000, 30000, 0, false ];

trig setTriggerStatements ["this", "", ""];

_mylist = [];

while{true}do{

_mylist = +(list _trig);

{if(_X isKindOf "Air")then{_AirVehicles = _AirVehicles + [_X]}} forEach _mylist;

player sidechat format["airveh: %1",_AirVehicles];//debug

player sidechat format["list: %1",(list_trig)];//debug

player sidechat format["mylist: %1",_mylist];//debug

sleep 5;

};

the errors tell me that foreach should receive an array and not a number. But _mylist is an array so what ?

Share this post


Link to post
Share on other sites

It's working better, but still not working ^^.

I've corrected the mistake.But debug message send me airvehicle = ANY, and _mylist = <NULL>.

Then i've got an .rpt at the second loop.

Share this post


Link to post
Share on other sites

No offense, but your formatting is just horrifying :D

Rather than using a trigger use the new function that returns an array of all vehicles in the mission - allUnits:

while {true} do
{
_AirVehicles = []; //We need to reset the array each time we start detecting again

{
	if (_X isKindOf "Air") then
	{
		_AirVehicles = _AirVehicles + [_X]
	};
} forEach allUnits;

player sidechat format["airveh: %1",_AirVehicles];//debug
sleep 5;
};

Share this post


Link to post
Share on other sites

Yup it's easier without a trigger but it's still not working , returns me an empty array each time. (and i've checked ther's plenty of aérial vehicle in my mission)

Share this post


Link to post
Share on other sites
AirVehicles = [];

Here it is global... but here:

{if(_X isKindOf "Air")then{_AirVehicles = _AirVehicles + [_X]}} forEach _mylist;

It is local. Gotta pick one. ;)

Also, I don't see why you're adding the trigger list to _mylist, why not just redefined _mylist = list trig each time?

while{true}do{
_mylist = list trig;
{if(_X isKindOf "Air")then{_AirVehicles = _AirVehicles + [_X]}} forEach _mylist;

Share this post


Link to post
Share on other sites

allright I've corrected those things.

_mylist works correctly returning me all objects but Airvehicles is always = ANY ??

And with deadfast method it's always Airvehicles = []

It's weird I don't see what we did wrong.

Share this post


Link to post
Share on other sites
No offense, but your formatting is just horrifying :D

Rather than using a trigger use the new function that returns an array of all vehicles in the mission - allUnits:

I looked at allUnits in the comref and got the impression that it only returns units (men), not vehicles. If it does return vehicles then by all means use it. Also deadfast is right you should reinitialize the AirVehicles array for each pass through the loop.

Share this post


Link to post
Share on other sites
Not valid snytax.

_mylist1 = +(list _trig);

This is a valid syntax.

The difference to _mylist2 = list _trig is that it will make a copy of the array while _mylist2 = list _trig is only a reference to an array. Means _mylist1 will point to another array than _mylist2 does.

Another tip...

Instead of adding elements to an array with the following syntax

_myarray = _myarray + [_an_element];

use BIS_fnc_arrayPush:

[_myarray , _an_element] call BIS_fnc_arrayPush;

The performance of BIS_fnc_arrayPush is much better than when you use the old syntax.

Xeno

Share this post


Link to post
Share on other sites
_mylist1 = +(list _trig);

This is a valid syntax.

The difference to _mylist2 = list _trig is that it will make a copy of the array while _mylist2 = list _trig is only a reference to an array. Means _mylist1 will point to another array than _mylist2 does.

Another tip...

Instead of adding elements to an array with the following syntax

_myarray = _myarray + [_an_element];

use BIS_fnc_arrayPush:

[_myarray , _an_element] call BIS_fnc_arrayPush;

The performance of BIS_fnc_arrayPush is much better than when you use the old syntax.

Xeno

Well looking at arrayPush, all it does is sort of like this:

_myarray set [count _myarray, _an_element];

Might as well, just type it in yourself, no?

Share this post


Link to post
Share on other sites

_myarray set [count _myarray, _an_element];

Might as well, just type it in yourself, no?

That's exactly the same, correct.

Xeno

Share this post


Link to post
Share on other sites

It is finaly working with this

_trig = createTrigger ["EmptyDetector",getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition")];

_trig setTriggerType "NONE";

_trig setTriggerActivation ["ANY", "PRESENT", true];

_trig setTriggerArea [30000, 30000, 0, false ];

_trig setTriggerStatements ["this", "", ""];

while{true}do{

AirVehicles = [];

_mylist = [];

_mylist = list _trig;

{if(_X isKindOf "Air")then{AirVehicles = AirVehicles + [_X]}} forEach _mylist;

player sidechat format["airveh: %1",AirVehicles];//debug

sleep 5;

};

All Units Only does count mens

Edited by luckyhendrix

Share this post


Link to post
Share on other sites

_trig = createTrigger ["EmptyDetector",getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition")];
_trig setTriggerType "NONE";
_trig setTriggerActivation ["ANY", "PRESENT", true];
_trig setTriggerArea [30000, 30000, 0, false ];
_trig setTriggerStatements ["this", "", ""];

while{true}do{
AirVehicles = [];
[s]_mylist = [];[/s]
_mylist = list _trig;
{if(_X isKindOf "Air")then{AirVehicles = AirVehicles + [_X]}} forEach _mylist;
player sidechat format["airveh: %1",AirVehicles];//debug
sleep 5;
}; 

That line's not necessary. Glad it works though.

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  

×