Hey guys. I've got this bit of code that is beginning to frustrate me so much and I just can't figure out why it isn't working the way I want it to.
I have a base class (Location) and I have 2 classes inheriting from it. "WaitingRoom" and "Arena". In WaitingRoom I've made a function that will set the bool variable I have in Location to TRUE. In Arena I have a function that just says --> If the bool is true, std::cout << "print this out";
I then call both of these functions (enterArena) and (isInArena) in a separate class called Game. I will copy this all out in order so it's easy to see.
void WaitingRoom::enterArena()
{
string.newLine();
std::cout << "Enter the arena and fight somebody by
typing 'goto arena'" << std::endl;
if (string.compareStrings("goto arena") == 0)
{
Location::inArena = true; // At this point it's working, inArena is set to true
}
}
1 2 3 4 5 6 7 8
void Arena::isInArena()
{
while(Location::inArena == true) //Once it gets here, the inArena bool is set to false..
{
std::cout << "You are in the arena!" << std::endl;
break;
}
}
1 2 3 4 5
void Game::enterArena()
{
waitingRoom.enterArena();
arena.isInArena(); //Sometimes doesn't even enter this function
}
What's happening is, the inArena bool is setting to true but once it enters the isInArena function in the Arena class, the bool just automatically sets itself back to false, henceforth skipping everything inside that function.
You have apparently two variables 'waitingRoom' and 'arena'. What you do can be demonstrated with a simpler example:
1 2 3 4 5 6 7 8 9
int main() {
bool foo = false;
bool bar = false;
foo = true; // foo is indeed true now
if ( bar ) std::cout << "invisible";
return 0;
}
Why should bar change when an unrelated variable foo does?
Class WaitingRoom object IS-A class Location object.
Class Arena object IS-A class Location object.
Two separate Location objects. Two objects with different type.
By the way, I would discourage you from using Location::inArena etc, in your derived classes; I've never seen this before and I was confused as to how your code was even compiling before I saw that Arena was probably a derived class.
Also, your Location class probably shouldn't have booleans representing where the player is, the derived Location class itself should probably identify that information somewhere.
Also, your Location class probably shouldn't have booleans representing where the player is, the derived Location class itself should probably identify that information somewhere.
In derived? No.
In Location? Perhaps. Location could have single bool playerIsHere; What is wrong in player being in 13 locations simultaneously? Quantum theory does not oppose. Fellow with chainsaw does not oppose.
The game could have Location * currentLoc; that gets updated when the player moves.