- Home /
Arrays vs Coordinates
G'day guys, I'm not after code but rather code design advice. I've built the 'chassis' of a maze-navigation game, where the player moves along seemingly in a grid. At each interval of 1 unit, they reach what I am calling a 'junction'. This tells them which directions are available - North, South, East or West. To check which 'junction' the player has reached, I've created an IF statement for each junction, that runs in Update().
I'm considering converting this to use a multidimensional array(or two standard arrays, one for X and one for Z), but since the current system runs so well, and I've had trouble finding any information on multidimensional array use in Unity except vague reports that it used to require a hack, but no longer does.
I'd like to hear other people's opinion on this, as I'm making respectable progress in what is my very first 3D game, and I'd like to refine it as much as possible.
If it's relevant, I plan on creating a browser-embedded version, to enable players to compete for a 'World High Score'.
Cheers
Stew
Answer by DaveA · Mar 08, 2011 at 11:59 PM
I would first try a 2D array of ints, each int consisting of a bit field where north = 1, south = 2, west = 4, east = 8 and you add those together to get the possible directions at each junction.
However, if you plan on expanding things beyond this, you might also try creating a Junction class, which contains such a bit field int, but could also contain other data, such as what texture to show on the floor there, what things they might find there, etc. Then make the 2D array of Junction.
That sounds good, but when you say 2D, you mean a $$anonymous$$ultidimensional array? There's no mention of multidimensional arrays in the scripting reference. Is it supported?
Answer by Eric5h5 · Mar 09, 2011 at 12:09 AM
I assume you're using JS; C# multidimensional arrays always worked. In any case, multidimensional arrays in JS now work directly without having to use type inference hacks.
private var maze : int[,]; var xSize = 10; var zSize = 20;
function Start () { maze = new int[xSize, zSize]; }
Note that you can always use a standard 1D array to represent any number of dimensions.
private var maze : int[]; var xSize = 10; var zSize = 20;
function Start () { maze = new int[xSize * zSize]; }
In this case, to access x = 3 and z = 5, you'd do
maze[3 + xSize*5]
As to why you might want to do this when multidimensional arrays exist, it's because 1D arrays are faster even with the extra bit of math. (Note: yes, xSize is correct, not zSize. When specifying rows, you need to multiply by the number of columns, assuming x = columns and z = rows.)
Ok I've edited my question, although it might go unnoticed. I'm tempted to ask a new question, but I'll try this first.
Sorry, I've been playing around with this alot. I have a variation with an Array working now, but on a different question. You can see it here http://answers.unity3d.com/questions/45915/building-a-multidimensional-array-for-grid-movement