Lately I've being seeing a bunch of posts about people asking how to save and load game data, so I've decided to help. On my site I have created tutorials that explain in detail how to save and load game data using DarkBasic's file commands, or array commands. You can find the tutorials here:
http://www.freewebs.com/underworld1020/tutorials.htm
I've also created my own file function set that will also save and load game data. I feel they are easier to use and also have the advantage of saving or loading data from a specic line in a file, and also the files are more organized because each line is marked with its line number.
Now you should probably read the tutorials I created for DarkBasic's commands,
BEFORE trying to use mine, but thats your choice.
Here's how to use my functions:
The first thing you must do is copy and paste all of this:
Function Create_File(FileName$,MaxLines)
If File Open(31)=1 Then Close File 31
If File Exist(FileName$)=1 Then Delete File FileName$
Open To Write 31,FileName$
Write String 31,"Lines:"+Str$(MaxLines)
For LineNum= 1 To MaxLines
Write String 31,"Line#:"+Str$(LineNum)
Next LineNum
Close File 31
EndFunction
Function Write_String(FileName$,LineNum,Data$)
If File Open(31)=1 Then Close File 31
If File Open(32)=1 Then Close File 32
If File Exist("TempFile.dat")=1 Then Delete File "TempFile.dat"
Copy File FileName$,"TempFile.dat"
Delete File FileName$
Open To Read 31,"TempFile.dat"
Open To Write 32,FileName$
Read String 31,MaxLines$
Write String 32,MaxLines$
For CurrentLine= 1 To (LineNum-1)
Read String 31,OldData$
Write String 32,OldData$
Next CurrentLine
Write String 32,"Line#:"+Str$(LineNum)+" "+Data$
For CurrentLine= (LineNum+1) To Val(MaxLines$)
Read String 31,OldData$
Write String 32,OldData$
Next CurrentLine
Close File 31
Close File 32
Delete File "TempFile.dat"
EndFunction
Function Read_String(FileName$,LineNum)
If File Open(31)=1 Then Close File 31
Open To Read 31,FileName$
Read String 31,Data$
For CurrentLine= 1 To LineNum
Read String 31,Data$
Next CurrentLine
Close File 31
DataInfo=Len(Data$)
DataInfo=DataInfo-11
DataInfo=DataInfo-Len(Str$(LineNum))
Data$=Right$(Data$,DataInfo)
EndFunction Data$
Into the
END of your program code. Then you are ready to use the functions.
Create_File():
To use the Create_File() function simply type in Create_File(. Like This:
Then the name of the file you want to create, make sure to include its extension and also put it in quotes. Like This:
Then type in a comma and then type in how many lines you want the file to be and then then end the parenthsis. Like This:
Create_File("MyData.txt",5)
What that will do is create a file on your computer named MyData.txt and will make it 5 lines in length. You can name the file anything you want and make it as long as you want.
Here's what it would look like if you opened up the file that we created:
Lines:5
Line#:1
Line#:2
Line#:3
Line#:4
Line#:5
It shows how many lines are in the file all together on the first line and then it numbers all the remaining lines with the correct line number according to your data. Thats it for the Create_File function.
Write_String():
The first thing you do to use the Write_String() command is type in Write_String(. Like This:
Then you must type in what file you want to store the data in. Make sure you include its extension and put it in quotes. Like This:
Write_String("MyData.txt"
Also make sure that you've already created that file with the Create_File function before using it here. Then you type in a comma and then the line number you want to store the data to and then type in a comma. Like This:
Write_String("MyData.txt",3,
Then the you must tell it what you want to store on that line. Simply type anything you want to store, but make sure you put it in quotes. Then type in a parathesis Like This:
Write_String("MyData.txt",3,"Hello my name is Tim")
That will store the string on line 3 of the file called MyData.txt, you can store numbers or words anything you want, just make sure its in quotes.
Read_String():
The Read_String() function loads the data into your program. Now the Read_String() function returns data, so the first thing to do is type in what string you want to store the data into. Like This:
If you don't want it to be a string, but a number instead type it in like this:
Then type in Read_String(. Like This:
Then type in what file you want to read from make sure you've already used the Create_File() for the file and also have used the Write_String() function to store data to the file. Also be sure to include the files extension and also put it in quotes. Like This:
Data$=Read_String("MyData.txt"
Then type in a comma, and then type in what line number you want to load the data from then type in a parathesis Like This:
Data$=Read_String("MyData.txt",3)
Now thats all there is to it, but if you did the Val( thing then your code should end with a parathesis. Like This:
Data=Val(Read_String("MyData.txt",3))
Here's An example of the functions in use, just run the code and you'll see:
Create_File("MyData.txt",5)
Write_String("MyData.txt",3,"Hello my name is Tim")
Data$=Read_String("MyData.txt",3)
Print Data$
End
Function Create_File(FileName$,MaxLines)
If File Open(31)=1 Then Close File 31
If File Exist(FileName$)=1 Then Delete File FileName$
Open To Write 31,FileName$
Write String 31,"Lines:"+Str$(MaxLines)
For LineNum= 1 To MaxLines
Write String 31,"Line#:"+Str$(LineNum)
Next LineNum
Close File 31
EndFunction
Function Write_String(FileName$,LineNum,Data$)
If File Open(31)=1 Then Close File 31
If File Open(32)=1 Then Close File 32
If File Exist("TempFile.dat")=1 Then Delete File "TempFile.dat"
Copy File FileName$,"TempFile.dat"
Delete File FileName$
Open To Read 31,"TempFile.dat"
Open To Write 32,FileName$
Read String 31,MaxLines$
Write String 32,MaxLines$
For CurrentLine= 1 To (LineNum-1)
Read String 31,OldData$
Write String 32,OldData$
Next CurrentLine
Write String 32,"Line#:"+Str$(LineNum)+" "+Data$
For CurrentLine= (LineNum+1) To Val(MaxLines$)
Read String 31,OldData$
Write String 32,OldData$
Next CurrentLine
Close File 31
Close File 32
Delete File "TempFile.dat"
EndFunction
Function Read_String(FileName$,LineNum)
If File Open(31)=1 Then Close File 31
Open To Read 31,FileName$
Read String 31,Data$
For CurrentLine= 1 To LineNum
Read String 31,Data$
Next CurrentLine
Close File 31
DataInfo=Len(Data$)
DataInfo=DataInfo-11
DataInfo=DataInfo-Len(Str$(LineNum))
Data$=Right$(Data$,DataInfo)
EndFunction Data$
Now if you still don't understand and you HAVE already read the tutorials also, then just ask me and I'll help you out