Array changing data on its own

BlitzMax Forums/BlitzMax Beginners Area/Array changing data on its own

MattVonFat(Posted 2005) [#1]
Hi. I have an array which i fill with values from a text file the problem is when i fill it with the values and then re access them a small chunk changes.

The code is:
Function populate_tiles(map:String)

	Local count:Int
	Local xCount:Int = 0
	Local yCount:Int = 0
	Local mapFile
	Local dataFile
	Local mapCode
	
	dataFile = ReadFile("data/maps.txt")
		xSize = Readint(dataFile)
		ySize = Readint(dataFile)
	CloseFile(dataFile)	
	mapFile = ReadFile(map)

	tiles = New Int[xSize,ySize,1]
	
	For yCount = 0 To ySize
		For xCount = 0 To xSize
			mapCode = ReadByte(mapFile)
			If mapCode = 13
				mapCode = ReadByte(mapFile)
				mapCode = ReadByte(mapFile)
				tiles[xCount,yCount,1] = Int(Chr(mapCode))
				Print xCount + ":" + yCount + ":" + Int(Chr(mapCode))
				Print tiles[xCount,yCount,1]
			Else
				tiles[xCount,yCount,1] = Int(Chr(mapCode))
				Print xCount + ":" + yCount + ":" + Int(Chr(mapCode))
				Print tiles[xCount,yCount,1]
			End If
		Next
	Next
	CloseFile(mapFile)
	Print ""
	For yCount = 0 To ySize
		For xCount = 0 To xSize
			Print tiles[xCount,yCount,1]
		Next
	Next
	Print ""
	FlushMem
End Function


Now when it runs through and prints out the values it has gotten in the first loop they all come up correctly. But when i run the second loop (which is there to make sure it is a problem inside the function) the 2nd - 20th values (x=2 to 20 y=0) changfe from 1 to 0. It is only these tiles though.

The screen text file reads:



and the xSize is 19 and ySize = 14 (as i start from 0 in the loops and array)

Does the problem come up for anyone else or does someone know where i am going wrong?


Shambler(Posted 2005) [#2]
If you want to fill an array from 0-xSize and 0-ySize you need to dimension that array by [xSize+1,ySize+1,1].

You are wanting an array 20 elements by 15 so you must dimension it [20,15,1] even though you access the elements 0-19 and 0-14 that is still 20 and 15 elements respectively.


Floyd(Posted 2005) [#3]
And for the same reason the third dimension must be at least 2.

If this is the source of the error then turning Debug On would catch it.


Shambler(Posted 2005) [#4]
Thats true, or he needs to

tiles[xCount,yCount,0] = Int(Chr(mapCode))



MattVonFat(Posted 2005) [#5]
Oh right, thanks i didnt realise.