[C#] Creating and Importing an Array from a separate file
I'm making a 2D strategy game and I want to make a modular level system. What I did was create a script that has an array which has 2 mandatory values: rows and columns that represents the map tiles and then I multiply those two numbers so I know how many tiles I can give a value to.
And I can then set each value separately so for example 0 would be nothing, 1 is a soldier, 2 is a building... And this works fine but I don't want to have to edit each levels content in that script, rather I'd like to create a file in which you have a list of numbers and then in the array in my script the values would be set to the list of numbers I made in the separate file.
How would I do this?
Thanks in advance :)
Answer by ShadyProductions · Dec 01, 2017 at 10:41 PM
I would just suggest going for some simple System.IO.File
setup a text file with your grid example:
1 1 1 1 1
2 1 2 2 1
1 3 2 1 1
1 2 1 1 1
and then just write a method that adds it into the array based on your 0 dimension and 1 dimension length. You can use File.ReadAllText
Method
Quick example (untested):
public int[,] GetGridByTextFile(string path, int gridSizeX, int gridSizeY)
{
// Get all characters in textfile except whitespace
var lines = System.IO.File.ReadAllText(path).Replace(" ", "").ToCharArray();
// Quick check
if (lines.Length < gridSizeX * gridSizeY)
throw new Exception("Grid is not correct in the textfile based on the method input.");
// Setup map grid
int[,] map = new int[gridSizeX, gridSizeY];
for (int x=0; x < gridSizeX; x++)
{
for (int y=0; y < gridSizeY; y++)
{
// Apply grid data
map[x, y] = int.Parse(lines[x].ToString());
}
}
return map;
}
Okay so I have a grid background that spawns and each one is presented by a number so I don't have to apply most of your code but what I needed I used and here is what I came up with:
In the update I call the GetGridByTextFile like this: for (int k = 0; k < 10*10; k++) GetGridByTextFile (Application.dataPath + "/LevelData/level0.ps", k);
The 10x10 just represents the rows and columns for now I just made it like this for testing. In the GetGridByTextFile method I did this:
void GetGridByTextFile (string path, int index) {
numbers = System.IO.File.ReadAllText(path).Replace(" ", "").ToCharArray();
for (int i = 0; i < numbers.Length; i++)
lvl [i] = int.Parse (numbers [i].ToString ());
}
This seems to me like it should work but I get an error saying:
FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)
Also some more errors that basically with this above say that there is a problem in parsing the ints. What is going on here?
Debug what you parse, see if there are no extra whitespaces etc You'll have to filter that out.
Answer by MacDx · Dec 01, 2017 at 09:01 PM
You could create some tools and a couple editor extensions to allow for an easier workflow and creation of this kind of files within Unity's editor.
Something like this tutorial might help with that: https://www.lynda.com/Unity-tutorials/Unity-5-2D-Building-Tile-Map-Editor/384876-2.html
Or you could also (and I like this option better) use an external program that is already specialized in this kind of task, and the only thing you need to code is the parsing of the level files which you probably are already doing. Right now I'm working on a project where I've taken this path. I have a script in my scene that can parse CSV (Comma Separated Values) files and instantiate the corresponding objects. As for the creation of said CSV file, I use the Tiled Map Editor which is like a mini photoshop for tiled based map creation and I really like it, it is pretty easy to use and it is free! There are a couple of plugins in the asset store that do this piece of functionality too, but those aren't free :(
Hope this helps!
I'm not really looking to make a tile editor. I have a script that spawns background tiles which you can select and each tile has a container GameObject which just tells it what is at that tile and I also have a script that creates objects depending on the ID and where in the array it is located. So if I said 1 would be a soldier, then if array element 6 has a value of 1 then at tile 6 the script would create that soldier. The problem is I don't want to have long lists of arrays in my script for each level but rather one level array in the script which takes a list of numbers from a separate file according to the level the player picks and spawns the map according to those numbers in the separate file. That is what I want to know how to do. Also please post only free tutorials :P
which takes a list of numbers from a separate file according to the level the player picks
And how are you planning on getting that file? Are you going to write it by hand? According to the number of levels and the size of your grid, that will get unmaintainable really really fast buddy :/ That's why a tile map editor is necessary.
And this works fine but I don't want to have to edit each levels content in that script, rather I'd like to create a file in which you have a list of numbers and then in the array in my script the values would be set to the list of numbers I made in the separate file. How would I do this?
You should be clearer on your explanation next time. So it is specifically the parsing of the file what you are interested in. $$anonymous$$y suggestion is you start doing some research on some basic parsing, serialization/deserialization, and common formats like JSON and CSV. If you want a more specific solution you will need to post some code so I can see what your working with.
And finally
Also please post only free tutorials :P
I got it for free so I assumed it was free, my bad. You have every right to not use it if you don't want to. Nevertheless, I only post and will keep posting content that I deem meritable. Some things are worth paying for :)
Your answer
Follow this Question
Related Questions
I want to add items from a list into an array using the editor and popup menus 0 Answers
How to perform this Word Game 0 Answers
Assign role randomly from array for my online game 2 Answers
Keeping track of dynamic positions of items in a grid using List/Array? 0 Answers
How do i move a list object at the end of the list while shifting back the others? 1 Answer