Sign in to follow this  
Adam West

simple c++ bool question

Recommended Posts

Hey there.

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

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


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

SYS->hideInGameMenu();

};
[/CODE]

the problem is i want to keep showInGame running until the escape key is pressed again, how would i go about doing this?

Share this post


Link to post
Share on other sites
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. Edited by szecs

Share this post


Link to post
Share on other sites
just tried this so now it stays down, i thought this might work with releasing it again but no dice.

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

};
[/CODE]

Share this post


Link to post
Share on other sites
[CODE]
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();

};
[/CODE]
This should do it.

But I don't know how the rest of your app works.

Share this post


Link to post
Share on other sites
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 [url="https://encrypted.google.com/search?q=irrlichty%20key%20press"]light Googling[/url] suggests that [url="http://irrlicht.sourceforge.net/docu/example004.html"]implementing the IEventReceiver interface[/url] might support this use. Edited by rip-off

Share this post


Link to post
Share on other sites
In the same line as what Shenaynay just posted, instead of writing

[code]if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = true;
else
showGameMenu = false;[/code]

you can do

[code]showGameMenu = receiver.IsKeyDown(irr::KEY_ESCAPE);[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Adam West' timestamp='1339105688' post='4947202']
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.
[/quote]
In order to do this you need to track the previous state of the button.

Share this post


Link to post
Share on other sites
[quote name='Adam West' timestamp='1339105688' post='4947202']
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.
[/quote]

Okay. there are two usual ways for input handling, you are doing one of them. I'm not sure which, because I'm not familiar with the library you are using.

One way: Event handling. Very simply put: whenever an input event occurs, like pressing a key, an "event is fired". There is a method/function that gets called when this event occurs, that handles the event. Since an event is an event, it happens only when you push the button, and the associated function is only called in case of that event (details are not so simple, but you get the idea). This method is ideal for the thing you want to do, since you want a thing to happen (changing state) when the specific event occurs.

the other: Polling: directly querying the sate of the button (pushed or not). Usually this querying is done in the game loop, so you are continuously checking the button state, which obviously not so good for events, like a keypress. Because you only know the state of the button, but don't know when the state change happened. This can be solved with a separate variable, that stores the key state. More on this later. (This type of input handling is good for continuous things, like running, steering in a game).

I hope it makes sense, it's important to grasp the difference between the two methods. One cares about state changes, one cares about states.

Your system, based on the function names is polling the input. To solve the said issue with this, you store the state of the key, so you can check if it has changed since the last time you queried its state.

Pseudocode:

[font=courier new,courier,monospace]key_state

in your main loop
{ new_key_state = IsKeyDown([/font][font=courier new,courier,monospace]KEY_ESCAPE[/font][font=courier new,courier,monospace]);
[color=#0000FF]if[/color]( new_key_state != key_state && new_key_state == pushed)
{
[color=#008000]// event!! the key state is changed and the key is is pushed, we can act[/color]

showGameMenu = !showGameMenu;[color=#008000] //we are toggling[/color]
}
key_state = new_key_state; [color=#008000]// we store the key state[/color]
}[/font]

See? you need an extra variable.

But as stated above, check the documentation of the library you are using, there may be a build in function for that already, that internally has this extra variable.
something like
[font=courier new,courier,monospace][color=#0000ff]bool[/color] IsButtonChanged([color=#0000ff]int [/color]key)[/font] or whatever. Edited by szecs

Share this post


Link to post
Share on other sites
actually, i figured it out myself at school, its quite a cool way too and its not as error prone as polling

[CODE]

if(showGameMenu)
{
showInGameMenu();
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = false;
}
else
{
hideInGameMenu();
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = true;
}
[/CODE]

just leaving it here incase others have the same problem

Share this post


Link to post
Share on other sites
It would be awesome if we knew what those functions are actually doing without the need to look them up. Maybe you don't know it either, and just shooting in the dark which may be a bit of a problem.

Or some more info about the whole stuff you are doing. What you posted here might work, but it looks messy to me.

We don't even know what show/hideIngameMenu does. Does it simply renders the menu, or does it also has its internal states to indicate if the menu has to be rendered (somewhere else in the source code)? Does it do initialization/deinitialization stuff? Edited by szecs

Share this post


Link to post
Share on other sites
[quote name='Adam West' timestamp='1339154443' post='4947329']actually, i figured it out myself at school, its quite a cool way too and its not as error prone as polling[/quote]

It's still polling - this is actually (a minor variation on) the solution proposed several times earlier in the thread.

Share this post


Link to post
Share on other sites
I understand you already have your answer, but with cases like this, it would really be better to use a switch statement to prevent the use of if statements bumping into each other. That way, you can break out of the switch statement without it falling through other cases and if statements if you so choose. Definitely get used to using switch statements now, they can be your best friend.

Share this post


Link to post
Share on other sites
[quote name='Adam West' timestamp='1339154443' post='4947329']
actually, i figured it out myself at school, its quite a cool way too and its not as error prone as polling

[CODE]

if(showGameMenu)
{
showInGameMenu();
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = false;
}
else
{
hideInGameMenu();
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
showGameMenu = true;
}
[/CODE]

just leaving it here incase others have the same problem
[/quote]

How does that fix the problem? If you hold down ESC, it will just switch between menu states every time through. You need to do what szecs proposed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this