Jump to content
  • 0
WLF

out of range in arrays

Question

Hi all!

How can I check the out of range in arrays?

_a = [0,1];
for "_i" from 0 to 3 do {
	_c = _a select _i;
	if (_c ???) exitWith {systemChat "Out of range!";};
	systemChat format ["_a[%1] = %2",_i,_c];

};

 

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0
7 minutes ago, JasonTM said:

Modify your for loop so you don't go out of range.

for "_i" from 0 to (count _a - 1) do {

 

no, i need to do exactly that.

Share this post


Link to post
Share on other sites
  • 0

I don't understand what you are trying to do. If you run that for loop you will get a zero denominator error because you are trying to access an element that does not exist. You have 2 elements in the array and you are trying to run the loop 4 times (from 0 to 3).

Share this post


Link to post
Share on other sites
  • 0
1 hour ago, JasonTM said:

I don't understand what you are trying to do. If you run that for loop you will get a zero denominator error because you are trying to access an element that does not exist. You have 2 elements in the array and you are trying to run the loop 4 times (from 0 to 3).

I make an example of real script:

_donn_carsConvy		= [
	["HMMWV_M1151_M2_CZ_DES_EP1_DZE",3],
	["UAZ_Unarmed_UN_EP1",1],
	["ArmoredSUV_PMC_DZE",2],
	["Ural_UN_EP1",5],
	["LandRover_Special_CZ_EP1",0]
];

// Cars in each convoy (_refere to donn_carsConvy)
_convoyFormation	= [
	[0,1,2],
	[4,3],
	[2,4,2,0]
//	,[4,5,6]
];


for "_cs" from 1 to (count _convoyFormation) do {
	_convoy = _convoyFormation select (_cs-1);
	_qtd = count _convoy;
	_cars = [];
	{
		_this_car = _donn_carsConvy select _x;	// so what if the user makes a mistake and prints a non-existent car?
												// (uncomment last line in _convoyFormation).
												// I need to check this.
		_cars = _cars + [_this_car];
	} forEach _convoy;

	_donn_motor = [];
	for "_n" from 1 to _qtd do {
		_car = _cars select (_n - 1);
//		_motor = createVehicle [_car select 0, position player, [], 10,"NONE"];
		_motor = _car select 0;
		_donn_motor = _donn_motor + [_motor];
	};
	diag_log format ["Convoy #%1: %2",_cs,_donn_motor];
};

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Advertisement
  • Supporters
  • Discord

×
×
  • Create New...