including a variable

Blitz3D Forums/Blitz3D Beginners Area/including a variable

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?

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?

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.

"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


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.)

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.

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.

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

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.

i realize that, and am looking into it atm :)

here's the entire file, butcher it. I can't think of anything :(

Graphics3D 1024, 768, 32, 2
SetBuffer BackBuffer()
AutoMidHandle 1
Global size#=2
Global black = ReadPixelFast (1, 1)

Type bricks
	Field x
	Field y
	Field id
	Field w
	Field h
	Field cr
	Field cg
	Field cb
End Type

Type BombBrick
	Field x
	Field y
	Field w
	Field h
	Field id
	Field r
	Field cr
	Field cg
	Field cb
End Type

Type metalbrick
	Field x
	Field y
	Field id
	Field w
	Field h
End Type

Type superbrick 
	Field x
	Field y
	Field w
	Field h
	Field id
	Field phase
	Field cr
	Field cg
	Field cb
End Type

Type powerbrick
	Field x
	Field y
	Field w
	Field h
	Field cr
	Field cg
	Field cb
	Field id
	Field kind 
End Type

Type powerup
	Field x
	Field y
	Field kind
End Type

Global ColorR = 255
Global ColorG
Global ColorB
Global BrickType$ = "normal"
Global bomb = LoadImage("images\bomb.bmp")
MaskImage bomb, 255, 255, 255

Color 255, 255, 255
Repeat
Text 0, 0, "WELCOME TO THE LEVEL CREATOR"
Text 0, 14, "TO PROCEED, PRESS THE SPACEBAR"
Until KeyHit(57)
Cls 
load("testingfile")
Repeat
	RenderWorld()
	
	If MouseDown(1) And (Round(MouseY(), (size*10)+1))+((size*10)/2) < 600 Then
		For b.bricks = Each bricks
			If b\x = Round(MouseX(), (size*30)+1) And b\y = Round(MouseY(), (size*10)+1) Then 
				Delete b
				Exit
			EndIf  
			
		Next
		
		For bb.BombBrick = Each BombBrick
			If bb\x = Round(MouseX(), (size*30)+1) And bb\y = Round(MouseY(), (size*10)+1) Then 
				Delete bb
				Exit
			EndIf  			
		Next
		
		For mb.metalbrick = Each metalbrick
			If mb\x = Round(MouseX(), (size*30)+1) And mb\y = Round(MouseY(), (size*10)+1) Then 
				Delete mb
				Exit
			EndIf  
			
		Next
		
		For sb.superbrick = Each superbrick
			If sb\x = Round(MouseX(), (size*30)+1) And sb\y = Round(MouseY(), (size*10)+1) Then 
				Delete sb
				Exit
			EndIf  
		Next
		
		For pb.powerbrick = Each powerbrick
			If pb\x = Round(MouseX(), (size*30)+1) And pb\y = Round(MouseY(), (size*10)+1) Then 
				Delete pb
				Exit
			EndIf  
		Next
		If Instr (BrickType, "normal") Then CreateBrick(Round(MouseX(), (size*30)+1), Round(MouseY(), (size*10)+1), size, ColorR, ColorG, ColorB)
		If Instr (BrickType, "metal") Then CreateMetalBrick(Round(MouseX(), (size*30)+1), Round(MouseY(), (size*10)+1), size)
		If Instr(BrickType, "ghost") Then CreateGhostBrick(Round(MouseX(), (size*30)+1), Round(MouseY(), (size*10)+1), size)
		If Instr(BrickType, "bomb") Then 
			BrickType$ = Replace$ (BrickType$, "bomb ", "")
			CreateBombBrick(Round(MouseX(), (size*30)+1), Round(MouseY(), (size*10)+1), size, ColorR, ColorG, ColorB, Int BrickType)
			BrickType$ = "bomb " + BrickType$
		EndIf 
		If Instr(BrickType, "super ") Then 
			BrickType$ = Replace$ (BrickType$, "super ", "")
			CreateSuperBrick(Round(MouseX(), (size*30)+1), Round(MouseY(), (size*10)+1), size, Int BrickType, ColorR, ColorG, ColorB)
			BrickType$ = "super " + BrickType$
		EndIf
	EndIf
	
	If MouseDown(2) Then 
		For b.bricks = Each bricks
			If b\x = Round(MouseX(), (size*30)+1) And b\y = Round(MouseY(), (size*10)+1) Then 
				Delete b
				Exit
			EndIf  			
		Next
		
		For bb.BombBrick = Each BombBrick
			If bb\x = Round(MouseX(), (size*30)+1) And bb\y = Round(MouseY(), (size*10)+1) Then 
				Delete bb
				Exit
			EndIf  			
		Next
		
		For mb.metalbrick = Each metalbrick
			If mb\x = Round(MouseX(), (size*30)+1) And mb\y = Round(MouseY(), (size*10)+1) Then 
				Delete mb
				Exit
			EndIf  
			
		Next
		
		For sb.superbrick = Each superbrick
			If sb\x = Round(MouseX(), (size*30)+1) And sb\y = Round(MouseY(), (size*10)+1) Then 
				Delete sb
				Exit
			EndIf  
		Next
		
		For pb.powerbrick = Each powerbrick
			If pb\x = Round(MouseX(), (size*30)+1) And pb\y = Round(MouseY(), (size*10)+1) Then 
				Delete pb
				Exit
			EndIf  
		Next
	EndIf 
	
	If KeyHit(46) Then
		FlushKeys()
		Print "Input the color (RGB) values"
		ColorR = Input$("RED : ")
		ColorG = Input$("GREEN : ")
		ColorB = Input$("BLUE : ")
		Cls 
	EndIf
	
	If KeyHit(31) Then
		FlushKeys()
		Print "Input the brick size, current size is " + size + "."
		Print "SUGGESTION : do not make size under 0.2 or over 4"
		Print "as the size is how many times the orginal dimensions (30 x 10)"
		size# = Float Input$("SIZE : ")
		Cls 
	EndIf
	
	
	If KeyHit(20) Or (Instr(BrickType$, "normal") = False And Instr(BrickType$, "metal") = False And Instr(BrickType$, "ghost") = False And Instr(BrickType$, "super") = False And Instr(BrickType$, "bomb") = False) Then
		
		FlushKeys()
		Print "Enter the new kind of brick"
		Print "'normal' for regular bricks"
		Print "'metal' for metal bricks"
		Print "'ghost' for ghost bricks"
		Print "for bomb bricks, type 'bomb ' (with the space) then the explosion radius"
		Print "for super bricks, type 'super ' (with the space) then the number of phases"
		BrickType$ = Input$("Brick Type : ")
		BrickType$ = Lower$(BrickType$)
		Cls 
		FlushKeys()
	EndIf
	
	drawbricks()
	Color 255, 255, 255
	
	Text 512 - (StringWidth("PRESS T TO CHANGE BRICK TYPE")/2), 640, "PRESS T TO CHANGE BRICK TYPE"
	Text 512 - (StringWidth("PRESS S TO CHANGE SIZE")/2), 660, "PRESS S TO CHANGE SIZE"
	Text 512 - (StringWidth("PRESS C TO CHANGE COLOR")/2), 680, "PRESS C TO CHANGE COLOR"
	Text 512 - (StringWidth("PRESS ESCAPE TO SAVE AND QUIT")/2), 700, "PRESS ESCAPE TO SAVE AND QUIT"
	Line 0, 600, 1024, 600
	Flip
	Cls 
	
Until KeyHit(1)

name$ = Input$("Filename (enter nothing to not save)  :  ")
If name <> "" Then Save(name$)

End 

Function Round( Number , N )

   If ( Number Mod N ) >= ( N *.5 )
        Number  = ( Ceil( Number / N ) + 1 ) * N
   Else
        Number = Floor( Number / N ) * N
   EndIf

   Return Number

End Function

Function CreateSuperBrick(x, y, s#, phases, cr, cg, cb)
	SB.superbrick = New superbrick
	sb\x = x
	sb\y = y
	sb\w = s*30
	sb\h = s*10
	sb\cr = cr
	sb\cg = cg
	sb\cb = cb
	sb\phase = phases
	If sb\phase > 5 Then sb\phase = 5
	sb\id = CreateImage(sb\w, sb\h)
	SetBuffer ImageBuffer(sb\id)
		Color cr, cg, cb
		Rect 0, 0, sb\w, sb\h
	SetBuffer BackBuffer()
End 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 

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

Function CreatePowerBrick(x, y, s#, cr, cg, cb, kind=0)
If kind = 0 Then

	pb.bricks = New bricks
	pb\x = x
	pb\y = y
	pb\w = s*30
	pb\h = s*10
	pb\id = CreateImage(pb\w, pb\h)
	SetBuffer ImageBuffer(pb\id)
		Color cr, cg, cb
		Rect 0, 0, pb\w, pb\h
		If cr < 255 And cg < 255 And cb < 255 Then
			Color 255, 255, 255
		Else
			Color 0, 0, 0
		EndIf 
		Text 2, 2, "SUPERBALL"
		
	SetBuffer BackBuffer()
	
EndIf 

Color 255, 255, 255
End Function 

Function CreateMetalBrick(x, y, s#)
	mb.metalbrick = New metalbrick
	mb\x = x
	mb\y = y
	mb\w = s*30
	mb\h = s*10
	mb\id = CreateImage(mb\w, mb\h)
	SetBuffer ImageBuffer(mb\id)
		Color 100,100,100
		Rect 0, 0, mb\w, mb\h
	SetBuffer BackBuffer()
End Function

Function CreateBombBrick(x, y, s, cr, cg, cb, r)
	bb.BombBrick = New BombBrick
	bb\x = x
	bb\y = y
	bb\w = s*30
	bb\h = s*10
	bb\r = r
	bb\cr = cr
	bb\cg = cg
	bb\cb = cb
	ResizeImage bomb, bb\w, bb\h
	bb\id = CreateImage(bb\w, bb\h)
	SetBuffer ImageBuffer(bb\id)
		Color cr, cg, cb
		Rect 0, 0, bb\w, bb\h
		DrawImage bomb, bb\w / 2, bb\h / 2
	SetBuffer BackBuffer()
End Function

Function DrawBricks()
	For b.bricks = Each bricks
		DrawImage b\id, b\x, b\y
	Next
	
	For mb.metalbrick = Each metalbrick
		DrawImage mb\id, mb\x, mb\y
	Next
	
	For sb.superbrick = Each superbrick
		DrawImage sb\id, sb\x, sb\y
	Next
	
	For bb.bombbrick = Each bombbrick
		DrawImage bb\id, bb\x, bb\y
	Next
	For pb.powerbrick = Each powerbrick
		DrawImage pb\id, pb\x, pb\y
	Next
End Function

Function Load(filename$)

	
	file = OpenFile("maps\testingfile.bb")
	
			brickcount = Int ReadLine(file)
			If brickcount > 0 Then 
			For n = 0 To brickcount
				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
			EndIf 
			brickcount = Int ReadLine(file)
			If brickcount > 0 Then 
			For n = 0 To brickcount - 1
				x = Int ReadLine(file)
				y = Int ReadLine(file)
				s# = Float ReadLine(file)
				Local phase = Int ReadLine(file)
				cr = Int ReadLine(file)
				cg = Int ReadLine(file)
				cb = Int ReadLine(file)
				If s > 0.0 Then CreateSuperBrick(x, y, s, phase, cr, cg, cb)
			Next
			EndIf 
			
		
	CloseFile file

End Function

Function countbricks()
	For b.bricks = Each bricks
		count = count + 1
	Next
	Return count 
End Function



Function Save(filename$)

	file = WriteFile("maps"+filename$+".bb")
	file = OpenFile("maps"+filename$+".bb")
	
		WriteLine file, countbricks()
		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
		
		For sb.superbrick = Each superbrick
		count = count + 1
		Next
		WriteLine file, count 
		For sb.superbrick = Each superbrick
			WriteInt file, sb\x
			WriteInt file, sb\y
			WriteFloat file, sb\w / 30
			WriteInt file, sb\phase
			WriteInt file, sb\cr
			WriteInt file, sb\cg
			WriteInt file, sb\cb
		Next
	;	WriteLine file, "METALBRICKS"
		For mb.metalbrick = Each metalbrick
			WriteLine file, mb\x
			WriteLine file, mb\y
			WriteLine file, mb\w / 30
		Next
	;	WriteLine file, "POWERBRICKS"
		For pb.powerbrick = Each powerbrick
			WriteLine file, pb\x
			WriteLine file, pb\y
			WriteLine file, pb\w / 30
			WriteLine file, pb\cr
			WriteLine file, pb\cg
			WriteLine file, pb\cb
			WriteLine file, pb\kind
		Next
	;	WriteLine file, "BOMBBRICKS"
		For bb.BombBrick = Each BombBrick
			WriteLine file, bb\x
			WriteLine file, bb\y
			WriteLine file, bb\w / 30
			WriteLine file, bb\cr
			WriteLine file, bb\cg
			WriteLine file, bb\cb
			WriteLine file, bb\r
		Next 
	
	CloseFile file 
End Function 


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.

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

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 :)