|
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?
}
(comments are locked)
|
|
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; 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)
|
|
Well, in your case I would suggest to use a string array (or maybe a List )instead ot your 2-dimentional char array:
If you need more information in one cell I would use a
(comments are locked)
|

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.