putting comments in text file

Blitz3D Forums/Blitz3D Beginners Area/putting comments in text file

slenkar(Posted 2004) [#1]
Is it possible to have comments or bookmarks in a text file?

so I dont have to open a new text file for each level data.


Matty(Posted 2004) [#2]
Yes.
You will just have to allow for it in your code somehow.
Sorry I cannot be more specific but if you elaborate on what you want your text file to contain it would help.


Gabriel(Posted 2004) [#3]
Sure. You could do something simple with Left$(in$,1) to see if it's ";" and if it is, ignore it. Or you could go the whole hog and use XML for your levels.


WolRon(Posted 2004) [#4]
To expand on what Sybixsus said;
He meant to check to see if the first character in the line is a semicolon (meaning a comment), and if so then to ignore it.
in$ = ReadLine("mydata.txt")
If Left$(in$, 1) <> ";"
  ;it's not a comment, do what you need to do
EndIf



slenkar(Posted 2004) [#5]
thanks thats very useful,

can I hold data for 10 levels in 1 text file of would it be easier to create 10 text files?

like for example
;level1
;resources
20

;level2
;resources
10

i need to create bookmarks
so i know where to read


WolRon(Posted 2004) [#6]
1 file is all you need. Users will be less likely to mess up your program if there are fewer files to lose/corrupt.

You got the right idea.


big10p(Posted 2004) [#7]
i need to create bookmarks
so i know where to read

This isn't really necessary if you write a half decent parser. For example, this could search through the file for a line that starts with "level = ", followed by the number of the level you want to read in. Then, from that point in the file, you read in all the other settings, like "resources = 20", or whatever.

However, for your situation, I think it would probably be best to simply read sequentially through the file at the start of the game and put all the values into an array of types.


Gabriel(Posted 2004) [#8]
Have a look at jump around ( should be in the User Creations forum ) and you will see that uses XML for level data. I assume it's open source so you can also get the source code to see how the data is read.


Zethrax(Posted 2004) [#9]
There's an XML parser somewhere in the code archives, too, but it's not very well documented.


Perturbatio(Posted 2004) [#10]
JumpAround


slenkar(Posted 2004) [#11]
I had a look at an XML page and it may be overkill to learn a whole new language,
I think Ill tell blitz to search for keywords


slenkar(Posted 2004) [#12]
Function Read_line(file)
.label1
ine$=ReadLine$(file)
If Left$(ine,1)<>";"
Return ine
Else
Goto label1
EndIf


I came up with this and it works but it doesnt read floats.

How do I have comments and the ability to read floats?

End Function


PowerPC603(Posted 2004) [#13]
Function Read_Line$(File)
	; Read a line from the given file
	LineFromFile$ = ReadLine$(File)

	; Keep reading lines until there's a line found that doesn't start with a semicolon
	While Left$(LineFromFile$, 1) <> ";"
		; Make sure the end of the file hasn't been reached yet
		If Not Eof(File)
			; Read a new line
			LineFromFile$ = ReadLine$(File)
		EndIf
	Wend

	; Return the first line without a semicolon
	Return LineFromFile$
End Function


This function reads all kind of data.
When the function returns something, it is always a string.
Just do this:

FloatVar# = Read_Line$(file)


And Blitz converts it automatically into a float, if the line contained a float value.


PowerPC603(Posted 2004) [#14]
Personally, I use "datablocks" in my textfiles.

A textfile from my game:
; This datablock containes all data about "Station01"
[Station]
Name              = Station01
FileName          = Station01.b3d
Texture           = Station01.bmp
PositionX         = -1000
PositionY         = 0
PositionZ         = -1000

; This datablock containes all data about "Jumpgate01"
[Jumpgate]
Name              = Sol system -> Wolf 359
FileName          = Jumpgate.b3d
Texture           = Jumpgate.bmp
PositionX         = 2500
PositionY         = 0
PositionZ         = 2500
TargetSector      = Wolf 359
TargetJumpgate    = Wolf 359 -> Sol system



Identifying datablocks isn't difficult.
Also splitting up lines in the textfile (the part before and after the "="-sign).

You can get my sample code here (for the above textfile):
http://users.pandora.be/vge/Blitz/ReadingTextFiles.bb

NOTE: This file only containes the required functions for reading the file.
The custom types do not exist in there.

Also, the function "ReadTextFile" automatically skips non-recognized lines (so also comments and other useless junk, you don't even need the ";") within the file, as it only scans for "known" datablocks: [Jumpgate] and [Station]

The Jumpgate-type-instance is a global one in my game, with all fields set as in the function ReadDataBlock_Jumpgate.


slenkar(Posted 2004) [#15]
thanks guys thats good advice