Tutorials  |     home
ChocoboMonkey's Art   |   ChocoboMonkey's Tile Creation   |   CHRs   |   CR2's   |   CHRs 2   |   Learning to Draw   |   Thesis on Tile Art   |   Tile Making with Photoshop 5   |   V2 Tutorial   |   Collision Detection   |   DMA   |   Working with Bits   |   Item & DAT Files   |   Menu Making   |   Reading From Files   |   Savegames   |   Entity Talking   |   Mordred Tutorial 1   |   Mordred Tutorial 2   |   Mordred Tutorial 3   |   Hicolor Fades   |   Tiny Tutorial on Multidimentional Arrays   |   V2.7 B10 Python Commands   |   Overkill's Tutorial 1   |   Overkill's Tutorial 2
Reading From Files
Reading from files comes in handy when making games more than a few maps long, but it may seem daunting at first. This is a common problem, which requires some thought, but once you've done it once, it's actually pretty easy to use. It is easiest to explain with an example, so here goes:

Suppose that you want a simple function which will read character stats from the file "stats.txt" for your party system (to keep it simple I'll only use three stats: Name, HP, and MaxHP). You have 4 characters, with an array for each of their stats, and you have a text file containing all their stats in this format:
Player Name
HP
Max HP

This is a simple format, with each stat taking up one line. There are no complications like reading multiple stats on the same line. It can be done, but I'll keep this simple.

Now, we can start coding!

string PlayerName[4];

This line creates the array which stores the players' names. It's important to remember that array indexes start at 0, not 1.

int HP[4], MaxHP[4];

Again, this creates arrays for storing HP and MaxHP for the players. Also, we need a global string variable to hold the strings read from the file, because there is a bug in V2 which prevents from reading directly into an array, and another bug which prevents you from reading into local strings! As you can see string support in V2 is picky at best.

string TempString;

Now, we can start writing the function.

Void ReadStats()
{

We need a few variables now. One to store the file pointer, the other as a counter.

int file, i;


OK, the variables are set up, now we need to open the file.

file=fopen("stats.txt");

Next is the fun part - reading the stats from the file. Because there are 4 characters, we'll use a for loop.

for(i=0; i<4; i++)
{

This next line is the heart of the whole function. It reads the entire line from the file and stores it in the string. It then moves to the next line for the next time it is called. In this case, we are reading the player name.

FGetLine(TempString, file);

And of course, once you have the stat in a variable, it is a simple matter of copying that variable into the array.

PlayerName[i]=TempString;

Simply repeat the above steps for the rest of the stats.

FGetLine(TempString, file);

Ah, but what's this? You're reading HP as a string, but you'll want it to be a number so you can do math stuff on it later. Fortunately, V2 has a useful function which converts a string into an integer, Val().

HP[i]=Val(TempString);

You can probably guess what the next lines will be, now. :)

FGetLine(TempString, file);
MaxHP[i]=Val(TempString);

We're done reading the stats, so we can close the for loop. And once the for loop is finished, we're done with the file, so we can close that as well. It is important to close the file when you're done with it, other wise you won't be able to open it again!

}
Fclose(file);

Finally, the final closing bracket will end the function.

}

And that's it! Simple, isn't it? :)