# Game State storage - best practices

This topic is 4408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a 10 x 10 array of game objects. At any given time I want to write out the "game state" to a file OR read the "game state" stored in a file back into the 2D array. Are there some websites / books / resources / strategies / tips / tricks for handling this? How is it typically handled? My board game is simple to store like chess or checkers but may have one or two additional things I want to store as well, like the current HEALTH of game pieces and whose turn is it, etc.

##### Share on other sites
You can try Serialization: Click Here. But also I do believe some libraries such as Boost provide such services... haven't played with them before though.

##### Share on other sites
well you could try possibly doing and fstream and do like

fstream file;

file = fopen("file.txt", RW);
for (int i = 0; i < arraysize; i++)
{
file << Array << "\n\r";
}

file = fopen("file.txt", RW);

for(int i = 0; i < arraysize; i++)
{
Array << file;
}

now i know syntax isnt correct but definantly look up fopen and such...if your doing it in c++ im presuming.

##### Share on other sites
std::ofstream file;file.open("file.txt");for (int i = 0; i < arraysize; i++){file << Array << "\n";}file.close();

std::ifstream file("file.txt") // or you can open and constructfor(int i = 0; i < arraysize; i++){file >> Array}

we dont want anyone complaining about code not working, now do we...

these classes are defined in:
#include <fstream>

the class type of Array will need a version of
ostream& operator<<( ostream& out, const ClassType& type );//andistream& operator>>( istream& out, ClassType& type );

##### Share on other sites
If you *know* it will be 10 by 10 *every time*, then first define a serialized (i.e. either text or binary) representation of the object, and let the file contents be a sequence of 100 of those representations. You will write the file by looping through the array and iteratively writing those representations. Similarly for reading in.

To facilitate this process, you will want to define member functions for the object as follows:

(i) a way to create an object given an istream reference. This will "read" one object representation off of the file, at the current point in the file, without caring if there is data before or after. This could be either just an init method (or an operator>>), a constructor taking the istream reference as a parameter, or a static "factory function".

(ii) a way to write an object out given an ostream reference. This will "write" one object representation to the file, at the current point (which is assumed to be at the end), without caring if there is data before. This would normally be either an operator<< (for a text file format) or a .write() or similarly-named member (for a binary file format).

In order to make it work you need to think about what things have to be recorded. If you just have plain data members (no pointers), then normally you write a representation of each data member in order. Primitives can be represented in the normal way (e.g. in a text formatted file, with an int-type member, you would output the int by operator<<, and then output a space). Text is harder to handle. I recommend using a std::string as a data member, and representing it by writing a number representing the length of the string, followed by the string data.

For more info, check Jemburula's link. You really won't do better assuming you want to do it yourself. If you don't want to do it yourself, then (a) congratulations, you're sane! ;) and (b) check out Boost::Serialization.