Blitz2D Newbies: Functions + Reading & Writing Files
by SwedishChef

Intro

This little tutorial simply learns you how to use functions, and how to read and write to files easily.

Three Ways of Using Functions

1st way:
Let's start with the easy way. This is probably the way functions are mostly used. This way, the function will just be a load of code. Whenever you call it, those lines of code will be read through. This is helpful if you have many similar pieces of code in your game. Here is an example from the source code:


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:
With this way, we will return a number with the function, i.e. the function will be worth a number, look at this example:


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:
This way makes use of the ( and ) that you find at the end of each function. If you don't know how they work, read on... :) If you want to make a function that is different every time you call it, you have to include something inside these. Here is an example:


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 / Writing files

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? :)

Conclusion

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

-SwedishChef


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.