This is used for saving and loading data in games, like the player's health, or the object's position.
Example code (With REM Statements to help you understand):
REM Project: File Saving
REM Created: 11/13/2006 3:43:04 PM
REM
REM ***** Main Source File *****
REM
`Turn Sincronization On
Sync On
`Start the loop
Do
`If you press the 'S' key then it jumps to the 'writetext' label
if scancode()=31 then gosub writetext
`Check if there is a file to load, then if you press 'L' go to the 'readtext' label
if file exist("MyFile.txt")
if scancode()=38 then gosub readtext
endif
`Put the directions on the screen
text 20,20,"Press S to Save a file"
text 20,50,"Press L to Load and read from a file"
`Call the syncronization
Sync
`Loop the loop :)
Loop
`This is the 'writetext' label
writetext:
`Clear the screen
cls
`If you already saved a file then delete it
if file exist("MyFile.txt") then delete file "MyFile.txt"
`Make the user enter his name, and if he is a male or a female
Input "Name : ",name$
`Clear the screen
cls
Input "Male or Female : ",morf$
cls
`Set the 'age' variable as something other than a string, randomly between 20 and 30
age#=rnd(10)+20
`Make the text file so you can write into it
Open to Write 1,"MyFile.txt"
`Saving a string variable (your name)
Write String 1,name$
`Saving a string variable (male or female)
Write String 1,morf$
`Saving a non-string variable (age), You are translating the non-string to a string
Write String 1,str$( age# )
`Close the file since you no longer need it
close file 1
`Go back to the loop
return
`This is the 'readtext' label
readtext:
`Clear the screen
cls
`Make sure there is a file to load
if file exist("MyFile.txt")
`Open the file so you can read from it
Open to Read 1,"MyFile.txt"
`Take the name you saved into a string from the first line
Read String 1,Rname$
`Take the gender you saved into a string from the second line
Read String 1,Rmorf$
`Take the age you saved into a string from the third line
Read String 1,Rage$
`Set the name variable to the name you just loaded
name$=Rname$
`Set the male or female variable to the gender you just loaded
morf$=Rmorf$
`Change the string back to a value, using the string you just loaded
age#=val( Rage$ )
`Close the file, since you don't need it anymore
close file 1
`Wait for a keypress
WaitKey()
`A mini loop displaying the name, gender, and age that you saved and loaded
repeat
text 20,20,name$
text 20,40,morf$
text 20,60,str$( age# )
`Call syncronization
sync
`Exit this mini-loop if you press ANY key
until scancode()>0
`Clear the screen
cls
`end the IF statement (checking if there is a file to load)
endif
`go back to the loop
return
`This is my WaitKey function, it waits for any key to be pressed
function WaitKey()
repeat
until scancode()>0
if scancode()>0
repeat
until scancode()=0
endif
endfunction
3D Example Code (Also with REMS) :
REM Project: File Saving
REM Created: 11/13/2006 3:43:04 PM
REM
REM ***** Main Source File *****
REM
`Turn Sincronization On
Sync On
`Make a plain green texture
create bitmap 1,10,10
box 0,0,10,10,rgb(0,255,0),rgb(0,255,0),rgb(0,255,0),rgb(0,255,0)
get image 1,0,0,10,10
delete bitmap 1
`Clear the screen
cls
`Make the ground
make matrix 1,500,500,10,10
`texture the ground
prepare matrix texture 1,1,10,10
`Make the player
Make object cube 10,5
`Make some random boxes
for random=50 to 100
make object cube random,rnd(1)+6
position object random,rnd(500),object position y(random),rnd(500)
next random
`Start the loop
Do
`Position the camera
position camera object position x(10),13,-10
`Point the camera at the player
point camera object position x(10),object position y(10),object position z(10)
`Move the player around using the arrowkeys
Move object 10,(upkey()-downkey())*.5
Turn object right 10,(rightkey()-leftkey())*.5
`If you press the 'S' key, go to the 'writeinfo' label to save
If scancode()=31 then gosub writeinfo
`If you press the 'L' key, go to the 'readinfo' label to load
If scancode()=38 then gosub readinfo
`Call the syncronization
Sync
`Loop the loop :)
Loop
`This is the 'writeinfo' label
writeinfo:
`Clear the screen
cls
`If you already saved a file then delete it
if file exist("MyFile.txt") then delete file "MyFile.txt"
`Make the text file so you can write into it, translating all the values into strings
Open to Write 1,"MyFile.txt"
`Saving a non-string variable (the player's X position)
Write String 1,str$( object position x(10) )
`Saving a non-string (the player's Z position)
Write String 1,str$( object position z(10) )
`Saving a non-string (the player's Y angle)
Write String 1,str$( object angle y(10) )
`Close the file since you no longer need it
close file 1
`Go back to the loop
return
`This is the 'readinfo' label
readinfo:
`Clear the screen
cls
`Make sure there is a file to load
if file exist("MyFile.txt")
`Open the file so you can read from it
Open to Read 1,"MyFile.txt"
`Take the X you saved into a string from the first line
Read String 1,objx$
`Take the Z you saved into a string from the second line
Read String 1,objz$
`Take the Y angle you saved into a string from the third line
Read String 1,angy$
`Position the object to the saved positions, translating the strings to values
position object 10,val(objx$),object position y(10),val(objz$)
`Rotate the object to the saved angle, translating the string to a value
yrotate object 10,val(angy$)
`Close the file, since you don't need it anymore
close file 1
`Wait for a keypress
WaitKey()
`end the IF statement (checking if there is a file to load)
endif
`go back to the loop
return
`This is my WaitKey function, it waits for any key to be pressed
function WaitKey()
repeat
until scancode()>0
if scancode()>0
repeat
until scancode()=0
endif
endfunction
If you have any questions, feel free to ask. The directions are in the program.
If you can't find the file you saved,
it's probably because you haven't created a project for it and its saving everything to the DBPro 'Temp' folder.