Parseing a dat\txt file

Blitz3D Forums/Blitz3D Beginners Area/Parseing a dat\txt file

Aussie(Posted 2010) [#1]
Gday everyone.

I have been following a tutorial on How to parse a txt file.
http://www.blitzbasic.com/Community/posts.php?topic=27896#292641 and have run into problems.

Here is what the dat file looks like

[W1_L1]
meshtype=Asteroid_1
x=50
y=25
xv=.2
yv=.1
rotx=2
roty=1
rotz=2
life=10
alpha=.1
order=-1
meshtype=Asteroid_1
x=-10
y=-32
xv=-.1
yv=-.2
rotx=1
roty=2
rotz=1
life=10
alpha=.1
order=-2
meshtype=Asteroid_2
x=10
y=15
xv=.01
yv=.2
rotx=1
roty=2
rotz=3
life=2
alpha=.1
order=-3
[Blah]
something
something
something
[Blah]
.
.
.
.


And here is the code.


The problem is that it dosn't seem to reconise the line [W1_L1].

I have changed the first foundit value to 1 just to test the code & have found it to read through the dat file without a problem. But with it set to 0 it won't read the file.

Any help would be greatly apreciated.
Cheers : )


Ross C(Posted 2010) [#2]
I would have thought that:

  If foundit = 1
    done = False 


would have caused an error, as you not closing it with an End If.


Aussie(Posted 2010) [#3]
Edit: Thanks Ross but the i'm prety sure "End If" is the last "End If" in the code. You posted as i was posting.

Ok i have got it working.

I just changed
If Lower(ReadLine$(filein))="[W1_L1]" Then foundit = 1

to
If ReadLine$(filein)="[W1_L1]" Then foundit = 1

Now i am using this to Parse the values into Types and i keep geting "entity dose not exist"

If i use this.

It works.
This is what the .dat file for this one looks like.
Asteroid_1
50
25
.2
.1
2
1
2
10
.1
-1
Asteroid_1
-10
-32
-.1
-.2
1
2
1
10
.1
-2
Asteroid_2
10
15
.01
.2
1
2
3
2
.1
-3

But if i use this to read from the .dat file in the first post.

It dosn't work.

I don't understand why it is working with the first Loading function i used but not the second one. Both codes are doing prety much the same thing, only one is searcing for "key words" & the other one is just reading values straight into the Types.
Cheers : )

Edit: I have just realized that it is creating a new Type & trying to position & set an entitys order every time the loop is repeated. So i have now got a "key word" set for it to create a new Type & set its position & order.
If the loop finds "NEW" a new type is created.
If the loop finds "FINISH" the position & order is set.
But i still keep geting "Entity dose not exist" at position entity????


Aussie(Posted 2010) [#4]
Got it working. The reason i was getting "entity dose not exist" was the case select.
i changed this.
Select e\meshtype$
				Case e\meshtype$ = Lower("Asteroid_1")
						e\mesh = CopyEntity(cube)
							
				Case e\meshtype$ = Lower("Asteroid_2")
						e\mesh = CopyEntity(cube1)


to this
Select e\meshtype$
				Case  Lower("Asteroid_1")
						e\meshtype$ = "Asteroid_1"
						e\mesh = CopyEntity(cube)
							
				Case  Lower("Asteroid_2")
						e\meshtype$ = "Asteroid_2"
						e\mesh = CopyEntity(cube1)
							
		End Select



Floyd(Posted 2010) [#5]
The first problem was caused by
  If Lower(ReadLine$(filein))="[W1_L1]" Then foundit = 1

because Lower( something ) converts to lower case. The result can never match [W1_L1].

You could have used Upper() or compared with [w1_l1].
Or done neither if you are confident of the case of [W1_L1] in the file.


_PJ_(Posted 2010) [#6]
Similaryl tis is somewhat unnecessary:

to this

Select e\meshtype$
				Case  Lower("Asteroid_1")
						e\meshtype$ = "Asteroid_1"
						e\mesh = CopyEntity(cube)
							
				Case  Lower("Asteroid_2")
						e\meshtype$ = "Asteroid_2"
						e\mesh = CopyEntity(cube1)
							
		End Select


Essentially each "Case" statement is a way of checking the argument presented with Select...

Select e\meshtype$
Case Lower("Asteroid_1")
					e\meshtype$ = "Asteroid_1"
						e\mesh = CopyEntity(cube)
is equivalent to:

If (e\meshtype$=Lower("Asteroid_1")) Then
						e\meshtype$ = "Asteroid_1"
						e\mesh = CopyEntity(cube)
End If


Since your case argument is known (what you are checking for), then there should be no need for the Lower() command, instead, you can enter this manually. You MAY instead be wanting to ensure that your unknown string variable, e\meshtype$ is converted to lower case for the check:

Select (Lower$(e\meshtype$))
Case "asteroid_1"


Then, you are reassinging the variable e\meshtype with the name you have pretty much just checked for, so it may be unnecessary, or at least, by ensuring it is NOW in lowercase will prevent any further reason to distinguish the case.
e\meshtype$ = "asteroid_1"


This entire procedure could be made much easier by placiong the Lower() command at the point of whenever e\meshtype$ is first defined.
temp$ = ReadLine(filein)
					e\meshtype$ = Lower(temp$)

This way, you can be sure that e\meshtype$ will ALWAYS be lower case in future.

Lastly, there's
						e\mesh = CopyEntity(cube)

I couldn't see any other reference to "cube" as an entity, so make sure that this exists and, is either passed to the load_level function, or is global.