|
I'm working with an array of arrays to fill a grid (with actual size CurrentGridSize) with objects. I use randoms to determine the position the objects should take in the array and thus on the grid. The current expected behavior is that there will be around (CurrentGridSize x 4) objects in the array (barring doubles). However, the actual behavior is: the amount of objects is always exactly (CurrentGridSize). And I just can't figure out why that is. Even if I execute the loop 1000 times, the behavior stays the same. Can someone help me out and point out where the problem might be? Much thanks to whoever can solve this mystery!
EDIT I've discovered where the problem might be: if I leave out the if() statement to check if the element is zero, it does work correctly. But now I still don't know why this is the case...
(comments are locked)
|
|
the problem is when your new random number is a full array element, you should subtract k by one to run another iteration to find an empty element. 1/4 of random numbers are full elements in your current code so the object will not be instantiated. also you can define a label to go back to it with goto statement when the array element is not equal to zero. Sorry, that's not it either. The point of the CurrentGridSize is to not exceed that size! It's an array of arrays: the first determines horizontal postion, the second vertical. That being said, there are actually CurrentGridSize available positions in the array. if I let the random exceed this by saying (CurrentGridSize*4) using the randoms, that still doesn't change the weird behavior and it defeats the whole point of having a set size for the grid.
Jan 24 '10 at 03:43 PM
era
i edited the question. i think i found the answer. the problem is you skip the iteration that could not instantiate a new object in an empty element.
Jan 24 '10 at 06:40 PM
Ashkan_gc
(comments are locked)
|
|
It looks like you actually create 4xCurrentGridSize objects, but it looks quitely likely that you move some of them to exactly the same position, making it look like there's only one. Check your hierarchy for the actual amount of islands created. alas, I already thought of that. The code actually creates CurrentGridSize objects, that's how many there are present. Creating another one at the same position is also prevented in the code (all the elements in the array are initialized at 0, if an element isn't zero, it's taken and can't be taken again).
Jan 24 '10 at 03:19 PM
era
How exactly are you determining the number of objects created? (just to make sure there's no error there!)
Feb 17 '10 at 12:30 PM
duck ♦♦
I checked the Hierarchy panel and the Scene to cross reference and make sure. Anyway, I still haven't found the solution to this riddle but I've written a workaround that does the trick just as well. I still hope to figure it out one day and I'll let you know!
Mar 19 '10 at 09:01 AM
era
(comments are locked)
|
