If statement with 2 booleans?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

If statement with 2 booleans?

Post 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 :o
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: If statement with 2 booleans?

Post by szensk »

Code: Select all

if not inPause or not inMenu then playerUpdate() end
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: If statement with 2 booleans?

Post 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

Code: Select all

if not (inPause and inMenu) then
to save a word :neko:
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

Re: If statement with 2 booleans?

Post 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
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: If statement with 2 booleans?

Post 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
Rockford
Prole
Posts: 26
Joined: Fri Mar 14, 2014 9:40 pm
Location: USA

Re: If statement with 2 booleans?

Post 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.
User avatar
SiENcE
Party member
Posts: 792
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: If statement with 2 booleans?

Post by SiENcE »

You better use gamestates instead of if's and else and not's :).

Here is a game template.
Post Reply

Who is online

Users browsing this forum: No registered users and 64 guests