including a variable

Blitz3D Forums/Blitz3D Beginners Area/including a variable

chwaga(Posted 2007) [#1]
I'm trying to load levels in a game with the include command, but you can't include a variable, apparently. Is the a way to declare as a constant each file within a folder?


Ross C(Posted 2007) [#2]
Not quite sure what you mean. Do you mean:


a$ = "playercontrol.bb"

Include a$


Not sure why you'd need to do that :o) Can you explain further?


Stevie G(Posted 2007) [#3]
Do you mean something like ....

if LevelNumber = 1 include "Level1.bb"
if LevelNumber = 2 include "Level2.bb"

If so then you need to understand better how the include command works as it's not designed for this purpose.


jhocking(Posted 2007) [#4]
"Include" is a static thing done at compile time, not a dynamic change at run time. To load separate levels during the game, define all the setup information for your levels in some sort of data file (the simplest would be a text file that lists the level mesh and the locations of all the enemies) and have a function load that data file and place all the meshes and what not according to that data (lookup ReadLine.) Then pass a variable to that function to define exactly which level file to load.

Something like:
LoadLevel("Level"+levelnum+".lvl")

Function LoadLevel(filename$)
  ClearWorld
  file=OpenFile filename
  level$=ReadLine file
  LoadMesh(level+".b3d")
;repeat previous two lines for rest of data
  CloseFile file
End Function



chwaga(Posted 2007) [#5]
the maps are designed like so :

CreateBrick(427, 588, 2, 255, 0, 0)
CreateBrick(183, 588, 2, 255, 0, 0)
CreateBrick(549, 588, 2, 255, 0, 0)
CreateBrick(793, 588, 2, 255, 0, 0)
CreateBrick(732, 588, 2, 255, 0, 0)

;....etc.

which is a real function in my game, could i read from a data file, and convert it into code?

or, does readline go to the next line every time it's called, so I could to do

repeat 
     CreateBrick(int readline(level), int readline(level), float readline(level), int readline(level), int readline(level), int readline(level))
until readline$(level) = "METALBRICKS"


and have each "brick" in the level file be written like so:

732
588
2
255
0
0


and so on? (there are multiple kinds of bricks, they would be written in a specific order, normal, metal, etc.)


jhocking(Posted 2007) [#6]
That second way is the right idea, although the way you have it written is cumbersome. Instead of putting ReadLine directly in the call to CreateBrick, I would read the numbers into variables and use those variables with CreateBrick.


chwaga(Posted 2007) [#7]
excactly what i did, but it still doesnt work, here's the code:

the saving function
Function Save(filename$)

file = WriteFile("maps\"+filename$+".bb")
file = OpenFile("maps\"+filename$+".bb")	
	For b.bricks = Each bricks
		WriteLine file, b\x
		WriteLine file, b\y
		WriteLine file, b\w / 30
		WriteLine file, b\cr
		WriteLine file, b\cg
		WriteLine file, b\cb
	Next
CloseFile file 
End Function

the loading function
Function Load(filename$)

	
	file = OpenFile("maps\"+filename$+".bb")
	
		For n = 0 To Eof(file)
			Local x = Int ReadLine(file)
			Local y = Int ReadLine(file)
			Local s# = Float ReadLine(file)
			Local cr = Int ReadLine(file)
			Local cg = Int ReadLine(file)
			Local cb = Int ReadLine(file)
			CreateBrick(x, y, s, cr, cg, cb)
		Next
	CloseFile file

End Function

the actual output of the saving function :
427
315
2
255
0
0


and finally, the CreateBrick function:

Function CreateBrick(x, y, s#, cr, cg, cb)
	b.bricks = New bricks
	b\x = x
	b\y = y
	b\w = s*30
	b\h = s*10
	b\cr = cr
	b\cg = cg
	b\cb = cb
	b\id = CreateImage(b\w, b\h)
	SetBuffer ImageBuffer(b\id)
		Color cr, cg, cb
		Rect 0, 0, b\w, b\h
	SetBuffer BackBuffer()
End Function 





the problem is that when loading this, i get an error message in the function CreateBrick(). On the command "SetBuffer ImageBuffer(b\id)", I get the error message: "Image does not exhist!".

the function works fine when called with non-read variables.


chwaga(Posted 2007) [#8]
more on it, it actually works when using int readline, instead of readint (same with the writing function).

it works perfectly when using just one type of brick, but when I try loading different types of bricks, using the :

repeat
;load stuff
until readline(file) = "SUPERBRICKS"


this causes a: IMAGE DOES NOT EXHIST


jhocking(Posted 2007) [#9]
That pseudocode in your second post is not nearly enough for us to tell what is going on. Neither the saved data nor the function you posted before make any reference to "SUPERBRICKS" so we need to see exactly what both your save file and function look like now.

I can tell you this much: I am fairly certain the problem is that the data is getting mixed up while you are reading it (eg. line saved into wrong variable) and as a result "s" in CreateBrick is being set to 0.

Just work backwards from the error message: The error is that the image does not exist, but you called CreateImage on the previous line. Well, CreateImage won't work if the dimensions are 0. The dimensions are 0 if s is set to 0. s could be set to 0 if the data is loaded in wrong.


chwaga(Posted 2007) [#10]
i realize that, and am looking into it atm :)


chwaga(Posted 2007) [#11]
here's the entire file, butcher it. I can't think of anything :(




jhocking(Posted 2007) [#12]
Well it looks like the number of times you loop is different from the number of bricks counted. Maybe try counting from 1 in your load function (For n=1 To brickcount)

If that doesn't work, I would suggest throwing a Break into your load function and run in debug mode so that you can see what the variables are being set to.


Stevie G(Posted 2007) [#13]
Your save function looks dodgy, not sure how much an effect it would have but ...

Rather than ...

file = WriteFile("maps\"+filename$+".bb")
file = OpenFile("maps\"+filename$+".bb")

Just use ..

file = WriteFile("maps\"+filename$+".bb")

How is the data arranged in your .bb files? Can you show us an example?

Stevie


chwaga(Posted 2007) [#14]
as it turns out, like 90% of my bugs, it was a typo, if you noticed I was using writeint into a txt file, giving all these random symbols of nonsense.

thanks for the help :)