Changing levels?

Blitz3D Forums/Blitz3D Beginners Area/Changing levels?

Cubed Inc.(Posted 2010) [#1]
I have a question.

What technique would you use to "change levels" or "worlds"in Blitz3d.
I really don't know how to do it. By the way, if you can make it so the level or world's music changes too, that would be a plus:)


please reply.later.


Matty(Posted 2010) [#2]
This really comes down to how you design your game at the beginning.

A simple explanation of one method to use would be as follows:

In your game code, break the logic into these components:

1 [Initialise Level]

2 [Play Game]

3 [Deinitialise Level]

In step 1 you load all of your world, making sure to store all references to entity handles etc so that you can keep track of them throughout step 2.

Step 2 is your game play section where all the action takes place (usually broken into further sub functions such as 'getinput','update game/world','drawworld' etc

Step 3 is where you free all object handles and reset the situation back to where it was before step1.

Then repeat steps 1-3 over and over....

Step 1 may involve reading from a text file / data file all your world information for the relevant level.

This is a fairly basic method.


Cubed Inc.(Posted 2010) [#3]
well,is there some sort of an example that I can see? Also, would it be possible to use the include function for levels aswell?


Dreamora(Posted 2010) [#4]
levels are normally stored in external files that contain the data. code based level setup are a horror to maintain


Zethrax(Posted 2010) [#5]
The 'Include' functionality is useless for levels, unless you're hard coding them.

Included code is statically compiled into the executable, rather than being dynamically integrated as you would get with a data driven approach.


PowerPC603(Posted 2010) [#6]
I'm using this in my Arkanoid clone.
The code is free, but a bit large.

http://users.telenet.be/vge/Arkanoid3D/Arkanoid3D.zip

But it clearly has 3 different functions:
LoadLevel(Level)
PlayLevel()
EndLevel()


The "Level" variable is a global variable to keep track of the current level that you're playing and is passed to the "LoadLevel" function.
LoadLevel does what it's name indicates: it loads the correct level-file, and based on the data in that file, the entire level is built (blocks are created, the player paddle is created, lights, the balls, ...) and all this information (like entity handles) are stored in type instances.

PlayLevel (the main loop for playing the game) is called when the level is completely setup.

Then finally, EndLevel loops through all type instances, freeing entities (all of them, even the player paddle and the balls) and the type instances too.
So after EndLevel, the entire 3D world is clean. No entities remain and no type instances.

For loading the next level, the game just increases the "Level" variable by 1, passes this variable to LoadLevel again to load the next level and you're ready to play level 2.

This goes on until there are no more levels.


Cubed Inc.(Posted 2010) [#7]
well, I did try using the include functions to change the levels completely. Also, in the Blitz3d game samples, the game WingRing used the include function to store the menu, the single-player and muliplayer game. So are you guys sure that you can't use the include functions?


Yasha(Posted 2010) [#8]
Include is not a function. It's a preprocessor command and will never even be encountered by the compiler, much less the Blitz3D engine.

The Blitz3D compiler only handles single translation units (to use C terminology, without a warrant). What this means is that whatever you pass to the compiler will first be processed in such a way that it's effectively one long text file, no matter how many .BBs you broke it up into on disk.

This is what Include does: it takes the contents of the specified file and performs a glorified copy/paste operation into your main file. You can call Include with the same parameter more than once, and the same text will be pasted more than once. You can even be really messy and use it to close loops or whatever (if you really want to make it hard to code).

What this means is that Include has absolutely no effect on your finished program other than dumping more code into the file; you can't use it with a condition check any more than you can condition-check yourself pressing Ctrl-V. In the game WingRing, and other big projects, it's simply used to store related code in separate blocks so that the files don't get too big to easily navigate.

Now that said, there's nothing to stop you writing code to load or generate a level, and putting it in its own separate .BB file (that's actually a good idea), so long as you realise that the Include command isn't what's doing the work. Ideally (for the purposes of organisation) it's best to have only one "main" file, and have the contents of all the others be purely function/type/constant libraries, so you can easily see the path of execution your program takes.

In short, Include is a text editing command and has absolutely nothing to do with any aspect of program execution, at all.


Cubed Inc.(Posted 2010) [#9]
well, I'm having trouble understanding. What would be the upmost easiest way to do this. I know that i'm being a real pain, but the ability to change levels is required for me to make my games. You guys give great examples and explainations, but I just want to know what way would be the easiest.


Yasha(Posted 2010) [#10]
Which method would be best depends a lot on the structure of your game. What style/genre are you going for? There's more than one way to solve the problem; the one you choose is dependent on how you want to organise your game in the first place (ie. you wouldn't use the same system for a game based around self-contained chapters (Thief, Hitman) as you would for a massive, free-roaming outdoor world (Elder Scrolls), or a prerendered-panel-interactive-movie (Final Fantasy 7)).


Cubed Inc.(Posted 2010) [#11]
Yasha

well,the game that i'm trying to learn to make is a platformer/action adventure. have you ever heard of the game psychonauts? If so, it's kind of like that when it comes to the levels and genre.


PowerPC603(Posted 2010) [#12]
Include simply loads the specified file and pastes the contents of the file where the Include command is located.

If you were to have this "Main.bb" file:
Include "CodePart1.bb"
Include "CodePart2.bb"

InitGraphics()

While Not KeyHit(1)
	Render()
Wend

End


This is the "CodePart1.bb" file:
Function InitGraphics()
	Graphics3D 800, 600, 0, 2
	SetBuffer BackBuffer()
End Function


And this is "CodePart2.bb":
Function Render()
	RenderWorld

	Text 10, 10, "Some text"

	Flip
End Function


The above "Main.bb" file would include "CodePart1.bb" and "CodePart2.bb" into "Main.bb".

What's the result (internally)?
Function InitGraphics()
	Graphics3D 800, 600, 0, 2
	SetBuffer BackBuffer()
End Function

Function Render()
	RenderWorld

	Text 10, 10, "Some text"

	Flip
End Function

InitGraphics()

While Not KeyHit(1)
	Render()
Wend

End


This last piece of code is the final result of the Include commands.
This is what's effectively sent to the compiler, which creates the .exe file.
You see that there is no Include command left for the compiler to work with, as the preprocessor has replaced 'Include "CodePart1.bb"' and 'Include "CodePart2.bb"' with the contents of the files.


Cubed Inc.(Posted 2010) [#13]
Thanks for the explaination:) How can I use this code to make the levels change?


Cubed Inc.(Posted 2010) [#14]
PowerPC603

Your code was very usefull. I think that i'm starting to understand it more.


Kirkkaf13(Posted 2010) [#15]
I think the help file in blitz pretty much covers include.

You ask "how can i use this code to make the levels change?" well to complete a level in any game some condition must be met once this condition is met you reach a new level.

You can use a select, case, to determin what level is loaded if case = 1 then level 1 is loaded if condition e.g score = 1000 then case = 2 load level 2 data.

You can then store different level data in different .bb files using include to load the .bb file.

If in anyway I am wrong here someone please correct me.

OneHitWonder


Oiduts Studios(Posted 2010) [#16]
For an easy explanation that is good for a start. Later you can go into more advanced methods. Remember there are MANY ways to do this like everyone said, you will start to develop your own style.




jfk EO-11110(Posted 2010) [#17]
Using Include for new levels might be rather "experimental", hope you don't mind.

You' re right about the condition. Here's a very simple example:

level=1
re=readfile("leveldata.xyz")
while (EOF(re)=0) and (mousedown(1)=0)
 z%=readline(re)
 cls:locate 0,0
 print "Level: "+level
 print "Guess a number from 0 to 9"
 repeat
  numstring$=input("Guess: ")
  z_guess=int(numstring$)
  if z_guess = z then ; our condition to reach the next level
   print "Correct! Level complete"
  else
   print "Wrong...try again!"
  endif
  delay 1000
 until (z_guess=z) or (mousedown(1)<>0)
 level=level+1
wend
closefile re


(just written, not tested) Before you can run this, you need to create a file named leveldata.xyz. Make it with notepad and write a number from 0 to 9 on each line, for about 10 or so line:

1
5
3
7
9
3
5
5
9
2

You may also create it as a standard .TXT textfile with notpad, then rename it accordingly later.

The program will then open the file and read one line every level. A more flexible way is to use one file for every level. You can even use the level number as a name convention:

for i=0 to 9
re=readfile(""+i+".xyz")
closefile re
next
This will open 0.xyz to 9.xyz. Here you can store a lot of information in each file. Paths of meshes that should be loaded, locations to position them, scale, rotate, etc. It's up to you to design a file format that allows you for example to load a number of objects, described in your level file. Some people prefer the XML langage to make their level files. I am using something homebrew that makes use of Linebreaks and ReadLine. It's up to you.