Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by JVILL · Feb 03, 2013 at 03:48 AM · terrainarraygridpush

Push into Two-Dimensional Array

Here is my two-dimensional array:

 var terrainArray: int[,] = new int[gridRows, gridCols];

I want to be able to add an additional Row (or Column) to this Array at will, and populate it. How would I go about doing this?

Why?: I'm making an infinite tile-based Terrain generator, and as the player approaches the edge of the grid, a new row or column is made and generated on the fly.

Thank-you in advance for any help.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Feb 04, 2013 at 06:21 AM

You can't resize a 2D array. You can make a new array with a different size and copy the contents over, but that's not really a good idea. It would be better to just keep the array the same size, and move content around as needed. So when the player approaches an edge, shift the contents so that the old stuff on the opposite edge is removed to make room for the new stuff.

Comment
Add comment · 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 JVILL · Feb 04, 2013 at 08:28 AM 0
Share

That isn't an option in this case. Surely if I changed to a regular array = [ [1,1] , [2,1] , [3,1] , [4,1] ], then I could PUSH additional values in?

avatar image Eric5h5 · Feb 04, 2013 at 08:39 AM 0
Share

No, that's a jagged array which also can't be resized. $$anonymous$$aking the array larger isn't an option either, because you will run out of RA$$anonymous$$ eventually. Since there's a limit on array size, you might as well just make the array as big as you think you need to begin with.

Anyway what I described is an option. If you absolutely need the old array data, save it to disk before it's pushed out, then reload it if necessary. That would require writing some kind of database system so you can track the array data appropriately.

avatar image JVILL · Feb 04, 2013 at 09:01 AM 0
Share

How do I go about saving something to disk, and then reloading if necessary? Are there any good tutes?

avatar image Eric5h5 · Feb 04, 2013 at 09:03 AM 0
Share

Use the System.IO.File class.

avatar image
0

Answer by bilek993 · Feb 03, 2013 at 02:12 PM

I will use c# but it is pretty similar in JS.

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
     
     // Declare 2d array
     public int [,] Tablica;
     
     void Start () {
         // Set size 128x128
         Tablica = new int[128,128];
         // Write into array position 115,5 / value 987
         Tablica[115,5] = 987;
         // Read from array position 115,5 and print it as string
         print (Tablica[115,5].ToString());
     }
     
     void Update () {
         
     }
 }
 
Comment
Add comment · Show 3 · 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 jvillinger · Feb 04, 2013 at 05:08 AM 0
Share

Please re-read my question. $$anonymous$$y Two-dimensional array needs an extra row / column. I already know how to put a value into a designated spot of the array.

Would: Tablica = new int[128,128]; Tablica[129,129]= 7;

Work?

avatar image Eric5h5 · Feb 04, 2013 at 06:21 AM 0
Share

Also you don't need ToString with print. It takes an object, not a string.

avatar image bilek993 · Feb 04, 2013 at 02:42 PM 0
Share

jvilinger You have to made second array and then copy first array (Tablica) into second. Next resize your first array (Tablica). Now again copy your second array into first. Why? because if your resize your array it is going to be cleared by system

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

How to generate a grid for procedurally generated platforms 1 Answer

How to create a 2D grid based array with passable/impassable grids for movement ? 1 Answer

A few GUI related questions. 1 Answer

I'm having trouble with grids and instantiating arrays of prefabs. 1 Answer

A* Can't Scan GridGraph 0 Answers


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