In my header file I have:
class Game
{
private:
string _name;
Level _currentLevel;
public:
Game();
~Game();
void setName();
void run();
};
In my cpp file I have my run function:
void Game::run()
{
bool finished = false;
string input;
while (!finished)
{
// get input
std::cout << "Enter a command: \n";
std::getline(std::cin, input);
if (input == "quit")
{
finished = true;
}
else if (input == "new")
{
Level _currentLevel;
}
else if (input == "print")
{
_currentLevel.printMap();
}
else
{
std::cout << "Unknown command! \n";
}
}
}
constructor and printmap method of Level
Level::Level()
{
_width = RandomGenerator::Instance()->getRandom(6, 10);
_height = RandomGenerator::Instance()->getRandom(6, 10);
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
addRoom(x, y);
}
}
}
void Level::printMap()
{
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
if (x != 0)
cout << " - ";
cout << _map[coordinate(x, y)].getSize();
}
cout << "\n";
}
}
However when I type new, that runs Level _currentLevel; (to create a new non pointer object), the object dosnt change. I can see it dosn't change the values of level when I run printmap (which prints a map with 30 random values created in the Level constructor). While debugging the value of _height changes in the Level constructor. How should the value of _currentLevel be updated from my Game class?