RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 3451 | Replies: 21
Reply
Join Date: Jul 2005
Posts: 26
Reputation: Rearden is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rearden's Avatar
Rearden Rearden is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #11  
Jul 22nd, 2005
aha! Thank you very much.
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #12  
Jul 22nd, 2005
ok I'm trying to save all of these variables:

#include <fstream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <conio.h>

using namespace std;


int trainingHut(void);
int forest(void);
void menuFunc(void);
int weaponShop(void);
int forbidenPalece(void);
int field(void);
int rest(void);
int beach(void);
int armorShop(void);
int attackPhase(void);
int deffensePhase(void);
int itemfind(void);
int attackAsk(void);

//inventory
string currentWeapon;
string currentAcessory;
string currentArmor;
string currentShield;
string currentFootwear;


string usedWeapons[200];
int index = 0;



//stats
int hp = 50;
int xp = 0;
int atk = 17;
int lvl = 1;
int def = 14;
int armdef = 0;
int wepatk = 0;
int mana = 15;
int acc = 11;
int eva = 9;
int spllvl = 1;
int totAtk = atk + wepatk;
int totDef = def + armdef;


//monster stuff

string monster;
int monsterHp;
int monsterAtk;
int monsterDef;
int monsterAcc;
int monsterEva;


int damage;
int monsterDamage;
int temporaryAtk = totAtk;
int temporaryDef = totDef;






//all weapons
string maces = "maces";
string clubs = "clubs";
string swords ="swords";
string scimitars = "scimitars";
string axes = "axe";
string daggers = "daggers";

//all defenses
string shield;
string armor;
string helmet;


//all other
string rings;
string footwear;
string gloves;
string necklaces;
string cape;


//all words
string yes;
string Hi;
int menu;
int gold = 500;




//maces
string spikedmaces = "spicked";
string bronzemaces = "bronze";
string ironmaces = "iron";
string silvermaces = "silver";
string icemaces = "ice";
string firemaces = "fire";
string goldmaces = "gold";
string dimondmaces = "dimond";



//armor
string spikedarmor = "spicked";
string bronzearmor = "bronze";
string ironarmor = "iron";
string silverarmor = "silver";
string icearmor = "ice";
string firearmor = "fire";
string goldarmor = "gold";
string dimondarmor = "dimond";


:to a file during the program and then when i start up my program again i want it to cheak and see if the variables are saved to a file like "saveOne" and if so i want the variables that were saved there replace the new ones if not, continue regularly


i saw the tutorial but it just told me how to make it write something not write a variable and then load it again
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #13  
Jul 22nd, 2005
Ohoho! I see.
There's an easy way to do this, but first, some more information.
For this game, do you have an inventory that allows you to hold multiple copies of armor, for example? Or can you only have one at a time?
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #14  
Jul 22nd, 2005
a.
invintory
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #15  
Jul 22nd, 2005
Let me just assume that you've got an inventory that only lets you have one particular set of equipment at a time.

If that is the case, why bother writing variables when you could write a character string that contains the current information?
Here's what I mean:

Let's take your example, and make it into a single string for maces and armor types.
//maces
string spikedmaces = "spiked"; // s 
string bronzemaces = "bronze";// b
string ironmaces = "iron";  //i
string silvermaces = "silver"; //a
string icemaces = "ice";// c
string firemaces = "fire";//f 
string goldmaces = "gold";//g 
string diamondmaces = "diamond";//d

//armor
string spikedarmor = "spiked";//s
string bronzearmor = "bronze";//b
string ironarmor = "iron"; //i
string silverarmor = "silver"; //a
string icearmor = "ice"; //c
string firearmor = "fire";//f
string goldarmor = "gold";//g 
string dimondarmor = "diamond";//d

Okay. Those comments are the character representations for those items.
Now:

char equip[2]; //Character equipment array. 0 is weapon; 1 is armor

Suppose you're blinged out with a diamond mace and gold armor. When you want to save, you do the following:

equip[0] = 'd'; //thus making your weapon a diamond mace
equip[1] = 'g';//this is your golden armor
And you save the equip array.

When you load the game, you then parse the input information. Based on the character contents of the string, you can equip yourself without storing huge numbers of variables.
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #16  
Jul 22nd, 2005
do you know a way that i could just do it variable by variable like if i was just going to save just one variable at a time?and then load it couldyou do like a whole example of like saving just a "money" variable?
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #17  
Jul 22nd, 2005
It's called a struct or class. But if you don't mind me saying, your implementation, while straightforward, isn't very efficient.

You're going to have to write this stuff as structures, then have it saved in binary, using ofstream and write. It's got its appeals...

Here's a quick example:

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

struct example{
char name[40];
int potions;
};

int main()
{  
  struct example hero; //declares instance of struct type 'example' called 'hero'.
  strcpy(hero.name, "Leroy Jheeenkins");
  hero.potions = 5;


//write data

  ofstream save_1("savefile", ios::out|ios::binary);
  if(!savefile) //Couldn't open save file.
  {
     cout<< "Save failed."<<endl;
     return 1;
  }
  save_1.write((char *) &hero, sizeof(struct example)); //writes entire struct to file.
  save_1.close(); //closes file stream
  return 1;
}
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #18  
Jul 22nd, 2005
do you know a way that i could just do it variable by variable like if i was just going to save just one variable at a time?and then load it couldyou do like a whole example of like saving just a "money" variable?
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #19  
Jul 22nd, 2005
See my last post for an edit. To get the info out of the file, you have to use ifstream and read.

ifstream load_1("savefile", ios::in | ios::binary);
if(!load_1)
{
cout<< "Cannot load file"<<endl;

return 1;
}
load_1.read((char *) &hero, sizeof(struct example)); //loads the saved struct.
cout<<hero.name<<endl;
cout<<"Number of potions: " << hero.potions<<endl;
load_1.close(); 
return 0;
}
Edit: This lets you use the hero struct and all its information until the program exits.

That's really the best way to do it simply, but I still think your method is rather awkward and uses a lot of resources it doesn't have to.
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #20  
Jul 22nd, 2005
yuuuyuyuiuttyi never mind




thx
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:30 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC