Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, February 22, 2014

ScriptableObjects - you shiny cute thing!

Given my ultra-lack of experience with Unity (and C#) I spent a couple days researching and working on ScriptableObjects. There are useful pages on the Unity site along with some excellent blog posts and found a great YouTube video.

Working example:
I got my "factions" example up and working and can easily create new factions and alter the existing ones via the editor. (As seen in this video).

...and then it hit me!

SCRIPTABLEOBJECTS ARE FOR READONLY DATA!


Sadly it wasn't until I actually got my example to update an existing faction that it hit me. All scriptableobjects are stored in the READONLY bundle. FFS!, what an idgett!

EDIT: They are of course still very useful in reducing memory as described HERE.

Monday, February 17, 2014

Universe Creator - close to pre-alpha status

The Universe creator is coming along nicely. Just resolved the faction sub-division problem by using what I called a "FactionMatrix" prefab. This guy has "sub cubes" layer out in random patterns that use OnTriggerEnter to report a newly created "sphere" (system) so that I can set the systems ("sphere's") faction properly.

This gets into the fundamentals of the design; as to how many factions and their layout based on the universe "seed" number. Given that every galaxy will have two additional factions:

  1. None
  2. Unknown
If the GC (Galaxy Creator) aka player chooses to use three factions, the galaxy will actually contain five total.



Creating and debugging the faction alignment process was fun and had me stumped for a bit but the solution is rather light-weight and fast. Especially since it only has to be run on the creation of each new universe.

Oh and StartCoroutine() and yield are my new best friends... :-)

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[] ePos, int currentIndex , Vector3 target) {
        // Yeah - this frackin' works!!!!
        while(true) {
            bool FoundOne = false;
            for (int sysNum = 0; sysNum < currentIndex; sysNum++) {
                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;
    }