I have a game with 6 races and 6 units per race for a total of 36 units (more coming). Right now, all of my code is based off the idea that each unit type and its important variables can be found in a series of nested arrays declared in a script called “GlobalVariables.cs” such as:
public static float[][] UnitHealth = new float[][] { new float[] {}, new float[] { 0, 1, 2, 3, 4, 5, 6 }, new float[] { 0, 1, 2, 3, 4, 5, 6 }, new float[] { 0, 1, 2, 3, 4, 5, 6 }, new float[] { 0, 1, 2, 3, 4, 5, 6 }, new float[] { 0, 1, 2, 3, 4, 5, 6 }, new float[] { 0, 1, 2, 3, 4, 5, 6 } };
All of my code is based off the idea that if you know the race (as an integer 1-6) and the unit# (as an integer 1-6) you can find the correct attribute whether that attribute is the unit’s sprite in UnitSprite[Race][Unit] or the unit’s attack in UnitAttack[Race][Unit] or the unit’s speed in UnitSpeed[Race][Unit]. A value of Race = 3 and Unit = 2 will always find the appropriate attributes no matter which array/attribute is being called for.
Now the problem I’m encountering is two-fold:
-
Is there a more elegant way to handle this? I’m roughly a month or so into C# and Unity. I feel like I can say with confidence that anything I do is not the optimal way of doing it. However, this is the best method I’ve come up with so far and it works, so three-cheers for me.
-
If this is a worthwhile method to continue with, my next question is this: “how can I present all of this data to me the coder/tester/developer in a presentable way for editing while still getting it into these arrays for use by other scripts?” Right now, all of this data is in a long “string” (above) in the variable declarations which I have to count along to find the right reference location if I want to test changing a unit’s health, for instance, but I’m unsure how to declare the variables more like the example below while still getting them into arrays for the rest of my code to continue working.
I would love it to look something like this (or better), which was how my old code was organized when I only had 6 units and no races to keep track of:
// Builder
public static float Builder_Cost = 1;
public static float Builder_HP = 3;
public static float Builder_Speed = 1;
public static float Builder_Attack = 1;
public static float Builder_Attack_Speed = 0.5f;
I read about property drawers, but it seems like an intense way to achieve the visibility and edit-ability I want while still leaving me unsure how to put it all into the arrays.
Thanks everyone for taking the time to help me with my dumb problem ![]()