Post by Azan on Sept 11, 2005 18:34:12 GMT -5
Ok I probably wont finish this today.
In this tutorial we will be going through how file saving works, and how you can use this to save your game.
Part 1: Understanding Basic file reading/writing
The part will be simple, but it will teach you the basics.
First lets look at this.
This gets 2 inputs and creates our file, then writes the information to the file (note you can open it to see what it looks like) and always whenever you are done using a file always close it. Now to move on to reading from a file.
When reading from a file there are different options depending on what you want to do.
For this example this is the best option as the number of lines we are printing is known, but lets say you had an unknown number of lines and wanted to print them all, lets look at option two.
Now looking at the closer the "While Not Eof(filein)" means While Not End Of File, so it will read through untill it hits the bottom of the file, we will be using this method later on when we are loading a save file.
Thats it for the basics, and you dont need to use Read/WriteLine theres Read/WriteInt for writing integers, Read/WriteString and Read/WriteByte, these can be looked up in the command refrence. Now to move on to the next part.
Part 2: Loading and Saving Game Data
This part will show you how to save/load type data.
For this next part we will be using the following code, its pretty basic, you click and it creates a box.
But now lets make our save and load functions.
This is the save function, it will write all the X and Y coordinates to the file, This should be self explanatory. Now for the load function.
This also should be self explanatory, if not please ask. Now to call the functions, Put load() just before the while loop, and save() in your while loop, assign it to a If Keyhit(). Heres the final product.
Hit the spacebar to save, then when you reopen up the program the boxes you made should be there. Now that pretty much raps it up, later I might add how to layout your textfile for if you want to load multiple types, but that shouldnt be hard to figure out .
Copyright: This document may not be posted anywhere else but ragnarokcom.proboards33.com with out the authors consent, by posting anywhere else with out the authors consent you are agreeing to give me your soul mwahahaha (but feel free to post links to this site/tutorial on other sites).
In this tutorial we will be going through how file saving works, and how you can use this to save your game.
Part 1: Understanding Basic file reading/writing
The part will be simple, but it will teach you the basics.
First lets look at this.
i$=Input(":")
i2$=Input(":")
fileout=WriteFile("MY_FILE.txt")
WriteLine(fileout,i$)
WriteLine(fileout,i2$)
CloseFile fileout
This gets 2 inputs and creates our file, then writes the information to the file (note you can open it to see what it looks like) and always whenever you are done using a file always close it. Now to move on to reading from a file.
When reading from a file there are different options depending on what you want to do.
filein=ReadFile("MY_FILE.txt")
Print ReadLine(filein)
Print ReadLine(filein)
CloseFile filein
For this example this is the best option as the number of lines we are printing is known, but lets say you had an unknown number of lines and wanted to print them all, lets look at option two.
filein=ReadFile("MY_FILE.txt")
While Not Eof(filein)
Print ReadLine(filein)
Wend
CloseFile filein
Now looking at the closer the "While Not Eof(filein)" means While Not End Of File, so it will read through untill it hits the bottom of the file, we will be using this method later on when we are loading a save file.
Thats it for the basics, and you dont need to use Read/WriteLine theres Read/WriteInt for writing integers, Read/WriteString and Read/WriteByte, these can be looked up in the command refrence. Now to move on to the next part.
Part 2: Loading and Saving Game Data
This part will show you how to save/load type data.
For this next part we will be using the following code, its pretty basic, you click and it creates a box.
Graphics 600,400,0,2
SetBuffer BackBuffer()
Type box
Field x,y
End Type
While Not KeyHit(1)
Cls
If MouseHit(1) Then
createbox(MouseX(),MouseY())
EndIf
updatebox()
Flip
Wend
End
Function createbox(x,y)
b.box=New box
b\x=x
b\y=y
End Function
Function updatebox()
For b.box=Each box
Rect b\x,b\y,10,10
Next
End Function
But now lets make our save and load functions.
Function save()
fileout=WriteFile("MY_SAVE.txt")
For b.box=Each box
WriteLine(fileout,b\x)
WriteLine(fileout,b\y)
Next
CloseFile fileout
End Function
This is the save function, it will write all the X and Y coordinates to the file, This should be self explanatory. Now for the load function.
Function load()
filein=OpenFile("MY_SAVE.txt")
While Not Eof(filein)
b.box=New box
b\x=ReadLine(filein)
b\y=ReadLine(filein)
Wend
End Function
This also should be self explanatory, if not please ask. Now to call the functions, Put load() just before the while loop, and save() in your while loop, assign it to a If Keyhit(). Heres the final product.
Graphics 600,400,0,2
SetBuffer BackBuffer()
Type box
Field x,y
End Type
load()
While Not KeyHit(1)
Cls
If MouseHit(1) Then
createbox(MouseX(),MouseY())
EndIf
If KeyHit(57) Then
save()
EndIf
updatebox()
Flip
Wend
End
Function createbox(x,y)
b.box=New box
b\x=x
b\y=y
End Function
Function updatebox()
For b.box=Each box
Rect b\x,b\y,10,10
Next
End Function
Function save()
fileout=WriteFile("MY_SAVE.txt")
For b.box=Each box
WriteLine(fileout,b\x)
WriteLine(fileout,b\y)
Next
CloseFile fileout
End Function
Function load()
filein=OpenFile("MY_SAVE.txt")
While Not Eof(filein)
b.box=New box
b\x=ReadLine(filein)
b\y=ReadLine(filein)
Wend
End Function
Hit the spacebar to save, then when you reopen up the program the boxes you made should be there. Now that pretty much raps it up, later I might add how to layout your textfile for if you want to load multiple types, but that shouldnt be hard to figure out .
Copyright: This document may not be posted anywhere else but ragnarokcom.proboards33.com with out the authors consent, by posting anywhere else with out the authors consent you are agreeing to give me your soul mwahahaha (but feel free to post links to this site/tutorial on other sites).