- Home /
Read text file to get 2D array
Gday all (again)
Another Q relating to the maze game I am working on:
I have made a maze game that creates levels from scripts rather than constructing it by manually placing the walls down. Initially I just have a Terrain and an empty Game Object called Startup with scripts attached. The maze load script loads the maze from an array and places the player at start location, exit trigger at the exit and walls etc down according to the characters in the array.
Currently I specify the array inside the maze load script (testing and proof of concept) and it works great.
What I WANT to do is read a text file to set up the array... This will allow people to make their own and play them.
What I am looking to do is read a text file and save each line as an array of single characters. I then want to put all the individual line arrays into another array (creating a 2D array)
Code as follows:
#pragma strict
var exitPrefab : GameObject;
private var thePlayer : GameObject;
var thePlayerPrefab : GameObject;
var wallPrefab : GameObject;
private var escMenu = false;
var rowOne = ["8","E","8","8","8","8","8","8","8","8","8","8","8"];
var rowTwo = ["8"," ","8"," "," "," "," "," "," ","8","Z"," ","8"];
var rowThree = ["8"," ","8","8","8","8","8"," ","8","8","8"," ","8"];
var rowFour = ["8"," ","8"," "," "," "," "," ","8"," ","8"," ","8"];
var rowFive = ["8"," "," "," ","8"," ","8"," "," "," "," "," ","8"];
var rowSix = ["8","8","8","8","8","8","8","8","8","8","8","8","8"];
var mazeArray = [rowSix, rowFive, rowFour, rowThree, rowTwo, rowOne];
private var iylength = mazeArray[0].Length; //13
private var ixlength = mazeArray.Length; //6
private var thePlayerLoc;
function Start () {
if (exitPrefab == null){ exitPrefab = Resources.Load("PrefabExit");}
if (wallPrefab == null){ wallPrefab = Resources.Load("WallPrefab");}
if (thePlayerPrefab == null){ thePlayerPrefab = Resources.Load("PlayerPrefab");}
SetupMaze();
}
function Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (escMenu == true) {
escMenu = false;
}
else {
escMenu=true;
}
}
if (escMenu){Screen.lockCursor = false;}
else {Screen.lockCursor = true;}
}
function SetupMaze(){
for (var ix = 0; ix < 13; ix++){
for (var iy = 0; iy < 6; iy++){
if (mazeArray[iy][ix] == "8"){
Instantiate(wallPrefab, Vector3 (ix+50.5, 0.5, iy+50.5), Quaternion.identity);
}
if (mazeArray[iy][ix] == "Z"){
thePlayer = Instantiate(thePlayerPrefab, Vector3 (ix+50.5, 1.5, iy+50.5), Quaternion.identity);
thePlayerLoc = Vector3 (ix+50.5, 1, iy+50.5);
}
if (mazeArray[iy][ix] == "E"){
Instantiate(exitPrefab, Vector3 (ix+50.5, 0.5, iy+50.5), Quaternion.identity);
}
}
}
yield;
thePlayer.transform.position = thePlayerLoc;
}
function OnGUI () {
if (escMenu == true) {
if (GUI.Button (Rect (Screen.width/2-50,Screen.height/2,100,20), GUIContent ("Quit"))){
Application.LoadLevel (0);
}
}
}
Questions:
How do I read a text file to individual arrays (I have searched but not been able to find anything that suits what I need)
Is there a way to test in the unity editor that will work when built? (ie. I am assuming I will have to add another folder once it is built as unity combines everything when it builds? But wont this mean the code that works when testing in unity will have to be changed to work when it is built?)
Is there a better way to compare the arrays? (the rowOne - rowSix is just for testing, When I can get it to read from text files I will make that part dynamic)
Assistance is GREATLY appreciated!!!
Here's a Linq approach, but it's C# (as I currently don't know how to express this in UnityJS), so not an answer to your question. It reads a text file and creates a two dimensional array out of it.
// Read the text file
var text = File.ReadAllText(@"C:\Path\To\Your\File.txt");
// Split the text into individual rows
var rows = text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
// Do some fancy Linq stuff
string[][] maze = rows.Select(r => r.ToCharArray().Select(o => o.ToString()).ToArray<string>()).ToArray<string[]>();
Answer by drudiverse · Aug 26, 2015 at 04:38 AM
Just looking into this topic.
perhaps writing JSON format would make sense in this instance.
The problem i have is that large strings cause an error with unity... gui string thingy not worky memory overload... message..
So either i have to read array entries line by line to strings, or i have to find another format that can read directly as non string data.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080143im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
DirectoryNotFoundException when running a build 1 Answer
Read txt file from Android Downloads file 1 Answer
Writing to txt file using textfield problem 2 Answers
How to read text file to array for my generateLetter script? 0 Answers
How do I make a public array of arrays appear in the inspector? 1 Answer