Jump to content
Sign in to follow this  
barbolani

How to check variables inside Array

Recommended Posts

Hello friends,

I noticed in my mission an error, which cause is still unknown. But, sometimes makes an array have invalid content.

Let's say I have an array like: ingredients = ["Potatoes","Tomatoes"] and I commit the mistake to introduce invalid data, like ingredients = ingredients + [Apples]

When I check the array it will be "Potatoes","Tomatoes",any.

How I detect this and automatically clean the array? I tried things like:

{

If (_x == nil) then ingredients = ingredients - [_x];

} forEach ingredients;

Any help?

Share this post


Link to post
Share on other sites

Something along the lines of ..

{
if (isnil "_x") then {
	ingredients deleteAt _forEachIndex;
};
}forEach ingredients

You can not say == as nil is nothing so can not be compared. You have to ask whether _x is nothing.

Rather than cleaning your array of erroneous data you would be better having a check where you are adding the data in the first place by making sure it is not nil before hand.

Edited by Larrow

Share this post


Link to post
Share on other sites

You almost got it. Because [Apples] is an array with a global variable namend Apples and if you didn't define it, it's null hence "any" (for "anything"). What you want is add ["Apples"] which is an array with a string.

So it's supposed to be:

["Potatoes","Tomatoes"] + ["Apples"]

Though if you want to add only one element, the most efficient way would be:

["Potatoes","Tomatoes"] pushBack "Apples"

Share this post


Link to post
Share on other sites

Thanks friends,

But no luck. I have my array with an any when I hint format it. I have a conditional with Larrows lines (tried also _x instead of "_x" and it seems everything isNil) and it does not detect the any.

The array modification has something as stupid as:

when FIA conquers the marker, then: aafmarkers = aafmarkers - [marker]; fiamarkers = fiamarkers + [marker];

Works most of the times, but sometimes I find aafmarkers with an any, and my spawn/despawn scripts go to hell (but even if I solve this, the spawning can be messing up with something else)

@Johnny: the mistake is wanted just for example purposes. Array performance is not a problem (yet)

Share this post


Link to post
Share on other sites

Try TypeName

Something like

---------------------------

ingredients = ["Potatoes","Tomatoes"];

ingredients = ingredients + [Apples]; // should work give or take any undefined variable error for Apples.

{

if (( typeName _x) == "STRING") then {

ingredients deleteAt _forEachIndex;

};

}foreach ingredients;

-------------------

Now if the Global variable "Apples" contained a string then it would still be part of the array but if it contained anything else or was undefined then it should be removed.

Share this post


Link to post
Share on other sites
Try TypeName

Something like

---------------------------

ingredients = ["Potatoes","Tomatoes"];

ingredients pushBack Apples; // should work give or take any undefined variable error for Apples.

{

if (( typeName _x) == "STRING") then {

ingredients deleteAt _forEachIndex;

};

}foreach ingredients;

-------------------

Now if the Global variable "Apples" contained a string then it would still be part of the array but if it contained anything else or was undefined then it should be removed.

Actually if the global Apples was a string it would be deleted from the array along with Potatoes and Tomatoes leaving you with an empty array, at least how you current have it setup. An if Apples was type "any" it would leave you with an array with just "any" in it ( [any] ).

I think your going for more of:

ingredients = ["Potatoes","Tomatoes"];
ingredients pushBack Apples; // should work give or take any undefined variable error for Apples.

{
if not (( typeName _x) isEqualTo "STRING") then {
ingredients deleteAt _forEachIndex;
};
}foreach ingredients;

Edited by JShock

Share this post


Link to post
Share on other sites

Typename will not help, as typename of nil is nil so can not be compared (== , isEqualTo) against anything. The best you will get is isNil {typename _x} which for all intense and purpose is no different to what i previously posted.

barbolani - If my snippet is not working for you then you really need to go and fix what ever is causing the error in the first place rather than trying to fudge a clean array.

But no luck. I have my array with an any when I hint format it. I have a conditional with Larrows lines (tried also _x instead of "_x" and it seems everything isNil) and it does not detect the any.

I dont quite no what to suggest if it is not working, as you can test your example of ingredients with a nil value in the array from the debugConsole and see that it catches and deletes all nil values present in the array.

Maybe post your script up here and one of us will try to take a look at it when we can.

Share this post


Link to post
Share on other sites

Thank you very much Larrow, but giving you the set of scripts is something which will need lots of time from. It's a very complex mission whith spawn/despawn system based on players position, markers position and which side owns the marker via ARRAY.

