|| Expression

G

Geo Jones

Guest
Alright so I am trying to make it so if this object is not colliding with a list of objects, then send a debug message. This is in the step command. It is always showing the debug message even if it is touching one of the list of objects.


if (!place_meeting(x, y, obj1_obj) || !place_meeting(x, y, obj2_obj) || !place_meeting(x, y, obj3_obj) || !place_meeting(x, y, obj4_obj) || !place_meeting(x, y, obj5_obj)){
show_debug_message("no collision");
}
}

So essentially I want to make it so if it is not colliding with obj1, obj2, obj3, obj4, or obj5; to display "no collision"
 
G

Geo Jones

Guest
Thanks that worked! I am just confused why though. Dosent || mean or? so If they are not colliding with obj1 or obj2 or obj3 or obj4 or obj5 then signal alarm.
 

Mick

Member
|| means or, so in your case the problem was that if any one of the collision checks were false then show_debug_message("no collision"); got executed. You wanted to check that ALL of the collision checks were false, hence the &&.
 

TheouAegis

Member
Your code doesn't say "if they are not colliding with obj1 or obj2 or obj3 or obj4", it says "if they are not colliding with obj1 or they are not colliding with obj2 or they are not colliding with obj3 or they are not colliding with obj4".

That's a biiiiiiiiiig difference in logic.
 

chamaeleon

Member
Think of the between the two statements "X is not meeting Y AND X is not meeting Z" and "X is not meeting Y OR X is not meeting Z". If Y and Z are not in the same position, in the case of OR your if statement will have a true value if X is not meeting Y OR X is not meeting Z. In the case of AND, it will only be true if X is meeting neither Y nor Z. In other words, in the case of OR, not matter how "much" X is meeting Y, if it's not meeting Z as well (as in AND/&&), your original test expression will be true.

Another way to look at it.. Imagine two doors. In the case of OR, the two doors are in the same wall, and if either door ("Door A OR Door B") is open (meaning "!place_meeting" being true) you can walk through/enter the block statements for the if statement. In the case of AND, imagine the two doors being one after another in a corridor, and both ("Door A AND Door B") must be open (meaning "!place_meeting" being true) for you to go all the way through.
 
I

IndieGameMaker

Guest
With || (OR) only one side needs to be true for the expression to be accepted as true.
With && (AND) both sides need to be true for the expression to be accepted as true.

Truth Table:
____________________________________
|___ p ___|___ q ___|__ p ^ q __|__ p v q __|
|___ 1 ___|___ 1 ___|____ 1 ___|____ 1 ___|
|___ 1 ___|___ 0 ___|____ 0 ___|____ 1 ___|
|___ 0 ___|___ 1 ___|____ 0 ___|____ 1 ___|
|___ 0 ___|___ 0 ___|____ 0 ___|____ 0 ___|

Key:
p = left hand side of expression
q = right hand side of expression
^ = AND
v = OR
1 = TRUE
0 = FALSE
 
Last edited by a moderator:
M

Maximus

Guest
What you were thinking was:

!(place_meeting(x, y, obj1_obj) || place_meeting(x, y, obj2_obj) || place_meeting(x, y, obj3_obj) || place_meeting(x, y, obj4_obj) || place_meeting(x, y, obj5_obj))
 
Top