Jump to content
  • Advertisement
Sign in to follow this  
lookyboop

money in rpg game

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

heyy everyone. i am making a a text based rpg in c++. meaning no graphics just a black command prompt screen. can some one tell me how i can make the program be able to keep track of the players money, xp, health, level and so on, and then be able to tell the player at any time. i thought i might be able to do it using an array but i wasnt sure. if it can be done using an array tell me how. if not tell me an alternitive method.

Share this post


Link to post
Share on other sites
Advertisement
Make a struct, or go complex and make a class with functions. Fill the struct with your stats. Make an instance of your struct. Set the members to whatever values you wish.


struct Player
{
int hp;
int money;
};

int main()
{
Player player1;
player1.hp = 100;
player1.money = 5;

cout << "HP: " << player1.hp << endl;
cout << "MONEY: $" << player1.money << endl;

cin.get(); //wait so you can see the output
return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by AdamGLMake a struct, or go complex and make a class with functions. Fill the struct with your stats. Make an instance of your struct. Set the members to whatever values you wish.*** Source Snippet Removed ***
thanks a bunch adamgl!!! but i was woindering how i could add to that like if a guy battled and he won  money. how would i add to the current money supply. or if he bought something and i wanted it to subtract money how would i do that??

Share this post


Link to post
Share on other sites
Just add and subtract from your money variable:


struct Player {
int hp, money;
};

struct AwesomeSword {
int cost;
};

int main() {
Player player;
player.hp = 100;
player.money = 50;

Player badDude;
badDude.hp = 11;
badDude.money = 42;

std::string victor = FightBattle(player, badDude);

if (victor == "player") //I won!
player.money += badDude.money;
else //he mugged me!
player.money = 0;

AwesomeSword awesomeSword;
awesomeSword.cost = 92;

if (player.money >= awesomeSword.cost) {
player.money -= awesomeSword.cost;
AddToInventory(awesomeSword); //you would need an inventory variable for this.
else
std::cout << "I am poor." << std::endl;
}

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by lookyboop
Quote:
Original post by AdamGLMake a struct, or go complex and make a class with functions. Fill the struct with your stats. Make an instance of your struct. Set the members to whatever values you wish.*** Source Snippet Removed ***
thanks a bunch adamgl!!! but i was woindering how i could add to that like if a guy battled and he won money. how would i add to the current money supply. or if he bought something and i wanted it to subtract money how would i do that??


You could do something like this


struct Stats
{
int HP, MP, Attack, Money; //etc..
Stats()
{
HP = 100; // default value
MP = 100; // default
Attack = 1; // default
Money = 0; // default
}
};

class Player
{
Stats *stats; // a stats struct for player variables
float x, y, z; // for player position or whatever
public:
Player(Stats *pstat == NULL)
{
stats = new Stats;
if (pstat != NULL)
memcpy(stats, pstat, sizeof(Stats));
}
void addMoney(int amount)
{
stats->Money += amount;
}
void delMoney(int amount)
{
stats->Money -= amount;
}
int getMoney()
{
return stats->Money;
}
};






And so when you start up your game, lets say you have a saved game all you would have to do is load a Stats struct and pass it off to a new Player object, or if you dont have a saved game then when you create an object of type Player, the default values are added for you...

Player Player1; // creates new player and puts default values in Stats struct
Player Player1(savedgame); // savedgame would be a Stats struct if values filled in already

Hope this helps, and if you have any questions let me know

btw// my code may need some slight revision, i dont have a compiler handy atm so i cant tell you this works 100%

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!