Infinate loop

Hi any way of generating this better ? i sometimes get huge loops.

void BuildBoulders()
int boulderX;
int boulderY;
int boulderCount = 0;

		boulderY = Random.Range(2,gridHeight -1);
		boulderX = Random.Range(0,gridWidth);
		if(gridPositionType[boulderX,boulderY] == 0)
			gridPositionType[boulderX,boulderY] = 3;
	while (boulderCount < boulderAmount );///make 1 boulder

The problem you are probably having is that you randomly pick a ‘gridPositionType’, so it’s possible that you pick the same one multiple times, are just unlucky and accidentally pick a lot of type 0. It is also possible that if there aren’t enough gridPositionTypes of 0 that it’ll continue looking for a spot indefinitely.
I’m not sure how big your gridPositionType array is, though you could try two thing:

  1. Itterate over your gridPositionType and then just look for places that are 0. Instead of randomly choosing spots. Though this will remove your randomness.

  2. First iterate over the gridPositionType array, and select all the position that have type 0, then select a bunch of positions from this. This will be slower, though keeps your randomness and will make sure there are no never-ending loops.

Hope it helps :wink:

PS. It also might be interesting to know a little more about the ‘gridPositionType’, then we can give you a better answer :wink: