-
Here is my GameManager.cpp file:
Code:
#include "GameManager.h"
#include "ShopClass.h"
#include "BattleClass.h"
#include <iostream>
#include <cctype>
using namespace std;
GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}
void GameManager::MainManager()
{
system("CLS");
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
system("CLS");
if (ShopBattle == "Shop" || ShopBattle == "shop") {
Shop.ShopMain();
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
Battle.BattleMain();
} else {
cout << "Error: Invalid Input";
}
}
int GameManager::GetAttack()
{
return attack;
}
int GameManager::GetDefense()
{
return defense;
}
int GameManager::GetLives()
{
return lives;
}
void GameManager::Lives()
{
lives -= 1;
}
int GameManager::GetMoney()
{
return money;
}
void GameManager::AddAttack(int amount)
{
attack += amount;
}
void GameManager::AddDefense(int amount)
{
defense += amount;
}
void GameManager::SetMoney(int newmoney)
{
money = newmoney;
}
void GameManager::SpendMoney(int amount)
{
SetMoney(money - amount);
}
void GameManager::AddMoney(int amount)
{
SetMoney(money + amount);
}
But seriously, try the game this time. You will understand what I'm trying to tell you about the variables resetting themselves.
Oh, I put in a few cool little things I learned to make the game look cooler.
http://z33.zupload.com/download.php?...filepath=38139
-
You shouldn't have the game logic in the constructor. The constructor's purpose is to initialize the object.
-
Initialize the object? That's kind of like doing something like this?
Code:
GameManager manager;
manager.SomeFunction(initialize a variable, another variable);
So, if that's what it is, then what do I need to do?
I don't really understand what you're trying to say with that.
-
>> You shouldn't have the game logic in the constructor. The constructor's purpose is to initialize the object.
In the posted code the logic is not in the constructor, it's in the MainManager function.
>> But seriously, try the game this time. You will understand what I'm trying to tell you about the variables resetting themselves.
I honestly don't even have time to download the source files above, let alone try the game. Sorry.
You said, "I'm still getting that problem where every time the program goes back to the MainManager, it must run the constructor and it sees the variables there and it sets the variables to what they are declared in in the constructor." Then you posted the GameManager class that I already said looks fine and is not calling any constructors out of place. I'm going to be offline for awhile anyway, but the only I could've helped is to see the specific snippet of code you were talking about.
Good luck.
-
The only place where I call the MainManager function is at the end of the shop and battle classes. They just do manager.MainManager();
There's really nothing to show. I just think that I'm putting the variables in the wrong place or something.
-
-
Ok, so I looked at your BattleClass BattleMain and I see your problem. It is the exact same problem, you create a local GameManager object, which is different than the GameManager that you start with.
The solution is a little trickier because your GameManager knows about the BattleClass and the BattleClass knows about the GameManager. This makes things trickier than with just one way.
The best solution for now is to pass an argument to BattleMain. Have BattleMain take a reference to the GameManager:
Code:
void BattleClass::BattleMain(GameManager& manager)
Then remove the GameManager object that you declared inside that function and use the parameter instead. Make sure to change the function declaration and definition. Do the same for ShopClass.
-
I think I might have done something wrong when I did that.
The error
Code:
ShopClass.h:12: error: variable or field `ShopMain' declared void
ShopClass.h:12: error: expected `;' before '(' token
In file included from GameManager.h:5,
from BattleClass.cpp:1:
BattleClass.h:12: error: variable or field `BattleMain' declared void
BattleClass.h:12: error: expected `;' before '(' token
BattleClass.cpp:9: error: no `void BattleClass::BattleMain(GameManager&)' member function declared in class `BattleClass'
The BattleMain function now looks like this:
Code:
void BattleClass::BattleMain(GameManager& manager)
...and the same with the ShopMain function accordingly.
This is how I declared the function:
Code:
public:
void BattleMain(GameManager& manager);
Do I need to call it differently or can I just leave it the way I was doing it before.
Code:
manager.MainManager();
-
Input from anyone would be greatly appreciated.
-
Sorry, I confused GameManager and MainManager. Those aren't very good names.
-
Add a forward declaration of GameManager to your BattleClass (and ShopClass) header file. It looks like this: and goes before your BattleClass class code.
>> manager.MainManager();
Don't call this inside BattleMain or ShopMain. Let the functions return on their own. Then add a loop to MainManager() so that it runs over and over.
-
I think I did what you said correctly, I'm getting more errors though that I don't know how to fix.
Code:
Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Greg\Desktop\RoboMadness\Makefile.win" all
g++.exe -c GameManager.cpp -o GameManager.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
GameManager.cpp: In member function `void GameManager::MainManager()':
GameManager.cpp:27: error: no matching function for call to `ShopClass::ShopMain()'
ShopClass.h:14: note: candidates are: void ShopClass::ShopMain(GameManager&)
GameManager.cpp:29: error: no matching function for call to `BattleClass::BattleMain()'
BattleClass.h:14: note: candidates are: void BattleClass::BattleMain(GameManager&)
make.exe: *** [GameManager.o] Error 1
Execution terminated
-
Don't forget to pass the GameManager to those functions when you call them. That's the whole point. Since you are calling ShopMain and BattleMain from inside your GameManager, you have to pass *this as the argument.
-
OK, what I'm going to try and do is finish up this game and then do my best to learn more about classes in C++. I really don't understand what you mean by "pass it" and I did try to Google it but I'm not really sure what exactly I would Google.
This is the code for GameManager::MainManager:
Code:
void GameManager::MainManager()
{
while (true)
{
system("CLS");
cout << "Current Status:\n";
cout << "Money: $" << money << "\n";
cout << "Lives: " << lives << "\n";
cout << "Attack Points: " << attack << "\n";
cout << "Defense Points: " << defense << "\n";
cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
cin >> ShopBattle;
system("CLS");
if (ShopBattle == "Shop" || ShopBattle == "shop") {
Shop.ShopMain(GameManager& manager);
} else if (ShopBattle == "Battle" || ShopBattle == "battle") {
Battle.BattleMain(GameManager& manager);
} else {
cout << "\nError: Invalid Input\n\n";
cout << "The Program Will Now Exit\n\n";
system("PAUSE");
}
}
}
.. and this is how I declared the shop and battle methods:
Code:
void ShopClass::ShopMain(GameManager& manager)
and when I initialized the methods in the header file, I did:
Code:
public:
void ShopMain(GameManager& manager);
and I did put above the two classes (shop and battle) in the header files.
This is the error I'm getting:
Code:
GameManager.cpp: In member function `void GameManager::MainManager()':
GameManager.cpp:27: error: expected primary-expression before '&' token
GameManager.cpp:27: error: `manager' undeclared (first use this function)
GameManager.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
GameManager.cpp:29: error: expected primary-expression before '&' token
-
>> Shop.ShopMain(GameManager& manager);
You're close, but that's not how you pass something to a function. You have to pass *this.