Thursday, February 13, 2014

For the love of While loops!

Wow... I suck at C#, there's just no sugar coating it!

I spent several hours this afternoon modifing the system generation function to add in a DISTANCE check (e.g. so that two system are not too close to each other).

Sounds simple right? Sure....

Um yeah... I've never had to FORCE QUIT Unity more times than this afternoon than in ALL the times I've been using Unity.

So... here's what I learned:
To use a WHILE loop inside of a FOR loop - create a separate function and have that function run the WHILE loop and RETURN the result (solution).

Here's some screenshots of code:

        if (i > 0) {
            Vector3 validPos = ValidateNewPosition(ExistingPos,i,newPos);
            newPos = validPos;
        }

And here's the function:

    Vector3 ValidateNewPosition (Vector3[] ePosint currentIndex , Vector3 target) {
        // Yeah - this frackinworks!!!!
        while(true) {
            bool FoundOne = false;
            for (int sysNum = 0sysNum < currentIndexsysNum++) {
                if (Vector3.Distance(ePos[sysNum],target) < distanceBetweenSystem) {
                    Debug.LogError("[" + sysNum +"] Failed Check " + Vector3.Distance(ePos[sysNum],target));
                    FoundOne = true;
                    sysNum = currentIndex + 1;
                }
            } 
            if (FoundOne) {
                target = ScalePosition(urand.PointInASphere());
                ExistingPos[currentIndex] = target;
            } else {
                break;
            }
        }
        return target;
    }





No comments:

Post a Comment