x


Infinate loop

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

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

    do
    {
       boulderY = Random.Range(2,gridHeight -1);
       boulderX = Random.Range(0,gridWidth);

       if(gridPositionType[boulderX,boulderY] == 0)
       {
         gridPositionType[boulderX,boulderY] = 3;
         boulderCount++;
       }
    }
    while (boulderCount < boulderAmount );///make 1 boulder
}
more ▼

asked Mar 29 '12 at 11:56 AM

markpollard gravatar image

markpollard
31 4 5 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

PS. It also might be interesting to know a little more about the 'gridPositionType', then we can give you a better answer ;)

-Pablo

more ▼

answered Mar 29 '12 at 01:15 PM

The Oddler gravatar image

The Oddler
253 27 36 45

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1360
x295

asked: Mar 29 '12 at 11:56 AM

Seen: 302 times

Last Updated: Mar 29 '12 at 01:15 PM