Reputation: 525
We have a label:
LABEL:
//Do something.
and we have a function. We want pass LABEL as a argument to this function (otherwise we can't access labels in a function), and in some condition we want to jump this label. Is it possible?
I'm giving a example (pseudocode) for clarification:
GameMenu: //This part will be executed when program runs
//Go in a loop and continue until user press to [ENTER] key
while(Game.running) //Main loop for game
{
Game.setKey(GameMenu, [ESCAPE]); //If user press to [ESCAPE] jump into GameMenu
//And some other stuff for game
}
Upvotes: 0
Views: 1561
Reputation: 1380
It seems like it may be worth refactoring your code to something similar:
void GameMenu() {
// Show menu
}
void SomethingElse() {
// Do something else
}
int main(int argc, char **argv) {
(...)
while(Game.running) {
int key = GetKey();
switch(key) {
case ESCAPE:
GameMenu();
break;
case OTHER_KEY:
SomethingElse();
break;
}
}
}
Upvotes: 1
Reputation: 14390
This sounds like an XY problem. You might want a state machine:
enum class State {
menu,
combat,
};
auto state = State::combat;
while (Game.running) {
switch (state) {
case State::combat:
// Detect that Escape has been pressed (open menu).
state = State::menu;
break;
case State::menu:
// Detect that Escape has been pressed (close menu).
state = State::combat;
break;
}
}
Upvotes: 5
Reputation: 8209
You can use setjmp()/longjmp()
to jump to some point in outer scope even to outer function. But note - jump target scope must be alive at the moment of jump.
Upvotes: -1