filein_atmosphere = ReadFile("data/atmosphere.txt") pot4_pos = ReadLine ( filein_atmosphere ) ; fog minimum pot5_pos = ReadLine ( filein_atmosphere ) ; fog maximumCloseFile( filein_atmosphere )fileout_atmosphere = WriteFile("data/atmosphere.txt") WriteLine ( fileout_atmosphere,pot4_pos ) ; fog minimum WriteLine ( fileout_atmosphere,pot5_pos ) ; fog maximum CloseFile( fileout_atmosphere )
116
min fog; 1max fog; 16
WriteLine ( fileout_atmosphere,"fog min; "+pot4_pos ) ; fog minimum
StrictType MyFog Extends TConfig Field minfog:Int = 1 Field maxfog:Int = 16End TypeType TPlayer Extends TConfig Field x:Int Field y:Int Field size:Float Field name:String Method Load:TPlayer( filename:String ) Return TPlayer( Super.Load( filename )) End MethodEnd TypePrint( "FOG" )Local fog:MyFog = MyFog( New MyFog.Load( "config.txt" ) )Print( "MIN: "+fog.minfog )Print( "MAX: "+fog.maxfog )Print( "PLAYER" )Local player:TPlayer = New TPlayer.Load( "config.txt" )Print( "NAME: "+player.name )Print( "X: "+player.x )Print( "Y: "+player.y )Print( "SIZE: "+player.size)player.saveas( "player.txt" )' Config Reflection' Si Dunford (Scaremonger), Nov 2020Type TConfig Field _filename:String Method Load:TConfig( filename:String ) Local fld:TField Local tid:TTypeId = TTypeId.ForObject( Self ) Local file:TStream = ReadFile( filename ) If Not file Return Null _filename = filename ' Parse the config file extracting fields While Not file.Eof() Local keyval:String[] = file.ReadLine().split( "=" ) If Len(keyval) <> 2 Continue fld = tid.findfield( keyval[0] ) If Not fld Continue fld.set( Self, keyval[1] ) Wend ' Clean up file.Close() Return Self End Method Method save:Int() If Not _filename Return False Local tid:TTypeId = TTypeId.ForObject( Self ) Local file:TStream = WriteFile( _filename ) ' Save fields into file For Local fld:TField = EachIn tid.EnumFields() If fld.name()[..1] <> "_" file.WriteLine( fld.Name()+"="+String(fld.get(Self)) ) Next ' Clean up file.Close() Return True End Method Method saveas:Int( filename:String ) _filename = filename Return save() End Method End Type
x=10y=20size=3.5minfog=1maxfog=16name=Scaremonger
Using Reflection; you can save/load custom types directly to/from files using something like this (Tested on BlitzmaxNG).
templine=readline(filein)minfog=Right$(templine$,Len(templine$)-9);-9 is the amount of characters in "Min fog; " before the numbers begin
Many ways of doing it however....What I like doing is this:writeline outfile,"fogout="+fogoutvariableand then on reading do this:val$ = readline(infile)if instr(val,"fogout=") then newval$ = replace(val,"fogout=","") if int(newval) > 0 then fogoutvariable = int(newval) endif endif or similar....
filein_atmosphere = ReadFile("data/atmosphere.txt") val$ = ReadLine(filein_atmosphere) ; fog minimum If Instr(val,"fog min =") Then newval$ = Replace(val,"fog min =","") pot4_pos = Int(newval) val$ = ReadLine(filein_atmosphere) ; fog maximum If Instr(val,"fog max =") Then newval$ = Replace(val,"fog max =","") pot5_pos = Int(newval) CloseFile( filein_atmosphere )
fileout_atmosphere = WriteFile("data/atmosphere.txt") WriteLine ( fileout_atmosphere,"fog min ="+pot4_pos ) ; fog minimum WriteLine ( fileout_atmosphere,"fog max ="+pot5_pos ) ; fog maximum CloseFile( fileout_atmosphere )