Trouble with FileStream loading ints
Monkey Forums/Monkey Beginners/Trouble with FileStream loading ints
| ||
I am making my first game in monkey, which will be a grid based tower defense game. i have a 2d array made up of objects with properties Id1 and Id2 which are used elsewhere to determine what to draw in which location. I am trying to set ids for each object in the 2d array based on a premade file containing ints. I receive the error "uncaught monkey exception" Any advice on where i could look to fix this would be appreciated. Method LoadLevel() Local path:String = "monkey://data/level.txt" Local File:=FileStream.Open(path,"r") While File.Eof() = 0 For Local x:Int=0 Until array.Length() For Local y:Int=0 Until array[0].Length() array[x][y].groundId = FileToRead.ReadInt() Next Next For Local x:Int=0 Until array.Length() For Local y:Int=0 Until array[0].Length() array[x][y].collisionId = FileToRead.ReadInt() Next Next Wend File.Close End This is the contents of the file i am trying to load (level1.txt) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| ||
The Method ReadInt() reads numbers which were previously written in a not human readable way. Your text file currently has no such format, so I advise you to use ReadLine() or ReadString() instead. You can than split the string apart into pieces by using Split() e.g. Local str:String = FileToRead.ReadString() 'Or ReadLine which will only read a single line (might be easier to handle for you) Local myNumbers:String[] = str.Split(" ") ' Start a new entry when the space character shows up .... To convert a string to Int you can use Int(myNumbers[index]) Then maybe something like.... to access the right number use myNumbers[height*y + x] |