simple c++ bool question

Started by
15 comments, last by BeerNutts 11 years, 10 months ago
Hey there.

this is a snippet from my game engine which handles the key being pressed down.


void redNovember::inputHandle()
{
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = true;
else
showGameMenu = false;


//menu shit
if (showGameMenu == true)
{
SYS->showInGameMenu();
}
else

SYS->hideInGameMenu();

};


the problem is i want to keep showInGame running until the escape key is pressed again, how would i go about doing this?
Advertisement
while(showGameMenu)
{
// stuff
}
you need to "toggle" ShowGameMenu if I understand your intentions.
This means whenever the appropriate key is pressed, change its state from true to false or vice-versa (if the input is event based). If you poll the input state, then the problem becomes a bit more complex, since you have to store the state of the key separately.
just tried this so now it stays down, i thought this might work with releasing it again but no dice.


void redNovember::inputHandle()
{
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = true;
while(showGameMenu == true)
{
SYS->showInGameMenu();
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = false;
}

};

void redNovember::inputHandle()
{
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = !showGameMenu; //invert current value, this should let you toggle between true and false by pressing esc.

//menu shit
if (showGameMenu == true)
{
SYS->showInGameMenu();
}
else
SYS->hideInGameMenu();

};

This should do it.

But I don't know how the rest of your app works.
szecs hit the nail on the head. If you are actively polling, you'll need to store state between calls - unless the library you are using (Irrlicht?) provides such functionality. Some light Googling suggests that implementing the IEventReceiver interface might support this use.
By the way op, rather than writing:

if (someBool == true) or if (someBool == false), you can do:

if (someBool) or if (!someBool)
In the same line as what Shenaynay just posted, instead of writing

if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = true;
else
showGameMenu = false;


you can do

showGameMenu = receiver.IsKeyDown(irr::KEY_ESCAPE);
Mis-read.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

i don't really understand the IEventReciever stuff. but all i want to do is that when escape is pressed it will open the gui and it wont close it until escape is pressed again.

This topic is closed to new replies.

Advertisement