That won't work (at least not in SFML).
What you're most likely looking for is getting some state machine set up that supports stacks of states rather than a single state at a time.
Some quick made up example:
class State {
public:
virtual bool update(float delta) {
return true;
}
virtual draw(sf::RenderTarget *target) {}
}
std::vector<std::unique_ptr<State>> states;
// Game starts
states.push_back(std::make_unique<PlayingState>());
// Inside PlayingState to create the UI
states.push_back(std::make_unique<UIState>());
// Inside PlayingState, when hitting the Pause Button
states.push_back(std::make_unique<PauseState>());
// Inside PauseState, when resuming/ending the pause
states.pop_back();
// -----
// Drawing the states:
for (auto &a = states.begin(); a != states.end(); ++a)
states.draw(&mySfmlWindow);
// Updating the states:
for (auto &a = states.rbegin(); a != states.rend(); ++a)
if (!states.update(delta))
break;
This will essentially create a state stack, which could look like this, while the game is paused:
- PlayingState
- UiState
- PauseState
When drawing, you walk through from beginning to end, essentially first drawing the actual game, then the UI, then the pause message.
When updating your game state, it goes the reverse order: First updating PauseState, then UiState, and PlayingState last. But these updates only propagate, if update() returns true.
So to pause the game, all your PauseState has to do is return false, which will halt all game states "below" it.