data from text

Blitz3D Forums/Blitz3D Programming/data from text

Eviltoes(Posted 2007) [#1]
is there a way to read data from a text file? I am trying to make a maze that is loaded from lines of values read from a text file. But i get a 'Data values must be constant' error. Unless there is a way to fix this, I will only be able to have one level! this is very bad.

I tried using "data readline(file)" but this didn't work, does anyone have any ideas?

Any help is greatly appreciated.


_33(Posted 2007) [#2]
Hi Eviltoes,

You have many choices when using data in files... You can either: ReadByte, ReadFloat, ReadInt, ReadLine or even ReadString!

So, you're definately not out of luck. You just need to wrap your head around those commands when reading your level data. Why not make the level files with writing the data into a file using, say "WriteString" and then when you have the game playing, do a ReadString to read that data?

It's really not that trivial IMHO. It's almost like a rooky question.


Gabriel(Posted 2007) [#3]
To answer the actual question, just forget about the data command, it's completely irrelevant here. ReadLine is a function. Just assign the return to a variable

a$=Readline(MyFile)


And make sure you opened the file first with

MyFile=ReadFile("MyFile.txt")



Eviltoes(Posted 2007) [#4]
I am trying to make it easy for a person to type lines of values into notepad/wordpad and load this into a program, which then converts it into a 3d maze. How would I do this without data lines?


Stevie G(Posted 2007) [#5]
A simple implementation ...

Create this in Notepad ... and save as "Level1.txt". Note that the first 2 values are maze width and height - you may no need these.

10
10
1111111111
1000200001
1110000111
1010000001
1010111011
1003010001
1000010301
1110000001
1000000001
1111111111


Use this to load it. Obviously the display function is just for testing. You may not need to store the values of each maze tile but I'm sure you'll figure out what to do from here.

Stevie


Graphics 640,480,16,1

Dim Tile(0,0)
Global SizeX, SizeZ

MAZEload( "Level1.txt" )

SetBuffer BackBuffer()
MAZEdisplay()
Flip
MouseWait
End


Function MAZEload( filename$ )

	file = ReadFile( filename$ )
	If file <> 0
	
		SizeX = ReadLine( file )
		SizeZ = ReadLine( file )
		
		Dim Tile( SizeX-1 , SizeZ-1 )
	
		For z = 0 To SizeZ - 1
		
			tmp$ = ReadLine( file )
			
			For x = 0 To SizeX - 1
				
				Tile( x, z ) = Mid( tmp, x+1 , 1 )	
			
			Next
			
		Next
		
	EndIf
	
	CloseFile file
	
End Function

;==============================================================================
;==============================================================================
;==============================================================================

Function MAZEdisplay()

	For z = 0 To SizeZ-1
		For x = 0 To SizeX-1
			Text 100+x*20, 100+z*20, Tile( x, z )
		Next
	Next
	
End Function



PowerPC603(Posted 2007) [#6]
I've got a nice package for you:
http://users.telenet.be/vge/downloads/MazePack.zip

This is a complete package which I've written to generate mazes as 2D bitmaps so you can print them on paper to solve for yourself.
Afterwards I've written a converter to convert that bitmap into a datafile which declares at which location a certain maze-part is located (a straight corridor, a corner, ...).
Finally Maze3D loads this datafile and generates a 3D maze, through which you can walk (just moving a camera).

In the zip-file are 4 directories.
The "MazeGenerator" dir holds all files (including full source-code) which make up the maze-generator, which generates the bitmap.
The "MazeConverter" dir holds all files (including full source-code) which make up the converter (which reads the bitmap and creates the datafile).
The "Maze3D" dir holds all files (including full source-code) which make up the 3D-maze, which loads the datafile and generates the 3D maze as one big mesh.
Finally, the "MazePack" holds all binaries in one dir, because the binaries load and save their datafiles in their own dir (they're just put together so no copying/moving files).

Use it like this:
Go into the MazePack dir and run MazeGenerator.exe.
The generator asks for the size of the maze (use values from 5 to 50). Going over 50 is possible, but Maze3D might generate an MAV, because the maze gets to big (over 65k vertices). You should now have a file Maze.bmp in the dir.

Then use MazeConverter.exe.
This examines the bitmap and generates code and saves it as Maze.dat (which can be read in Notepad).

Then use Maze3D.exe and have fun collecting all the coins.
There are as many coins as the size of the maze.
If you created a maze of size 50, then there will be 50 coins to collect at random locations.
You can disable gravity by pressing spacebar and you can go up/down using z/s respectively, so you can view the maze from above.
It also has collisions built-in.
It's not optimized to reduce polycount by merging walls and such, I haven't been able to figure that out yet.

If you're searching for the code which reads the Maze.dat file, look into Maze3D\Includes\ReadMazeData.bb.