- Home /
How to copy a multidimensional array?
I'm trying to make a Astar-pathfinding system and have encountered some bugs, and the first one is that I can't seem to find a good way to copy a multidimensional array.
I was looking at some examples here on answers but I cant find a good way to do this efficiently.
Here is the code, init runs only once at the start and after that I want to copy the array to grid without modifying the first one.
private Node[,] refGrid;
//initiate the pathfinder.
public void init(int width, int height){
refGrid = new Node[width, height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
refGrid[x,y] = new Node(x, y);
}
}
}
public List<Node> CalculatePath(int startX, int startY, int endX, int endY){
//copy the reference array to grid. (this is where I don't know what to do)
Node[,] grid = new Node[refGrid.GetLength(0), refGrid.GetLength(1)];
System.Array.Copy(grid, refGrid, refGrid.GetLength(0)*refGrid.GetLength(1));
//rest of the code omitted.
}
Answer by RudyTheDev · Jan 06, 2015 at 04:06 PM
Can't you simply:
Node[,] grid = new Node[refGrid.GetLength(0), refGrid.GetLength(1)];
for (int x = 0; x < refGrid.GetLength(0); x++)
for (int y = 0; y < refGrid.GetLength(1); y++)
grid[x, y] = refGrid[x, y];
Don't know if your Node
is struct
or class
, so above will either store a copy or a reference.
Also, this sounds very inefficient for A* -- copying the whole map on each search.
Node is a class, containing the F, G and H value and the parent node, so I guess it will store a reference which is why it doesn't reset between moves.
I know its inefficient, I simply wanted it to work so I could start solving the actual bugs in the algorithm and look at optimization later.
But maybe it's better to approach this differently right from the start, what would you recommend?
Would it work to remember what nodes have been modified and once you are done reset only those values? Will there be any issues if several units wants to walk at the same time or is it all done in a single thread so they wait for the 1st one to complete?
Thanks!
These tutorials are apparently a good A* tutorial. The guy implements a binary heap / priority queue, which is probably as efficient as you can get in terms of algorithms. Unless you are micro-optimizing, that's probably sufficient.
You can reuse the same grid, but only create it once. I'm not 100% sure why you are copying it, wouldn't it just be the same as refGrid?
Thanks for the link, will take a look at them and see if I find something useful. I already have a priority queue set up but a heap would be even better.
the Node class also have information like a bool to see if it is on the openlist so I don't actually have to search the list to see if it has been added. So I have to reset them back so the next unit can use the same tiles.
the copying was not the final solution i had in $$anonymous$$d, just something so I knew it would work =)
I'm gonna start by taking a look a the tutorials and see how they do it.
Thanks again!
Node class also have information like a bool to see if it is on the openlist
Ins$$anonymous$$d of bool
, keep an int
. Also keep a global search counter
. Each search iteration, increase this counter. Any cell equal to the current counter value is true, otherwise false (some old value). Increasing the counter essentially sets everything to false for the next search. This way you don't need to reset every node (except may be when you run our of int values).
Your answer

Follow this Question
Related Questions
Copy From Array to List without reference [C#] 2 Answers
How to copy part of an array 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers