• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Taxen0 · Jan 06, 2015 at 03:40 PM · c#arraycopy

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.
 
 }
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
Taxen0
torleone

People who like this

2 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Taxen0 · Jan 06, 2015 at 06:40 PM 0
Share

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!

avatar image RudyTheDev · Jan 06, 2015 at 10:42 PM 0
Share

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?

avatar image Taxen0 · Jan 06, 2015 at 10:58 PM 0
Share

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 mind, 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!

avatar image RudyTheDev · Jan 07, 2015 at 12:49 AM 0
Share

Node class also have information like a bool to see if it is on the openlist

Instead 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to copy part of an array 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Copy From Array to List without reference [C#] 2 Answers

How to implement and Access Jagged Array C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges