Data from File

BlitzMax Forums/BlitzMax Beginners Area/Data from File

Hi. I'd like my unit data to be stored in a file so that it can be changed at a later date. In previous games I made some code that generated a data file by basically just dumping a long series of numbers and information. The game then read this and loaded back the information in the right order.

The problem with this is that more or less data than expected for an entry causes offset-loading problems with later entries, and it's impossible for a human to modify it.

I would like a data file that is written like a text document....
newunit
unitname =
unitattack =
endunit
etc

So it can be modified easily. But I don't know how to go about doing that. Is there any built-in way to access eg ini files, or any free code available?

Thanks.

Look at readline/writeline or, probably better, search the forums for libxml or maxml as, for a lot of data, xml could be the way to go.

This is an early prototype so it's far from optimal. But it should give you a basic idea of how to do something like this. If you don't want to use the "three letters and separator" approach I've used, you'll need to either write your own string tokenizer or find one that's readily available.

' Metal Messiah Structure Loader.

SuperStrict

Type wwStructureFactory
	Global wwsfInst:wwStructureFactory
	Field tbDefs:TBank
	Field tmMap:TMap

	Method New()
		If wwsfInst = Null
			wwsfInst = Self
			tmMap = New TMap
		Else
			Throw "wwStructureFactory is a Singleton, and can only be instanced once."
		EndIf
	EndMethod

	Function getInstance:wwStructureFactory()
		If wwsfInst = Null
			Return New wwStructureFactory
		Else
			Return wwsfInst
		EndIf	
	EndFunction
	
	Method LoadDefinitions(sUrl:String)
		tbDefs = LoadBank( sUrl )
		Local tsIn:TStream = ReadStream( tbDefs )
		Local wwsCurObj:wwStructure = New wwStructure
		While Not Eof(tsIn) 
			Local sCmd:String = ReadLine(tsIn)

			If sCmd.length = 0
				tmMap.insert(wwsCurObj.sName , wwsCurObj)
				wwsCurObj = New wwStructure
			Else
				Select sCmd.toLower()[..3]
					Case "obj"
						wwsCurObj.sName = sCmd[4..]
					Case "mes"
						wwsCurObj.sMesh = sCmd[4..]
					Case "tex"
						wwsCurObj.sTexture = sCmd[4..]
					Case "lgt"
						wwsCurObj.sLightMap = sCmd[4..]
					Case "sha"
						wwsCurObj.sShadow = sCmd[4..]
					Default
						Throw "Unkown definition: ~q" + sCmd + "~q in ~q" + sUrl + "~q"
				EndSelect
			EndIf
		EndWhile
	EndMethod
EndType

Type wwStructure
	Field sName:String
	Field sMesh:String
	Field sTexture:String
	Field sLightMap:String
	Field sShadow:String
EndType

And an example structure file:
obj=Powerplant
mes=powerplant.3ds
tex=powerplant.png
lgt=powerlight.png
sha=powershadow.png

obj=Terrain
mes=terrain.3ds
tex=terrain.png
lgt=terrainlight.png

obj=Homotank
mes=homotank.3ds
tex=homotank.jpg


http://www.blitzbasic.com/codearcs/codearcs.php?code=1890#comments

This could also be of help.