- Home /
if ((this || that) && (that || this)).. Does that work?
Hey, I was trying to write some code to show the scoreboard when player 1 and 2 either don't finish or finish. So far I got this:
if ((player1nf || player1Finished) && (player2nf || player2))
{
showScoreBoard();
}
Although it didn't give me any errors, I have a feeling the syntax of the if() isn't correct. I was wondering how I would use ||'s along with &&'s to create something like I have in the title, if that makes any sense.
Thanks a ton.
I'm not going to post an answer as a perfectly valid one is already there.
But, assu$$anonymous$$g you have the right variable name(player2, which is not the same as the ones in player1's vars), and that you indeed are setting those variables correctly, than the statement is valid and should work.
The reason it should work like you think is because of your parenthesis. It separates two sides of an AND logic gate, and you happen to have two separate OR logic gates, one on each side of the AND. You could have something else, like one single bool and the other an OR, or basically whatever.
$$anonymous$$y question here could be simply whether this is the best way to do what you want. It is pretty simple so could work fine, but for example, you maybe could ins$$anonymous$$d of this, have a single variable for the "state" of a player, where 0 could be "playing" 1 could be "done/lost" and 2 could be "done/won" where lose and win is deter$$anonymous$$ed by whether they "finished." In fact, you could use an enum(look it up) to control these variables.
This is just a general program$$anonymous$$g question. Ifs, Ands and Ors in Unity work the same as anywhere else.
You should be able to look-up and get more better detail on If tricks from general program$$anonymous$$g web sites.
Answer by Habitablaba · Nov 08, 2014 at 03:06 AM
(My answer got eaten?)
That is a totally valid if statement, but there is really no way to know if it does what you want it to do with the information given.
Logical operators follow the "inside-to-out" order of operations, just like arithmetic operators.
Consider the following:
if((a||b) && (c||d))
a||b will evaluate first. True, if either a or b is true.
We will call this A for now. c||d will evaluate next. True, if either c or d is true.
We will call this C for now.
This leaves us with
if(A && C)
which will evaluate next. True only if both A and C are true.
This means for your specific situation, that each grouping needs to have at least one true in it to drop into the if block.
Does that clear it up a bit?
You have a small error in your sequence ;)
a || b will evaluate first, let's call this A
If A evaluated to false we're done here and the if block will not be executed. c and d aren't evaluated at all
If A evaluates to true it will continue to evaluate the right-hand operand of our && operator which is also a bracket term
If what's inside the brackets evaluates also to true the if is executed.
What you have explained would happen with the "&" operator. The conditional "and" operator && quits as soon as the outcome is clear.
Same for the conditional "or" operator || , however there it's of course reversed. If the left-hand operand is true it won't even evaluate the right-hand operand. However if the left hand operand is false it will look at the right-hand operand as well.
For plain variables that's actually quite irrelevant. However if you have a method call inside the bracket it will be important. For example:
bool someVar = true;
if (someVar || Physics.Raycast(ray, out hit))
{
// ...
}
Here it won't call Physics.Raycast at all because someVar is true. That means inside the if-body hit is not being set if someVar is true.
You might want to have a look at the "Operator precedence and associativity" page which also shows the order of the operators. In our example here the brackets are important because the && operator is evaluated before ||. That means
if(a || b && c || d)
is acutally
if(a || (b && c) || d)
The above logic is a bullseye .. But just to add you must check if the logical context you desire is actually in line with the above logic. I am a bit confused to the nature of the variables and their settings for your purpose... You must also ask yourself if the variables you used in the if statement share a logical relation. You must be very careful if you are using interdependent variables...
Example.
Player1finished =!player1nf
Red
@Bunny83 I intentionally left out short cutting because I figured if they are asking this type of question, then the very basics will do fine. You are, of course, correct though.
+1 for the link to operator precedence, definitely something that should be kept in $$anonymous$$d, especially as you start to learn 'clever' program$$anonymous$$g tricks.
Thanks this clears it up, my code was fine but if one isn't true it won't continue.
Answer by Redeemer86 · Nov 08, 2014 at 08:10 AM
simply use ...
if(player1finished || player2finished)
Red
That wasn't my question, and isn't usable for what I'm doing since I want to detect if the players have not finished or died also.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How are the if's in a GUI.Toolbar handled? 1 Answer
How to Less Than or Equal to with a Raycast? 1 Answer
Multiplayer Support for Android 1 Answer
Targetting script bug. (Javascript) 0 Answers