Original Post
Hello,
I'm working on a port of a free Print'n'Play game from BoardGameGeek called ZombiePlague. Its my first full game project so be easy on me. I'm having trouble saving a vector or a class and reading it back. To begin, here is the class I'm looking to write to a file:
In my game, I keep the game pieces in an array as that there could be more pieces added every turn. What would be the best method to write this vector to a file and read it back? I've had multiple attempts and none have been successful. Any web reference or feedback appreciated.
Here is what I've tried without success:
Here is a pic of the prototype I have so far:
I'm working on a port of a free Print'n'Play game from BoardGameGeek called ZombiePlague. Its my first full game project so be easy on me. I'm having trouble saving a vector or a class and reading it back. To begin, here is the class I'm looking to write to a file:
enum Facing{
NORTH = 0,
EAST = 1,
SOUTH = 2,
WEST = 3
};
enum GamePieceType{
CHARACTER = 0x01,
ZOMBIE = 0x02,
BARACADE = 0x04
};
class GamePiece
{
public:
int tileX;
int tileY;
int facing;
int actionPoints;
unsigned char _type;
GamePiece() {facing = NORTH; actionPoints = 2; _type = ZOMBIE;};
GamePiece(int x, int y, int direction, int ap, unsigned char type) {tileX = x; tileY = y; facing = direction; actionPoints = ap; _type = type;};
};
In my game, I keep the game pieces in an array as that there could be more pieces added every turn. What would be the best method to write this vector to a file and read it back? I've had multiple attempts and none have been successful. Any web reference or feedback appreciated.
Here is what I've tried without success:
void loadLevel()
{
fstream levelData ("level.dat", ios::in | ios::binary);
for(int i = 0; i < (int)gamePieces.size(); i++)
levelData.read((char *)&gamePieces.at(i), sizeof(GamePiece));
};
void saveLevel()
{
fstream levelData("level.dat", ios::out | ios::binary);
for(int i = 0; i < (int)gamePieces.size(); i++)
levelData.write((char *)gamePieces.at(i), sizeof(GamePiece));
};
Here is a pic of the prototype I have so far: