Here is a loading format I am using for a game I am making.
Read all the comments! I added some to help you understand it a little better.
REM This is were we would load our texture for the objects, the textures can be used for several objects, meshes can not, so we will make those as we need them
REM Open level file to read
open to read 1,"Example.txt"
REM Read how many objects were saved in the level so we know how many to load
read string 1,T$: NUMBER_OF_OBJECTS = val(T$)
REM Start the level reading and creating process, using a for .. next loop - The amount of times this loops is dertimined by the NUMBER_OF_OBJECTS variable, read from our level file
for n = 1 to NUMBER_OF_OBJECTS
REM Read contents of level file, so we can determin what type of object it is, where it is positioned and how it is rotated
read string 1,T$ : OBJECT_X_POSITION = val(T$)
read string 1,T$ : OBJECT_Z_POSITION = val(T$)
read string 1,T$ : OBJECT_TYPE = val(T$)
read string 1,T$ : OBJECT_ROTATION = val(T$)
REM Check what type of object it is by checking variable OBJECT_TYPE
REM Check if the object is a cube object
if OBJECT_TYPE = 1
REM Make a cube and assign it to the current object number - defined by our loop variable - n
make object cube n,1
REM Texture the current object with the corresponding texture(Remmed out for this example)
rem texture object n,1
REM Position the object with the OBJECT_X_POSITION and OBJECT_Z_POSITION data - Read from our level file - An extra Y value can be added by changing the file structure a bit
position object n,OBJECT_X_POSITION,0,OBJECT_Z_POSITION
REM Rotate the object correctly using the OBJECT_ROTATION data - Read from our level file
yrotate object n,OBJECT_ROTATION
endif
REM Check if the object is a sphere object, OBJECT_TYPE would have a value of 2 if it is, and a value of one if it is a cube object
if OBJECT_TYPE = 2
make object sphere n,1
rem texture object n,2
position object n,OBJECT_X_POSITION,0,OBJECT_Z_POSITION
yrotate object n,OBJECT_ROTATION
endif
REMSTART
You can have as many object types as you want
just make sure you add them to the level writing code as well as the level reading code
No changing of the level structure is required
REMEND
REM Move onto the next object
next n
A little explanation of commands.
open to read 1,"Example.txt"
Use this command to oped files to read, make sure they are in your project folder. The 1 before the filename says what channel your working with, and don't try to open two files on the same channel without closing the file first, using the
close file 1
command. Again, 1 is the channel you use to tell what file to close. I recommend always closing files after your done with them.
read string 1,T$ : OBJECT_X_POSITION = val(T$)
The read string command will read a line of text from a file(specified channel using 1) and put it into a variable, T$. It is read as text, not values (even though you would write numbers into a file, they are saved as text), so we must convert it into a value using val(T$) and then save it into the OBJECT_X_POSITION variable to use later in the loop. When a line is read, the next time you use the read string command it will automatically read the next line, no need to specify the line to read.
BTW: We need to find out how many objects there are so we know how many to read.
As for Example.txt, you can use any file type you want, as long as that is the file type you saved it as. For example you could call your level Mylevel.lvl.
Depending on how complex you want your levels, you might need to add more data to each object:
I.E. A size variable
read string 1,T$ : OBJECT_X_POSITION = val(T$)
read string 1,T$ : OBJECT_Z_POSITION = val(T$)
read string 1,T$ : OBJECT_SIZE = val(T$)
read string 1,T$ : OBJECT_TYPE = val(T$)
read string 1,T$ : OBJECT_ROTATION = val(T$)
When writing your levels, the level file for the loading code I gave would look something like this
56
100
2
250
And they would represent
Where to position the object on the x axis
Where to position the object on the z axis
The type of object( I.E a cube or sphere)
The rotation of the object
At the beginning of the file there would be the number of objects in the level(This is important).
A real level file might look something like this
5
56
100
2
250
30
110
1
200
56
150
1
250
56
200
1
250
56
500
2
100
So that about covers loading, if you need anything cleared up don't be afraid to ask.
Oh and
TDK's file access tut helped me quite a bit when I was learning.