ofstream help
I am trying to save a level in my RTS game to a file. It says it cannot convert to char* when I use the write or read functions. Here is the data type:
class Creature
{
public:
//Position/stage of movement vars
unsigned char x; //current integer position on map
unsigned char y;
float addx;//how much off center of square
float addy;//if >= 1, subtract 1, add 1 to y, etc. use brain.
float attackstage;//if >= 1, do attack, subtract 1
float harveststage;
//Built-in values - set when creature is made
char cname[20];
unsigned char nlen; //length of the name
int hp;
int maxhp;
char damage;
char armor; //subtract 10% from damage taken per 1 armor
char movespeed; //percent of square moved per frame
char attackspeed; //percent of attack round per frame
char harvestspeed; //percent of harvest/cash in done per frame
char sight; //RADIUS in map units of sight
char range; //RADIUS
char xsize;//size of the SPRITE!!! does not affect occupyedness
char ysize;//of the squares it is near - could use picnum, but easier this way
char picnum;//which surface in my array of ''em holds it
unsigned flies : 1; // can it fly?
int hsoGreeting[3]; //3 indexes to greeting sounds
int hsoCommand[3]; //3 indexes to sounds when they are commanded
int hsoFinished[3]; //3 indexes to sounds when they are made
int hsoDeath[3]; //3 indexes to sounds when they die
//What it is doing
unsigned moving : 1;//only if moving around on map - harvest, etc.
unsigned attacking : 1;//actually building up and doing damage
unsigned pursuing : 1;//moving toward target, also moving = true
unsigned harvesting : 1;//actually at the bank
unsigned toharvest : 1;//going toward the bank, also moving=true
unsigned cashingin : 1;//actually in the BO cashing in dough
unsigned tocashin : 1;//going to the BO
unsigned patrolling : 1;//if patrolling, also moving=true
unsigned invading : 1;//with any others, will attack any in range
unsigned char mx2;//where it''s moving to - for ALL MOVEMENT
unsigned char my2;
unsigned char px2;//other patrolling point besides mx2,my2
unsigned char py2;//swap if you get to mx2, my2
int target;//target enemy, bank
char money;//amount of money the unit has
char dir; //1 is up, then clockwise - set every frame depending on where it''s going, idle time
float frame; //update when moving
char pindex; //which player controls it
int cindex; //which creature # it is
//Member functions
void Upkeep(); //take care of himself - add subfunctions if needed
void Move();
void Patrol();
void Invade();
void Harvest();
void Draw(int MapX, int MapY);//draw it on the screen
void Idle(); //do what happens in idle time
//copy constructor
Creature &operator=(const Creature&);
};
Anything in here that would cause a problem?
~BenDilts( void );
Are you trying to simply write out the whole structure to a file? If so, that is almost never the way to do it. Many routines have no built-in way of converting ints to char*, that is something you have to do yourself. And when you have pointers in your class, writing the actual part of memory pointed to is useless anyway. Generally you would write it out member by member and then read them in the same way.
As an aside - why so many bitflags for the creature''s state? Can the creature actually do more than 1 of these things at once? If not, then why not just have a single State variable?
As an aside - why so many bitflags for the creature''s state? Can the creature actually do more than 1 of these things at once? If not, then why not just have a single State variable?
Hmm, I wasn''t paying enough attention. Are you trying to do this?
ofstream somefile("filename.ext");
somefile << aCreature;
If so, then you''re gonna be disappointed A stream is not some magic thing that can write whatever you throw at it, it can only handle your classes if you write a << operator for that class (or less commonly, a conversion to a type that ofstream -can- deal with). Or, if you pass it to one of the specific functions (ie. not the standard << operator) then you have to pass the function the type it is expecting, like any other function. So, if it expects a char*, and you feed it a Creature, of course it will choke If you write a << operator for Creature, that will almost definitely involve you writing out all the member variables. You will need to write a similar routine in reverse for ifstream too, if you want to use that method to read them in again.
ofstream somefile("filename.ext");
somefile << aCreature;
If so, then you''re gonna be disappointed A stream is not some magic thing that can write whatever you throw at it, it can only handle your classes if you write a << operator for that class (or less commonly, a conversion to a type that ofstream -can- deal with). Or, if you pass it to one of the specific functions (ie. not the standard << operator) then you have to pass the function the type it is expecting, like any other function. So, if it expects a char*, and you feed it a Creature, of course it will choke If you write a << operator for Creature, that will almost definitely involve you writing out all the member variables. You will need to write a similar routine in reverse for ifstream too, if you want to use that method to read them in again.
Kylotan is right. It does not work to do somefile << aCreature; You must make a function to write out all the imformation to the file. It will be lengthy, but you will only have to do it once. Here is a start.
int WriteCreatureStuff(Creature WriteCreature,ofstream somefile)
{
somefile.open(WriteCreature.cname);
somefle<<ect.
}
Visit http://members.xoom.com/ivanickgames
int WriteCreatureStuff(Creature WriteCreature,ofstream somefile)
{
somefile.open(WriteCreature.cname);
somefle<<ect.
}
Visit http://members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement