Jump to content
  • 0

out of range in arrays


WLF

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];

};

 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 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).

Link to comment
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];
};

 

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...