Jump to content
  • 0

whats wrong with this array and foreach/count ?


StiflersM0M

Question

hey :)

 

Just getting some trouble and cant find why the hack this code below does not wotk.... ->>

_safes = nearestObjects [player, ["VaultStorageLocked"], 12000];
_safearray = [];
{
if ((_x getVariable ["CharacterID","0"]) == "0000") then { //added under it some debug code via diag_log seems like he is not able to get the variable.... but it also do not throws out "0"
_safearray set [count _safearray, _x];
};
} forEach _safes; //Also tried count, makes no difference
//if you diag_log the safearray now it throws out a null

maybe any of you could help me with it...... and yes, there are safes with the characterID "0000"

 

Thank you for your help

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
private ["_safes","_safearray","_vault"]; // remeber to scope it
_safes = (allMissionObjects "VaultStorageLocked"); // checks all objects named VaultStorageLocked thats loaded in to the current mission
_safearray = [];  // wut

_vault = _safes getVariable "CharacterID"; // gets the variable of the objects

if (_vault = "0000" ) then { // checks the variable
        _safearray set[count _safearray, _safes ];  // sets the index of _safes to _safearray for whatever reason
	//diag_log format ["SAFE TEST: SafeArray contains %1",_safeArray]; // not tested..
};

Will grab all safes on the map, check their characterID and if its 0000 or 0 (doesnt really matter if it says 0000 or 0) then start the next part.

 

Edit: You might want to add a counter to check all of the vaults. Otherwise script wont really work. I suggest you check out for loops.

 

 

but this will not work

_safearray set[count _safearray, _x]; 

Youre counting the array and stuff, ye that works but why do you even want to append _x to the array? 

 

Also _x doesnt work on objects.

Link to comment
Share on other sites

  • 0

Why 

 

_safes = (allMissionObjects "VaultStorageLocked");
_safearray = []; 

_vault = _safes getVariable "CharacterID";

if (_vault = "0000" ) then {
        _safearray set[count _safearray, _x]; 
		
};

Will grab all safes on the map, check their characterID and if its 0000 or 0 (doesnt really matter if it says 0000 or 0) then start the next part.

 

but this will not work

_safearray set[count _safearray, _x]; 

Youre counting the array and stuff, ye that works but why do you even want to append _x to the array? 

 

Also _x doesnt work on objects.

 

Wow completly forgot about the allmissionobjects :D

 

But also, why should i cant add a object to an array ?  nearestobjects returning also an array with the objects ? 

 

And for sure it does matter if it says 0 or 0000, 0 will only be returned if the variable is not set on the object.

 

and also, your code do not work either, even if i fix the _vault variable.

 

P.S the problem is in the if condition, but i dont know what the problem should be.

Link to comment
Share on other sites

  • 0

How i said, it seems like he get the variable but also do not get the variable, if i make it like you said it also wont work. (added a diag_log line inside the condition)

What exacly does not work, your diag_log line or do you get an .rpt error?

Cause there is definitly nothing wrong with this

_safes = nearestObjects [player,["VaultStorageLocked"],12000];
_safearray = [];
{
	if ((_x getVariable ["CharacterID","0"]) == "0") then {
		diag_log str(_x); // Dont know what you want to log..
		_safearray set [count _safearray,_x];
	};
} forEach _safes; 

Link to comment
Share on other sites

  • 0

You can add an object to an array but there is already an array that has the object, why would you need to have a second one?

 

Sure you can use nearestobjects, i just changed it becuase you were checking a radius of 1200 which is quite large and is not very efficent. I'd run the script server side then instead of client side as well. 

 

 

What exacly does not work, your diag_log line or do you get an .rpt error?
Cause there is definitly nothing wrong with this

_safes = nearestObjects [player,["VaultStorageLocked"],12000];
_safearray = [];
{
	if ((_x getVariable ["CharacterID","0"]) == "0") then {
		diag_log str(_x); // Dont know what you want to log..
		_safearray set [count _safearray,_x];
	};
} forEach _safes; 

 

Are you really sure this works because I'm fairly sure that _x doesnt return objects. 

 

 

You're launching the code I posted server side? Also Yes it works. 0000 or 0 depending on what it's set to in your database. 

 

 

You need to count the safes as I said otherwise it wont work. Try it like this:

_safes = (allMissionObjects "VaultStorageLocked");
_count = (count _safes);
_safearray = []; 

