Jump to content

Recommended Posts

Hello. I have a piece of code here that needs some polishing.
some help to understand: _guard is in the middle of a city, _taskitems is not important right now. The for loop should go through all the houses in the area. This piece of code works 80% of the time without any problems. 20% of the time it gives me an error: "Undefined variable in the expression: _pos".Why does it work more often than not? I don't see my own mistake here.

_houses = nearestObjects [_guard, ["house"], 400];
	_crateSpawned = 0;
	_crateHouse = 0;
	_taskItems = [];
	_pos = 0;
	_defendergroup = createGroup civilian;

	for "_i" from 0 to count _houses -1 do {
		_item = _houses select _i;
		_chance = floor (random 5);

		if( (_chance == 1 && _crateSpawned == 0) || _houses select (count _houses -1) == _item) then
		{
			_positions = [_item] call BIS_fnc_buildingPositions; 
			if(count _positions >3 || _houses select (count _houses -1) == _item)
			then
			{
				_pos = selectrandom _positions;
				_crate = createVehicle ["B_CargoNet_01_ammo_F",_pos, [], 0, "NONE"];
				_crateSpawned = 1;
				_taskItems pushBack _crate;
				_crateHouse = _item;
				sleep 0.5;
			};
		};
	};

 

Share this post


Link to post
Share on other sites

Some buildings do not have any buildingPositions, so selectRandom won't work.

You'll just have to include a check for that.

  • Like 1

Share this post


Link to post
Share on other sites

OMG, Thanks a lot, holy arma, how do I do that?

Share this post


Link to post
Share on other sites

You already do it but for some reason it is paired with an OR condition that apparently checks for the last house in the list. (not sure why)

 

So maybe just change:

 

if(count _positions >3 || _houses select (count _houses -1) == _item)

 

to

 

if(count _positions >3) 

 

Share this post


Link to post
Share on other sites

because I want the crate to spawn, and if it doesn't spawn, "because of _chance not being 1", then it won't spawn at all :) but thanks, I figured I can just do this :
 

if (count _positions != 0 ) then {
				if(count _positions >2 || _houses select (count _houses -1) == _item)
				then
				{
					_pos = selectrandom _positions;
					_crate = createVehicle ["B_CargoNet_01_ammo_F",_pos, [], 0, "NONE"];
					_crateSpawned = 1;
					_taskItems pushBack _crate;
					_crateHouse = _item;
					sleep 0.5;
				};
			};

 

Share this post


Link to post
Share on other sites

No need to loop through all the houses because as soon as you have spawned a crate ( _crateSpawned == 1 ) then you are spawning no more. Other than if its the last house, which I presume is a logical error from your description..

3 hours ago, JeyR said:

because I want the crate to spawn, and if it doesn't spawn, "because of _chance not being 1", then it won't spawn at all

where it should have been..

if( _crateSpawned == 0 && ( _chance == 1 || _houses select (count _houses -1) == _item ) ) then

If no crate has been spawned AND either chance is 1 OR its the last house, then spawn a crate.

 

Instead of the loop just choose a house that has more than 2 positions (as per your last code snippet) and spawn a crate.

//select houses within 400m that have more than 2 house positions
_houses = nearestObjects [_guard, ["house"], 400] select { count ( _x buildingPos -1 ) > 2 };
//Make sure we have atleast one valid house
if ( count _houses > 0 ) then {
	//Select random house
	_crateHouse = selectRandom _houses;
	//Select random house position
	_pos = selectRandom ( _crateHouse buildingPos -1 );
	//Spawn crate at house position
	_crate = createVehicle ["B_CargoNet_01_ammo_F",_pos, [], 0, "CAN_COLLIDE"];
};

 

  • Like 2

Share this post


Link to post
Share on other sites

I see. I haven't thought of that. I was just doing the very first idea coming to my mind, but that one is better.

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

×