An that interacts with the conquering script (change the marker name from array A to array B) so in the next check will spawn units from the other side.

Anyway I've tested more and the distance check script crashes with clean arrays, so the error must be somewhere else.

Share this post


Link to post
Share on other sites

Hii barbolani,

I'd debug it the old fashioned way and log everything to do with that array to the rpt.

You'll soon spot the nil entries and can narrow it down from there.

As mentioned above, removing them from the array is a band-aid to the problem imo.

Good luck :)

Share this post


Link to post
Share on other sites

The rpt says there's an error on this script, sometimes... Sometimes script stops with no error, and even worse: the final hint does not appear.

Ok, maybe you see where I'm not seeing.

I have an external script which checks this one with a scriptdone and sometimes it fires, sometimes not.

I assign some local variables to global variables in case the global ones change in the middle of the foreach, nothing gets messed up.

Also some external arrays like ciudades which mean the markes is assigned to a certain type of zone so it spawns/despawns in some way.

Here's the code:

if (!isServer) exitWith{};

cercanos = [];

debugperf = false;
debugdist = false;

private ["_tiempo"];

if (debugperf) then {_tiempo = 0;};

while {true} do {
if (debugperf) then {hint format ["Tiempo transcurrido: %1 para %2 marcadores", time - _tiempo, count marcadores]; _tiempo = time;};
_marcadores = marcadores;
_mrkAAF = mrkAAF;
_mrkFIA = mrkFIA;

sleep 0.2;
{
_marcador = _x;
_posicionMRK = getMarkerPos (_marcador);

if (_marcador in _mrkAAF) then
{
if (({(_x distance _posicionMRK < distanciaSPWN)} count amigos > 0) and (not (_marcador in cercanos))) then
	{
	if (debugdist) then {hint format ["%1 está cerca, lo movemos a cercanos", _marcador]; sleep 2};
	cercanos = cercanos + [_marcador];
	if (_marcador in colinas) then {[_marcador] spawn createAAFcolinas};
	if (_marcador in ciudades) then {[_marcador] spawn createCIV; [_marcador] spawn createAAFciudades};
	if (_marcador in power) then {[_marcador] spawn createAAFpower};
	if (_marcador in bases) then {[_marcador] spawn createAAFbases};
	if (_marcador in controles) then {[_marcador] spawn createAAFcontroles};
	if (_marcador in aeropuertos) then {[_marcador] spawn createAAFaerop};
	if ((_marcador in recursos) or (_marcador in fabricas)) then {[_marcador] spawn createAAFrecursos};
	if ((_marcador in puestos) or (_marcador in puertos)) then {[_marcador] spawn createAAFpuestos};
	};

if (({_x distance _posicionMRK < distanciaSPWN} count amigos == 0) and (_marcador in cercanos)) then

	{
	if (debugdist) then {hint format ["Nos hemos ido de %1, lo movemos a lejanos", _marcador]; sleep 2};
	cercanos = cercanos - [_marcador];
	};
};

if (_marcador in _mrkFIA) then
{
if (({_x distance _posicionMRK < distanciaSPWN} count enemigos > 0) or (({(isPlayer _x) and (_x distance _posicionMRK < distanciaSPWN)} count amigos > 0) and (not(_marcador in cercanos)))) then
	{
	if (debugdist) then {hint format ["%1 está cerca, lo movemos a cercanos", _marcador]; sleep 2};
	cercanos = cercanos + [_marcador];
	if (_marcador in ciudades) then {[_marcador] spawn createCIV;[_marcador] spawn createAAFciudades};
	if ((_marcador in recursos) or (_marcador in fabricas)) then {[_marcador] spawn createFIArecursos};
	if (_marcador in power) then {[_marcador] spawn createFIApower};
	if (_marcador in aeropuertos) then {[_marcador] spawn createNATOaerop};
	};

if ((({_x distance _posicionMRK < distanciaSPWN} count enemigos == 0) and ({(isPlayer _x) and (_x distance _posicionMRK < distanciaSPWN)} count amigos == 0)) and (_marcador in cercanos)) then
	{
	if (debugdist) then {hint format ["Nos hemos ido de %1, nuestra distancia es de %2 lo movemos a lejanos", _marcador, player distance _posicionMRK]; sleep 2};
	cercanos = cercanos - [_marcador];
	};
};
} forEach _marcadores;
};

hint "Distance check bugged!!!!!!!!!!!!!!!!!!!!!!!!!!!"

Thank you for your time!!

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  

×