for "_i" from 0 to _count do {
	
	_selectVault = _safes select _i;
	
	_vault = _selectVault getVariable "CharacterID";
	
	if (_vault = "0000" ) then {
		_safearray set[count _safearray, _selectVault]; 	
	};
};

Link to comment
Share on other sites

  • 0

You can add an object to an array but there is already an array that has the object, why would you need to have a second one?

 

Sure you can use nearestobjects, i just changed it becuase you were checking a radius of 1200 which is quite large and is not very efficent. I'd run the script server side then instead of client side as well. 

 

 

 

Are you really sure this works because I'm fairly sure that _x doesnt return objects. 

_safes = (allMissionObjects "VaultStorageLocked");
_safearray = [];
_counter = 0;
_count = count _safes;
{
//if ((_x getVariable ["CharacterID","0"]) == "0000") then {
_safearray set [count _safearray, _x];
//};
} forEach _safes;
diag_log format["safearray = %1", _safearray];

"safearray = [2c8dc800# 1059964: safe.p3d REMOTE,2e352b00# 1060194: safe.p3d REMOTE,2d7c3900# 1060840: safe.p3d REMOTE,2cb76b00# 1059916: safe.p3d REMOTE,2ce24800# 1059879: safe.p3d REMOTE,2d6db900# 1059540: safe.p3d REMOTE,2d6e4100# 1059539: safe.p3d REMOTE,2d15f900# 1059779: safe.p3d REMOTE,2ce25600# 1059877: safe.p3d REMOTE,2dc29d00# 1060663: safe.p3d REMOTE,2dc29600# 1060664: safe.p3d REMOTE,2de45d00# 1060544: safe.p3d REMOTE,2de3f900# 1060558: safe.p3d REMOTE,2cec4100# 1059829: safe.p3d REMOTE,2e4bf200# 1060148: safe.p3d REMOTE,2dabf900# 1060732: safe.p3d REMOTE,2de7c100# 1060520: safe.p3d REMOTE,2de78800# 1060526: safe.p3d REMOTE,2de78100# 1060527: safe.p3d REMOTE,2de7c800# 1060519: safe.p3d REMOTE,2e684f00# 1060110: safe.p3d REMOTE,2b9e9d00# 1059425: safe.p3d REMOTE,2b9e9600# 1059426: safe.p3d REMOTE,2d325600# 1059657: safe.p3d REMOTE,2e6c4f00# 1060044: safe.p3d REMOTE,2bab9600# 1059292: safe.p3d REMOTE,2d274f00# 1059717: safe.p3d REMOTE,2e19dd00# 1060396: safe.p3d REMOTE,2d252400# 1059726: safe.p3d R

 

Maybe i got some few informations missing, i did it like this:

_safes = (allMissionObjects "VaultStorageLocked");
_safearray = [];
_counter = 0;
_count = count _safes;
{
diag_log format["object before condition"];
if ((_x getVariable ["CharacterID","0"]) == "0000") then {
_safearray set [count _safearray, _x];
diag_log format["object with match the condition"];
};
} forEach _safes;

and it throws out about 150 "object before condition"

But not a single one "object with match the condition"

 

So i thought something with the condition is wrong,

 

and i got 29 safes which got the characterid 0000

Link to comment
Share on other sites

  • 0

 

You can add an object to an array but there is already an array that has the object, why would you need to have a second one?

 

Sure you can use nearestobjects, i just changed it becuase you were checking a radius of 1200 which is quite large and is not very efficent. I'd run the script server side then instead of client side as well. 

 

 

 

Are you really sure this works because I'm fairly sure that _x doesnt return objects. 

 

 

You're launching the code I posted server side? Also Yes it works. 0000 or 0 depending on what it's set to in your database. 

 

 

You need to count the safes as I said otherwise it wont work. Try it like this:

_safes = (allMissionObjects "VaultStorageLocked");
_count = (count _safes);
_safearray = []; 

for "_i" from 0 to _count do {
	
	_selectVault = _safes select _i;
	
	_vault = _selectVault getVariable "CharacterID";
	
	if (_vault = "0000" ) then {
		_safearray set[count _safearray, _selectVault]; 	
	};
};

even if i set the characterid to 0 in the database, the server monitor will set the characterid to 0000, so in my code "0" would only be returned if the variable is not set.

 

And it was my failure, i was on my test server and looked for the safes with 0000 in the real database and not in the test database, sry for that, but thank you anyway for the allmissionobjects :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...