Alright - My SAVE LOAD is working great - except for one number. That number is the RACE and CLASS number. It tells the game what RACE the player is, and what CLASS the player is. It is the index of what member of the array of pointers containing all of the available RACEs/CLASSes. The CLASS number is 0, or 1. Here's my fileio.cpp file...
Code:
#include"player.h"
#include"main.h"
#include<fstream.h>
#include"baseStats.h"
#include"item.h"
#include"magic.h"
int getNextInt(ifstream& loadFile);
int saveGame(char* fileName)
{
ofstream saveFile(fileName, ios::out);
if(!saveFile)
{
cout << "Error opening file for saving!";
return 0;
}
saveFile << "#Player Name\n" << player1.szName;
saveFile << "\n#Speed\n" << player1.speed;
saveFile << "\n#Attack\n" << player1.attack;
saveFile << "\n#Wisdom\n" << player1.wisdom;
saveFile << "\n#Defense\n" << player1.defense;
saveFile << "\n#CurHP\n" << player1.curHp;
saveFile << "\n#MaxHP\n" << player1.maxHp;
saveFile << "\n#CurMp\n" << player1.curMp;
saveFile << "\n#MaxMp\n" << player1.maxMp;
saveFile << "\n#Total EXP\n" << player1.totalexp;
saveFile << "\n#Level\n" << player1.level;
saveFile << "\n#EXP\n" << player1.exp;
saveFile << "\n#EXP TNL\n" << player1.tnl;
saveFile << "\n#No Items\n" << player1.noItems;
saveFile << "\n#No Spells\n" << player1.noSpells;
saveFile << "\n#Gold\n" << player1.gold;
saveFile << "\n#curX\n" << curX;
saveFile << "\n#curY\n" << curY;
saveFile << "\n#Current Area\n" << player1.curArea;
saveFile << "\n#Inventory";
for(int i =0;i < itemMax;i++)
{
if(player1.inventory[i])
saveFile << '\n' << player1.inventory[i]->idNo ;
}
saveFile << "\n#Spells";
for(i =0;i < spellMax;i++)
{
if(player1.spellInv[i])
saveFile << '\n'<< player1.spellInv[i]->idNo;
}
saveFile << "\n#Current Weapon\n" << player1.curWeapon->idNo;
saveFile << "\n#Current Armor\n" << player1.curArmor->idNo;
saveFile << "\n#Race\n" << player1.pRace->number;
saveFile << "\n#Class\n" << player1.pClass->number;
saveFile.close();
return 1;
}
int loadGame(char* fileName)
{
for(int q =0;q<spellMax;q++)
player1.spellInv[q] = 0;
for(q =0;q<itemMax;q++)
player1.inventory[q] = 0;
ifstream loadFile(fileName, ios::in);
if(!loadFile)
{
cout << "Error opening file for loading,";
return 0;
}
char ch;
int i = 0;
while(!loadFile.eof())
{
loadFile.get(ch);
if(ch == '#')
{
loadFile.ignore(1000,'\n');
continue;
}
if(ch == '\0')
break;
if(ch == '\n')
break;
player1.szName[i] = ch;
i++;
}
player1.speed = getNextInt(loadFile);
player1.attack = getNextInt(loadFile);
player1.wisdom = getNextInt(loadFile);
player1.defense = getNextInt(loadFile);
player1.curHp = getNextInt(loadFile);
player1.maxHp = getNextInt(loadFile);
player1.curMp = getNextInt(loadFile);
player1.maxMp = getNextInt(loadFile);
player1.totalexp = getNextInt(loadFile);
player1.level = getNextInt(loadFile);
player1.exp = getNextInt(loadFile);
player1.tnl = getNextInt(loadFile);
player1.noItems = getNextInt(loadFile);
player1.noSpells = getNextInt(loadFile);
player1.gold = getNextInt(loadFile);
curX = getNextInt(loadFile);
curY = getNextInt(loadFile);
player1.curArea = getNextInt(loadFile);
int nNoPounds = 0;
for(int k =0;k<itemMax;k++)
{
if(loadFile.peek() == '#')
nNoPounds++;
if(nNoPounds == 2)
break;
player1.inventory[k] = ITEM.allItems[getNextInt(loadFile)];
}
nNoPounds = 0;
for(k =0;k<spellMax;k++)
{
if(loadFile.peek() == '#')
nNoPounds++;
if(nNoPounds == 2)
break;
player1.spellInv[k] = SPELL.allSpells[getNextInt(loadFile)];
}
player1.curWeapon = ITEM.allItems[getNextInt(loadFile)];
player1.curArmor = ITEM.allItems[getNextInt(loadFile)];
player1.pRace = RACE.allRaces[getNextInt(loadFile)];
player1.pClass = CLASS.allClasses[getNextInt(loadFile)];
return 1;
}
int getNextInt(ifstream& loadFile)
{
int value = 0;
unsigned char ch = 0;
while(!loadFile.eof())
{
loadFile.get(ch);
if(ch == '#')
{
loadFile.ignore(1000,'\n');
continue;
}
if(ch == '\n')
break;
value = 10*value + (ch - 48);
}
return value;
}
The problem is, when it gets the class integer, it will get 1, or 0, like it should. Next it will pull out some bogus charecter like 315 etc. I have no idea where is come from. When it multiplies the 1 and 318 together, we get a totally weird value that crashes the program when CLASSes are used. The entire source is <a href=http://wetfire.angelcities.com/source.zip> here</a>. Thanks