x


C# dynamic char[,] from file?

I am still a bit new to c#/unity, and I would like to dynamically create a 2d char array. So I have an object(levelLoader) with a mostly working script below.

This is functional, but is not dynamic, and a massive kludge. Is can someone help me out?

The text file is nothing by simply chars with a end of line char '/n' ... thoughts?

void Start()
{
    //load the mapfile into the data line
    char[] dataLine = mapFile.text.ToCharArray();
    int row = 0;
    int col = 0;
    char[,] mapMatrix = new char[2000, 2000];
    int x = 0;
    int y = dataLine.Length;
    while (x < y)
    {
        if (dataLine[x] == '\n')
        {
            row++;
            x++;
            col = 0;
        }
        //Debug.Log(Spot0 + " " + Line0);
        else
        {
            mapMatrix[row, col] = dataLine[x];
            x++;
            col++;
        }

    }

}

more ▼

asked Feb 25 '11 at 09:49 PM

Siegeon gravatar image

Siegeon
45 6 6 15

The main reason that I am focused on a 2d array is that it is very easy to navigate for a tile based 2d world. Each element of the array is both the coordinate within the world, and the object. There should be a natural way to accomplish this, but the syntax is escaping me.

Feb 26 '11 at 12:53 AM Siegeon
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I'd probably store the size of the mapMatrix you required inside the mapFile.txt up the top as some form of meta data. You could then just grab the dimensions first thing after opening the file and create the array.

Otherwise you'll probably need to use some form of container

Personally never used a multidimensional array for anything but small data that requires very quick processing. I find the functionality and cleanness of a container much more convenient. You do lose the ability to use multiple dimensions but finding the required index from a x/y value is easy as y * width + x;

more ▼

answered Feb 25 '11 at 10:17 PM

tertle gravatar image

tertle
1.1k 15 18 33

Since I am looking to make the maps on the fly I need the array to be dynamic, for the most part I will not know the size before its use.

Feb 26 '11 at 12:54 AM Siegeon

But the files are generated ahead of time, and you know the matrix size at file generation time. So store width and height at the top of the file, read those first, and then create char[,] mapMatrix = new char[width, height];

Feb 26 '11 at 08:59 AM yoyo

If you don't want to change the file format, then parse the file twice -- first pass just determine the width and height (maxcol and maxrow) without storing anything, then allocate your array, then second pass store the data.

Feb 26 '11 at 09:00 AM yoyo
(comments are locked)
10|3000 characters needed characters left

Well, in your case I would suggest to use a string array (or maybe a List )instead ot your 2-dimentional char array:

string[] mapMatrix = null;

void Start()
{
    //load the mapfile
    mapMatrix = mapFile.text.Split('\n');

    // access via mapMatrix[row][col]
}

If you need more information in one cell I would use a List<List<CCustomClass>>

more ▼

answered Feb 26 '11 at 12:25 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x149
x18

asked: Feb 25 '11 at 09:49 PM

Seen: 1246 times

Last Updated: Feb 25 '11 at 09:49 PM