How to make Class A modify Class B member?

Im making a pong clone, and i cant figure out how to make changes in the Settings state change member variables in the Game state. Anyone have any ideas? If it helps, Settings and Game are both derived from a GameState class.
Just to know things better: how are you "building" your game? if you have 2 different classes derived from one class, how do you interact with those?
Does it look like following?
GameState->Game
GameState->Settings

There is little problem if so... GameState is a class alone and you derive it from both classes. Is it possible to use just a GameState class which interact with the Settings?
1
2
3
4
5
6
7
8
class Game {
private:
Settings settings; // now you have access to the settings via your game class
// other definitions
public:
Game(); // Default settings can be handled here
// other definitions
};
Last edited on
make the required members of gamestate protected. if settings class is derived from it you will have access to them.
The settings should not be owned by any individual state, but instead should be owned by the game itself.

Individual states could have a pointer/reference to the game's settings, but they should not be maintaining their own copy of it.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Game
{
  // the game owns the settings
  Settings  mySettings;

  void Run()
  {
    // when you run a state, give the state a pointer to the game (this)
    GameState* currentstate = new GameState_Intro(this);
    currentstate->Run();
    //...
  }
};

//....

void GameState_Intro::Run()
{
  // when you need the settings, get them from the game
  Settings& gamesettings = thegame->GetSettings();
    // note:  gamesettings is a reference (Settings&), not a copy
    // thegame is whatever Game pointer was passed to the constructor
}
@errago

When the game goes to the settings page, settings' HandleEvents() function changes the difficulty member based on where the user clicked. So how would Game( game is the state where the actual game is played) know what the user clicked on in Setting's and be able to set the AI's speed accordingly?
Can you just create member function to Game-class?
void Game::handleSettings(); //void is for example

From there you should figure out which settings have been modified and set them ;)
Last edited on
Actually i dont think i can make it work. I just remembered when the state switches from settings back to the title settings is deleted. If anyone has ever made a game with settings could you tell me how i would do it for my next project?
Topic archived. No new replies allowed.