Saving Objects

J

JoeC

I am writing a game and all my game pieces are stored in a single
vector of piece handles. I have the basics I can read and write char
and number files but I am trying to do comthing more complicated. I
am trying to save the data I need to save and I can fill in some when
the necessary data is saved.

Vector->piece handle -> pointer to the pieces. How do I go about
saving that data into a file. I have been doing some research to some
sites: http://www.angelfire.com/country/aldev0/cpphowto/
cpp_BinaryFileIO.html

I seem to get somthing to save. So far in my handle for the pieces I
have a save function that will have the pieces save themselvs. Here
is the data I want to save:

class unit{
protected:

coord loc;
coord currentLoc;
graphics * gr;
tbox * combatBox;
std::vector<color>colors;

float attack;
float dattack;
float defence;

More numbers...

Here is how I am starting:
void unit::write(std::fstream& f){

f.write((char*)&loc, sizeof (loc));
f.write((char*)&colors, sizeof (colors));

Am I going about it the right or wrong way?
 
T

Thomas Tutone

I am writing a game and all my game pieces are stored in a single
vector of piece handles. I have the basics I can read and write char
and number files but I am trying to do comthing more complicated. I
am trying to save the data I need to save and I can fill in some when
the necessary data is saved.

Vector->piece handle -> pointer to the pieces. How do I go about
saving that data into a file. I have been doing some research to some
sites: http://www.angelfire.com/country/aldev0/cpphowto/
cpp_BinaryFileIO.html
[snip]

Am I going about it the right or wrong way?

The FAQ covers this subject in depth:

http://www.parashift.com/c++-faq-lite/serialization.html

Best regards,

Tom
 
J

JoeC

I am writing a game and all my game pieces are stored in a single
vector of piece handles. I have the basics I can read and write char
and number files but I am trying to do comthing more complicated. I
am trying to save the data I need to save and I can fill in some when
the necessary data is saved.
Vector->piece handle -> pointer to the pieces. How do I go about
saving that data into a file. I have been doing some research to some
sites: http://www.angelfire.com/country/aldev0/cpphowto/
cpp_BinaryFileIO.html
[snip]

Am I going about it the right or wrong way?

The FAQ covers this subject in depth:

http://www.parashift.com/c++-faq-lite/serialization.html

Best regards,

Tom

Thanks I did go to that link in my research but I am not sure how it
is supposed to help me. I don't see any exaples of saving data
there. I created a two person game and I would like to create a play
by e-mail system where after each turn the file of pieces are saved
and sent off to play the next turn. I also have a city control object
and the number of points each side has.

Would it be better if I created a text file put in all the necessary
information and sent that off and use the objects contsrtuctors and
set the approate variables when the program is loaded back up. I have
used files to some extent already, it is that I have yet to do
anything complicated and I would like to get suggestions on stratigies
or at least a starting point.
 
O

Ondra Holub

JoeC napsal:
I am writing a game and all my game pieces are stored in a single
vector of piece handles. I have the basics I can read and write char
and number files but I am trying to do comthing more complicated. I
am trying to save the data I need to save and I can fill in some when
the necessary data is saved.

Vector->piece handle -> pointer to the pieces. How do I go about
saving that data into a file. I have been doing some research to some
sites: http://www.angelfire.com/country/aldev0/cpphowto/
cpp_BinaryFileIO.html

I seem to get somthing to save. So far in my handle for the pieces I
have a save function that will have the pieces save themselvs. Here
is the data I want to save:

class unit{
protected:

coord loc;
coord currentLoc;
graphics * gr;
tbox * combatBox;
std::vector<color>colors;

float attack;
float dattack;
float defence;

More numbers...

Here is how I am starting:
void unit::write(std::fstream& f){

f.write((char*)&loc, sizeof (loc));
f.write((char*)&colors, sizeof (colors));

Am I going about it the right or wrong way?

Hi. I recommend you to find some XML library and store your data in
XML file. If XML is not for any reason what you can use, use text
file. It will save you lot of time with reading data and it is better
portable too.
 
G

gamediaceo

XML is a very good choice, simply because the format is standardized
and it's human-readable. You should look into XML formats and decide
if that's the course for you. If so, then try http://www.grinninglizard.com/tinyxml/,
which I found to be a very easy-to-use C++ XML library. I might have
some XML code around on my harddrive so if I'll poke around to see if
there's anything that could be of use to you (I used to write games
using TinyXML for saving/loading files as well).

If you don't want to use XML, then the way you're going about it is
fine as well, if your game is simple enough. Beware though, if you
make changes between your saving routine and your loading routine
you'll run into problems. For example, if you change the order in
which you save but leave the order in which you load different
variables, that'll run you up the crapper. XML will solve that problem
because every variable is tagged, so you know *exactly* what you're
loading.

Hope that helps,
Cheers,
Henry

http://hamath.blogspot.com - Science, Technology, Interesting Stuff
Blog
 
J

JoeC

XML is a very good choice, simply because the format is standardized
and it's human-readable. You should look into XML formats and decide
if that's the course for you. If so, then tryhttp://www.grinninglizard.com/tinyxml/,
which I found to be a very easy-to-use C++ XML library. I might have
some XML code around on my harddrive so if I'll poke around to see if
there's anything that could be of use to you (I used to write games
using TinyXML for saving/loading files as well).

If you don't want to use XML, then the way you're going about it is
fine as well, if your game is simple enough. Beware though, if you
make changes between your saving routine and your loading routine
you'll run into problems. For example, if you change the order in
which you save but leave the order in which you load different
variables, that'll run you up the crapper. XML will solve that problem
because every variable is tagged, so you know *exactly* what you're
loading.

Hope that helps,
Cheers,
Henry

http://hamath.blogspot.com- Science, Technology, Interesting Stuff
Blog


Thanks, I didn't know that XML was out there I though it was
Extendable Marck-up Language.
 
R

red floyd

JoeC said:
Thanks, I didn't know that XML was out there I though it was
Extendable Marck-up Language.

It is. XML stands for eXtensible Markup Language. However, gamedia is
right. It's an excellent language for storing structured data.
 
G

gamediaceo

It is, XML stands for eXtensible Markup Language. But the power of it
comes from allowing you to custom-define any sort of data you want,
which is helpful. Anyways, good luck!

Cheers, Henry Yuen
 
R

red floyd

It is, XML stands for eXtensible Markup Language. But the power of it
comes from allowing you to custom-define any sort of data you want,
which is helpful. Anyways, good luck!

Darn near identical and simultaneous posts! Wow!
 
J

JoeC

Darn near identical and simultaneous posts! Wow!



I don't know much about XML but trying the save information with
fstream is getting pretty cumbersome. It dosn't even work when I am
trying to save information in a vector. When I get some time I will
take a closer look at XML
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top