- Home /
Multidimensional Array of Vector2
Could someone please explain how I can make a multidimensional array of Vector2 variables.
Currently I am trying to use a list as follows:
private List<Vector2[, , , , , ,]> enemySpawnPositions = new List<Vector2[, , , , , ,]>();
But my question is, how do I populate the data?
I have tried the following:
enemySpawnPositions.Add(new Vector2[new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0)]);
enemySpawnPositions.Add(new Vector2[]{new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0),new Vector2(0, 0)});
enemySpawnPositions.Add(new Vector2[{ new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0) }]);
enemySpawnPositions.Add(new Vector2[ Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0)]);
enemySpawnPositions.Add(new Vector2[]{ Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0)});
enemySpawnPositions.Add(new Vector2[{ Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0), Vector2(0, 0) }]);
...but all give me errors (which isn't surprising). So could someone please explain how I populate multidimensional arrays/lists. The data is irrelevant right now, I just need to know how to do it.
In case you're wondering I need a block of 7 (always 7) Vector2 co-ordinates but an unlimited quantity of them.
Thanks
Some simple examples:
$$anonymous$$aking a 1D array of Vector2 objects: new Vector2[];
$$anonymous$$aking a 2D array of Vector2:
vectorArray = new Vector2[7,7];
for (int i = 0; i < vectorArray.GetLength(0); i++) {
for (int j = 0; j < vectorArray.GetLength(1); j++) {
vectorArray[i,j] = new Vector2(0,0);
}
}
$$anonymous$$aking a 2D list of Vector2:
List> vectorList = new List>();
vectorList.Add(new List() { new Vector2(0,0), new Vector2(0,0).. etc
Answer by adelphiaUK · Aug 15, 2014 at 11:01 AM
OK. I've managed to sort it.
I was making it more complicated than I should have been.
My solution is:
private List<Vector2[]> enemySpawnPositions = new List<Vector2[]>();
And then to populate it, it's:
enemySpawnPositions.Add(new Vector2[7] { new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0) });
Or if you don't care how many you have per group then it's:
enemySpawnPositions.Add(new Vector2[] { new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0) });
Note: the omission of 7 in the square brackets.
Thanks guys for pointing me in the right direction!
Answer by rutter · Aug 14, 2014 at 10:20 PM
Supposing you have a 2-dimensional array:
int[,] values = new int[10,10];
You could access it like so:
int x = 0;
int y = 0;
values[x,y] = 10;
With that said, though, your description at the bottom sounds more like this sort of structure:
class MyClass {
public Vector2 a; //give these vars descriptive names
public Vector2 b;
public Vector2 c;
public Vector2 d;
public Vector2 e;
public Vector2 f;
public Vector2 g;
}
List<MyClass> myList = new List<MyClass>();
I had tried this method but it becomes a bit impractical when you have to do the following to add one entry (remember, the entries needs to be unlimited):
$$anonymous$$yClass insertion = new $$anonymous$$yClass();
insertion.a = new Vector2(0,0);
insertion.b = new Vector2(0,0);
insertion.c = new Vector2(0,0);
insertion.d = new Vector2(0,0);
insertion.e = new Vector2(0,0);
insertion.f = new Vector2(0,0);
insertion.g = new Vector2(0,0);
myList.Add(insertion);
Either that or I'm misunderstanding your answer.
You should be able to initialize a
, b
and so on as Vector2.zero in the constructor of $$anonymous$$yClass rather than writing it like that
I appreciate that, but Vector2(0,0) was just being used as an example. I may want to do Vector2(3.5f, 0.2f) or Vector2(6.3f, 1.5f) if you see my point. $$anonymous$$aybe i could have written a constructor within my class which would have accomplished the same thing I guess. Anyway, it's sorted now, thanks (see my answer).