Loading a Saved Tilemap (ReadFile)

BlitzMax Forums/BlitzMax Beginners Area/Loading a Saved Tilemap (ReadFile)

Matt Vinyl(Posted 2009) [#1]
Hi all,

		If KeyHit(KEY_S) Then 'save the map file
			Local ts_savemap:TStream = WriteFile("media\usermaps\map.txt")
			For Local x:Int = 0 To 14
				For Local y:Int = 0 To 14
					WriteInt(ts_savemap,ar_mapd1[x,y].t)
					WriteInt(ts_savemap,ar_mapd2[x,y].t)
				Next
			Next
		End If
	
		If KeyHit(KEY_L) Then 'load the map file
			Local ts_loadmap:TStream = ReadFile("media\usermaps\map.txt")
			For Local x:Int = 0 To 14
				For Local y:Int = 0 To 14
					ar_mapd1[x,y].t=ReadInt(ts_loadmap)
					ar_mapd2[x,y].t=ReadInt(ts_loadmap)
				Next
			Next
		End If


The save portion of the above code works fine, but the load portion doesn't. It 'works', but it just populated my ar_mapd1 and ar_mapd2 arrays with all the same tiles (in otherwords, not what I saved!)

Would anyone be kind enough to point out where I've made the mistake(s)!

Thanks!


Jesse(Posted 2009) [#2]
are you shure you are saving it correctly? have you looked at the saved file? put a print in the save loop to see what it's saving:
print ts_savemap,ar_mapd1[x,y].t + " " + ts_savemap,ar_mapd2[x,y].t



Dabhand(Posted 2009) [#3]
SuperStrict 

Graphics 640,480

Const LOAD_FROM_FILE:Byte = False   'change to true to load from file

Const MAP_WIDTH:Int = 15
Const MAP_HEIGHT:Int = 15

Type TTilemap
	Field t:Int 
End Type

Global ar_mapd1:TTilemap[MAP_WIDTH,MAP_HEIGHT]
Global ar_mapd2:TTilemap[MAP_WIDTH,MAP_HEIGHT]

If LOAD_FROM_FILE = True
	LoadTileMap()
Else
	RestoreData dat
	For Local y:Int = 0 To MAP_HEIGHT-1
		For Local x:Int = 0 To MAP_WIDTH-1
			Local incomingData:Int
			ReadData incomingData
			ar_mapd1[x,y] = New TTileMap
			ar_mapd2[x,y] = New TTileMap
			ar_mapd1[x,y].t = incomingData
			ar_mapd2[x,y].t = incomingData
		Next
	Next
End If

Repeat
	Cls
	
	If KeyHit(KEY_S) Then 'save the map file
		SaveTileMap()
	End If
 
	If KeyHit(KEY_L) Then 'load the map file
		LoadTileMap()
	End If
	
	For Local y:Int = 0 To MAP_HEIGHT-1
		For Local x:Int = 0 To MAP_WIDTH-1
			If ar_mapd1[x,y].t = 1
				DrawRect x Shl 4, y Shl 4,16,16
			End If
		Next
	Next
	
	Flip
Until KeyDown(KEY_ESCAPE)

End

Function LoadTileMap()
	Local ts_loadmap:TStream = ReadFile("map.txt")
	For Local y:Int = 0 To MAP_HEIGHT-1
		For Local x:Int = 0 To MAP_WIDTH-1
			ar_mapd1[x,y] = New TTileMap
			ar_mapd2[x,y] = New TTileMap
			ar_mapd1[x,y].t=ReadInt(ts_loadmap)
			ar_mapd2[x,y].t=ReadInt(ts_loadmap)
		Next
	Next
	CloseStream ts_loadmap
End Function

Function SaveTileMap()
	Local ts_savemap:TStream = WriteFile("map.txt")
	For Local y:Int = 0 To MAP_HEIGHT-1
		For Local x:Int = 0 To MAP_WIDTH-1
			WriteInt(ts_savemap,ar_mapd1[x,y].t)
			WriteInt(ts_savemap,ar_mapd2[x,y].t)
		Next
	Next
	CloseStream ts_savemap
End Function

#dat
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


Quicky! ;)

Dabz


Matt Vinyl(Posted 2009) [#4]
Thanks - tested that and it certainly appears to be saving the data as expected. So I'm guessing it's defintely the load routine that's falling over somewhere... Hmm...


Dabhand(Posted 2009) [#5]
ar_mapd1[x,y] = New TTileMap
ar_mapd2[x,y] = New TTileMap


In my example, tinker with this line:-

Const LOAD_FROM_FILE:Byte = True 


Dabz


InvisibleKid(Posted 2009) [#6]
well Dabz beat me to it while i was typing it up, but here it is anyway

seems to work.


i'm wondering if your problem is a lack of 'CloseStream' commands in the load and save functions, their absent atleast from the code in the first post.


Matt Vinyl(Posted 2009) [#7]
Thanks guys, I've got it working now thanks to your help.

Where would I be without this site?! ;) ;)


Dabhand(Posted 2009) [#8]

well Dabz beat me to it while i was typing it up, but here it is anyway



lol, I've wrote that much code surrounding tilemaps that it just rolls off the fingers! :)

Dabz


InvisibleKid(Posted 2009) [#9]
glad you got it working matt

lol, I've wrote that much code surrounding tilemaps that it just rolls off the fingers! :)



no doubt :)

as i was coding my example, i was able to fill the arrays with dummy figures, then save the map file and clear it, but i could not load it unless i exited out of the program and restarted then load the map right away.

it's been quite awhile since i wrote any load/save routines so i pondered over it again and again then i decided to look at the help files. sure enough 'dohhh' i forgot to close the streams after use, once i did that every thing seemed to work fine.


Matt Vinyl(Posted 2009) [#10]
Cheers!

but i could not load it unless i exited out of the program and restarted then load the map right away.


Strangely, that was a subsequent issue I had - As you say, it was down to the fact I wasn't closing the stream. DOH!

Thanks once again...