- Home /
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.
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.
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?
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.
How do I go about saving something to disk, and then reloading if necessary? Are there any good tutes?
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 () {
}
}
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?
Also you don't need ToString with print. It takes an object, not a string.
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
Follow this Question
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