Includes and Excludes

Blitz3D Forums/Blitz3D Beginners Area/Includes and Excludes

wizzlefish(Posted 2004) [#1]
I have a menu file, "menu.bb," and I have a actual game file, "mainloop.bb," and when you run the menu, if you click on "Start Game," it goes to the command "Include 'mainloop.bb.'" And, if you press Escape while running "mainloop.bb," it directly includes "menu.bb." So both of these files are including each other, and I get all kinds of errors:
"Function can only appear in main program"
"Global can only appear in main program"
ETC.

Is there a way to fix this and pack my menu and game into one file?


Gabriel(Posted 2004) [#2]
Includes should not be conditional and embedded in functions or whatever. Includes should be constant and at the beginning of your program. The concept of an include is that it's exactly the same as putting all of that code into the one source file, not that you can choose to include or exclude things based on what happens at run time.


_PJ_(Posted 2004) [#3]
Yes. Maintain a main program file, even if all it consists of is lines of INCLUDEs.

i.e:


Include "Menu.BB"
Include "MainLoop.BB"
Inlcude "EndGame.BB"
Include "Highscores.BB"

etc.


TomToad(Posted 2004) [#4]
I believe you are using Include incorrectly. When you "Include" a file, you are basically putting the source where the include is. So do something like this:
;Main.bb
Include "Menu.bb"
Graphics 800,600
While Not KeyHit(1)
If KeyHit(MENU_KEY)
 a = Menu()
 Print "You Pressed " + a
Wend
End

;Menu.bb
Const MENU_KEY = 59

Function Menu()
Repeat
Print "1) Do Nothing"
Print "2) Don't do anything"
Print "3) Nothing done here"

a = Input("Your Choice? "
Until a > 0 And a < 4
Return a
End Function

Then you compile Main.bb. The compiler, when it compiles will see Main.bb as though you had written everything into one source code like so:
;Main.bb
;Menu.bb
Const MENU_KEY = 59

Function Menu()
Repeat
Print "1) Do Nothing"
Print "2) Don't do anything"
Print "3) Nothing done here"

a = Input("Your Choice? "
Until a > 0 And a < 4
Return a
End Function
Graphics 800,600
While Not KeyHit(1)
If KeyHit(MENU_KEY)
 a = Menu()
 Print "You Pressed " + a
Wend
End

Inserting Menu.bb right where the "Include" was in Main.bb


wizzlefish(Posted 2004) [#5]
So "Function Menu()" is a While....Wend loop?


TomToad(Posted 2004) [#6]
In my example it is a Repeat Until loop. Actaully it pauses while it waits for an input, then loops back to the start if the input is less than 1 or greater than 3.

Of course, my example is really useless in the real world. It was merely used to demonstrate how Blitz compiler sees the source when you use Include.


wizzlefish(Posted 2004) [#7]
ok