|  | 
 
 This little tutorial simply learns you how to use functions, and how to read and write to files easily. 
 
1st way: 
 This function is the same everytime it is called. I have put it in to a function because I use this exact same code in two places in the source code. You can also use the Gosub command for this, then it would look like this: 
 These two pieces of code would do the exact same thing, but most people would suggest you to use the function way because it looks neater. 
2nd way: 
 Here, if the score is higher than the top-score that was read in the ReadtxtFile() function, CheckNewHighScore() will be worth 1. So if I put: Print CkeckNewHighScore(), it would print 1. Easy, isn't it? Check how I use this way in the source code to know how to re-write the file. 
3rd way: 
 See, when we call the function, we can include what ever we want inside the ( and ). So, if the new score is higher than the previous top score we would want to re-write the file so that that score is at the top. Then we would call the function like this: 
 The new players score and name are called first, because we want them to be written to the file first, because they are the highest. Then the old highest (high1) will be called second etc. This code is included in the R W harder file. 
 Reading and writing is easy when you understand it (isn't everything? :). In the source code I use reading and writing to make a highscore file. You might have got the grasp of it be looking through the above examples, but here I'll try to explain more. I'll start with reading. If you have stored data in a file that you would like to use in you code(Maybe display on the screen or something). You will have to read it first. when reading files, you give the data identifiers(This is Not done when writing). Example of reading a highscore file(as above): 
 To read from the file we first need to open it :) Then we read the data, to read an integer, we use ReadInt, to read a string, we use ReadString, to read a floating number, we use ReadFloat, note, you have to have written to the file using WriteInt, WriteString and WriteFloat to read using the showed commands. The data have to be read in order, you can't read the data showed with the identifier high2 above without first reading the data with the identifies high1 and name1. Now I'll go through how writing works. If you for some reason want to re-write a file (if the player scored a new highscore etc.) you will use the write commands, follow the example: 
 If we want to write to a file, we open it using the WriteFile command. Then we write to it. To write an integer, use WriteInt, a string, WriteString, a floating number, WriteFloat. Note, when writing to a file, the old data that was stored there is over written. If you want to create a file inside the program, you use the writefile command aswell. This will create a new file if it doesn't already exist, helpful isn't it? :) 
 Go through the source file too, it is very well commented so if you don't understand something from my explanations here, you will understand from my explanations there, thats a promise! You will find that reading and writing to files is easy. My usual mistake is that I forget using the $ and # signs, I often forget to put the variables global as well. There are a set of Blitz Basic files that go with this tutorial. To grab them: Click Here 
 For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |