Configuration screen

Blitz3D Forums/Blitz3D Beginners Area/Configuration screen

_PJ_(Posted 2003) [#1]
I am trying to add a configration options screen which will be a separate executable from the main game.
This program should read from a .DAT file, and overwrite it too allowing for the main program to use the data by reading it back.

However, when I test it, I have a problem with the first line of the DAT file.
For test purposes, I only use two lines:

first line = Integer value for GfxMode
second line = string$ for game difficulty

The string$ works [erfectly. The integer, however, once Written (with WriteInt) the .DAT file becomes somehow corrupted and instead of being a '1' or whatever, just shows null-characters (the little squares) and some tabs.

How can I prevent this?

my code is below:

config=ReadFile("Config.dat")

gfxmode=ReadLine(config)
difficulty$=ReadLine$(config)

CloseFile(config)
totalgfx=CountGfxModes()
Graphics GfxModeWidth(gfxmode),GfxModeHeight(gfxmode),GfxModeDepth(gfxmode),0
SetBuffer BackBuffer()
While Not KeyDown(1)
Cls
Text GraphicsWidth()/2,0,"Configuration Settings",1,0
Text GraphicsWidth()/2,30, "RESOLUTION (R): "+GfxModeWidth(gfxmode)+" x "+GfxModeHeight(gfxmode)+" Bit Depth:"+GfxModeDepth(gfxmode),1,0
If KeyDown(19) Then gfxmode=gfxmode+1
If gfxmode>totalgfx Then gfxmode=1
Text GraphicsWidth()/2,60, "DIFFICULTY LEVEL (D): "+Difficulty$,1,0
If KeyDown(32) Gosub DIFF_CHANGE
If KeyHit(19) Or KeyHit(32) Then Delay 250
Flip
Wend
EndGraphics
config=WriteFile ("config.dat")
WriteInt (config,gfxmode)
WriteString (config,difficulty$) 
CloseFile(config)
ExecFile "Turbo.exe"
.diff_change
If difficulty$="Easy" Then difficulty$="Normal" : Return
If difficulty$="Normal" Then difficulty$="Hard" : Return
If difficulty$="Hard" Then difficulty$="Very Hard" : Return
If difficulty$="Very Hard" Then difficulty$="Very Easy" : Return
If difficulty$="Very Easy" Then difficulty$="Easy" : Return


Apologies for messy code - it's still in test-phase :)


Gabriel(Posted 2003) [#2]
But integers SHOULD show up as null characters in a dat file. It you read it back with ReadInt() it will come back as 1.

If you want it to be readable, then write everything as a string. Blitz does type conversion automatically so you can WriteString() an integer.


Sir Gak(Posted 2003) [#3]
Readline() reads text, including the terminator for the line. The docs for Readline$() say:

Characters are read from the input file until an "end-of-line" mark is found. An "end-of-line" can be a single carriage return (0Dh) or a single linefeed (0Ah) or carriage return followed by a linefeed (0Dh, 0Ah). Reading beyond the end of file does not result in an error, but each value read will be a zero length string.
ReadLine$ returns all chars except chr$(13)/chr$(10).

When reading from a file, you need to use the command that corresponds with the one that you used to write to that file. This you did not do when you used WriteInt for the write operation, but then used Readline (not ReadInt) for the read operation. Pair 'em up! Use Writeline/Readline, or use WriteInt/ReadInt, but don't mix and match the commands.