- Home /
Reading a Matrix from a .txt for level buildup
Okay, so I'm fairly new to Unity, js and C#, but I have to create a game for a school project. I'm working on a 2D tiled level, and I had the idea of saving a set of digits in a txt to use for generating the level. For example:
1,0,2,1
5,2,0,1
4,2,3,1
Every number would be a different type of tile, with 0 being no tile (a gap), 1 being grass, 2 being water etc.
On reading, a separate game object would instantiate a tile on the correct position at start (I guess this will be done with loops, but how would I count the amount of rows and columns then?).
The problem: I don't know how exactly to make it read the file (read some stuff about System.IO but it is rather confusing), plus I'd like to save this grid in a Matrix (var text[x][y], right?) for future reference in the game (like checking where a character can go and where not). How do I do this? Thanks in advance.
Answer by syclamoth · Feb 29, 2012 at 12:53 AM
In this case, there's not too much point messing around with System.IO
. I recommend learning C# over JS, because it's something that will be, in general, more useful to you (as well as having better .net compatibility).
I recommend storing the information as serialised objects internally in Unity. Unless there is some pressing need to be able to modify the levels outside, there's not need to trouble yourself.
Now, because Unity does not support serializing either jagged arrays or multidimensional arrays, you'll need to create your own special classes for this. Start with an enum for tile types:
[System.Serializable]
public enum TileType
{
Rocks,
Trees,
Water,
Sand
}
Then, create the data-storage types:
[System.Serializable]
public class TileArray
{
public Tile[] contents;
public TileArray(int size)
{
contents = new Tile[size];
}
}
[System.Serializable]
public class TileArrayArray
{
public TileArray[] xAxis;
public Tile this[int x, int y]
{
get{
return xAxis[x].contents[y];
}
set {
xAxis[x].contents[y] = value;
}
}
public TileArrayArray(int sizeX, int sizeY)
{
xAxis = new TileArray[sizeX];
for(int i = 0; i < sizeX; ++i)
{
xAxis[i] = new TileArray(sizeY);
}
}
}
Now, in any of your classes, you can use
public TileArrayArray tiles = new TileArrayArray(10, 10);
to create a 10x10 grid of 'Tile' objects, that can be accessed by using
Tile curPoint = tiles[xPos, yPos];
There you go! Serializable multidimensional arrays.
Copied them in c# scripts, but in the TileArrayArray the line public TileArrayArray(sizeX, sizeY) gets underlined, and I get three errors: two times CS1041: Identifier expected (first time withTileArrayArray.cs(19,27) and once with (19,34), and once CS0100:Theparameter name 'NeedSomeGeneratorHere' is a duplicate. Any idea what went wrong? I had to make them in three separate cs files, right? Anyway, thanks so far for helping! :)
Sorry, made a few mistakes there. They're fixed now (forgot to add the types for the constructor). No, you can make them all in one file, just make sure you don't put them inside a '$$anonymous$$onoBehaviour' class. I have no idea what that 'NeedSomeGeneratorHere' thing is. Remember that I haven't given you any usage examples here- all you really need to know is the indexer syntax and constructor.
Sorry to revive this after a month, but I'm looking to do something quite similar to the OP - but what is "Tile" and where is it defined? That's the only part I'm a bit confused on.
Obviously 'Tile' is a class that you declared elsewhere. If it's not in the Unity API, you need to write it yourself! The Unity API is a tool, not a complete program.
Answer by Nalx · Nov 11, 2016 at 11:43 AM
To read your numbers try this :
public void LoadText (string fileName){
string[][] dataMatrix;
string[] result = File.ReadAllLines(fileName);
dataMatrix = new String[result.Length][];
for (int i = 0; i < result.Length; i++) {
string[] entries = result [i].Split (',');
for (int j = 0; j < entries.Length; j++) {
dataMatrix[i] = entries;
}
}
for (int i = 0; i < result.Length; i++) {
for (int j = 0; j < dataMatrix[i].Length; j++) {
Debug.Log (dataMatrix [i] [j]);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613064609im_/https://answers.unity.com/themes/thub/images/avi.jpg)