something.lvl

Blitz3D Forums/Blitz3D Programming/something.lvl

ala_samodi(Posted 2004) [#1]
how could i make a *.lvl file. create it and read it. every once in a while i would run into a blitz3d game that has *.lvl's. I want the players of my game beable to make there own levels and create new features (coding) within the game.


jhocking(Posted 2004) [#2]
Look at the File commands in the 2D section of the Command Reference. You'll need to write a level editor.


ala_samodi(Posted 2004) [#3]
well how can i get this working??

areas = WriteFile("areas.kio")
WriteInt( areas, dine=LoadMesh( "dine.3ds" )
ScaleEntity dine,3,3,3
PositionEntity dine,0,0.2,0
EntityAutoFade dine,70,140

hall=LoadMesh( "hall.3ds" )
ScaleEntity hall,3,3,3
PositionEntity hall,0,0.2,0
EntityAutoFade hall,70,140
)

CloseFile( areas )

;the kio is my file type


jhocking(Posted 2004) [#4]
What's up with the parentheses? I can't tell what you're trying to do. But then it doesn't really matter, because pretty much all that stuff you should be doing apart from the file handling stuff. Scaling and positioning entities has nothing to do with file IO after all.

Use WriteString or WriteLine to write out the object's filename. Then when you read the file back, pass that filename to LoadMesh to load the model. Something like:

(writing the level file)
level=WriteFile("level.lvl")
WriteLine(level,"object.3ds")
CloseFile(level)

(loading the level)
level=ReadFile("level.lvl")
lvlMesh$=ReadLine(level)
LoadMesh(lvlMesh)
CloseFile(level)


ala_samodi(Posted 2004) [#5]
the errors say mismatched brackets


jfk EO-11110(Posted 2004) [#6]
Seems like you are trying to write multiple Lines to a file, as seen in PHP or Pearl. In Blitz this doesn't work, Especially not with writeInt. WriteInt writes a single 32 Bit Number value to a file. That's why it says Bracket problem, you got 3 Brackets on one line, but the number of brackets always needs to be even and there need to be as many opening brackets as closing brackets,
like a = ((( b+2 )))

Anyway, if you really want to write Blitz Code to a file, then you'll need to include it in a sourcecode later. You would then have to do it this way:

areas = WriteFile("areas.kio") 
Writeline areas, "dine=LoadMesh( "+chr$(34)+"dine.3ds"+chr$(34)+")"
Writeline areas, "ScaleEntity dine,3,3,3 "
Writeline areas, "PositionEntity dine,0,0.2,0 "
Writeline areas, "EntityAutoFade dine,70,140" 

Writeline areas, "hall=LoadMesh( "+chr$(34)+"hall.3ds"+chr$(34)+" )"
Writeline areas, "ScaleEntity hall,3,3,3"
Writeline areas, "PositionEntity hall,0,0.2,0"
Writeline areas, "EntityAutoFade hall,70,140"


CloseFile( areas ) 


as you can see, the strings you want to write to a file need to be enclosed by "". When there is a " inside a string you want to save, you need to encode it this way:
"+chr$(34)+"

But as far as I see, you lvl File should serve an other purpose, you want to load a level, right? So maybe you better only save the parameters of the commands, and not the commands.

Instead of writing the loading and scaling rigth into the code, you maybe better make an editor that can open a fileselector, then loads a mesh and lets you position and scale it. Then you can add a function that will save all mesh filepaths, locations, scaling values etc. to a file. Then you can create a loader that is reading that file and restores the world as you have saved it. Creating a loader from a saver is pretty simple, mainly just alter the WriteLine to ReadLine.


WolRon(Posted 2004) [#7]
By the way, there is absolutely no point in saving this data:
dine=LoadMesh( "dine.3ds"
to disk. You are saving the mesh handle which isn't gauranteed to be the same every time you play your game.

Also, there's no point in saving it because you still have to use the loadmesh command the next time the program is run, which will overwrite the saved data anyways.


Strider Centaur(Posted 2004) [#8]
A good idea is to look into one of the many INI file parsers in the archives. A INI file has its info in very HUMAN readable format so it could look something like this:

[playermesh]
meshfile = "player.3ds"
scalemesh = 0.2,0.2,0.2


[object]
objectID = 100
objectmesh = "house.3ds"
objectposition = 1021.1,0.0,121.1
objectrotation = 0.0,82.2,0.0
objectname = "Equipment Store"
objectselectable = True
[end object]

[object]
objectID = 1
objectmesh = ""
objectpostition = 0.0,0.0,0.0
objectrotation = 0.0,90.0,0.0
objectname = "Player Start Location/Dirction"
objectselectable = False
[end object]

.... etc .....



Read this file line by line using the readline.

Then you can just parse in the file with simple use of the right$, Instr, Mid$ and Left$ string commands to read the data.

The advantage to a file format like this is that it is easy to see exactly what should be going on. Unlike more binary data based files where you have no real way to tell if the level data is valid or not. Now there are limitation to this type of file, for instance its not a very good Save Game format. With Save Game files you are probably just storing some internal variables as ints and floats so just using writeline and readline should work for them as long as you right and read in the same order.

No matter what format you use, keeping your level file broken up into sections will make creating, editing and loading them alot easier in the long run.

Ive recently come to conclusion I need to make a world editor form my underwater demo/game Im presently working on. The funny thing is, Im going to use the demo/game to do this, so anyone can simply goto edit mode while in the game and change the terrain and location of structures in real time. The format of the game level data is like that I posted above.