Page 1 of 1
If statement with 2 booleans?
Posted: Mon Jun 02, 2014 4:05 am
by Rockford
I am having a problem, and I cant figure out what to search for to find the correct answer.
Lets say I wanted to do this:
Code: Select all
if not inPause then
playerUDPATE()
end
if not inMenu then
playerUDPATE()
end
How can I consolidate that into one if?
I know that booleans act differently with
and and
or and so what I thought would work, didnt. Its bugging me because I know I have seen the answer somewhere before
Re: If statement with 2 booleans?
Posted: Mon Jun 02, 2014 4:54 am
by szensk
Code: Select all
if not inPause or not inMenu then playerUpdate() end
Re: If statement with 2 booleans?
Posted: Mon Jun 02, 2014 6:02 am
by T-Bone
I typically put some extra parentheses around "not" statements for clarity, like this:
Code: Select all
if (not inPause) or (not inMenu) then
Not necessary at all but I find it much easier to read.
You could also do
to save a word
Re: If statement with 2 booleans?
Posted: Mon Jun 02, 2014 6:33 pm
by Rockford
Thanks guys. The first two didnt work. That is actually what I tried before posting, and what had me frustrated. It would only stop the updating if both the booleans were true -- even if I put or inbetween them.
The second one though, T-Bone, did work (with the and replaced with or of course).
Any idea why the first example doesnt work? I checked and I am not altering the booleans anywhere else, so there isnt a conflict happening.
Anyways, thanks though, got it working at least!
Rockford
Re: If statement with 2 booleans?
Posted: Mon Jun 02, 2014 9:15 pm
by DaedalusYoung
Because if you check if the game is not paused
or the game is not in menu, then when you are paused, but not in menu, the statement will still return true and thus will run the update function. You should use
and instead. When you're stuck, just make the classic truth table:
Code: Select all
or | not inPause | not inMenu | result
------------------------+-------------+------------+--------
game paused and in menu | false | false | false
game paused | false | true | true
game in menu | true | false | true
game playing | true | true | true
and | not inPause | not inMenu | result
------------------------+-------------+------------+--------
game paused and in menu | false | false | false
game paused | false | true | false
game in menu | true | false | false
game playing | true | true | true
You only want the statement to be true if the game is playing normally, not paused and not in menu, so look at the table, you'll see that only happens if you use "(not inPause) and (not inMenu)", not
or. It's because you're negating. If you didn't check for
not inPause, then you would've used
or, for example:
Code: Select all
if inPause or inMenu then
-- do nothing
else
gameupdate(dt)
end
Re: If statement with 2 booleans?
Posted: Tue Jun 03, 2014 11:47 pm
by Rockford
Thanks Daedalus for explaining that! I can see now where I was going wrong.
Never knew of that chart, but it will be every helpful.
Re: If statement with 2 booleans?
Posted: Wed Jun 04, 2014 2:33 pm
by SiENcE
You better use
gamestates instead of if's and else and not's
.
Here is a
game template.