- Home /
Trouble with understanding code that uses "==" operator.
Hi there,
I'm following a tutorial on Youtube where the tutor is coding a script that detects whether the player has collided with obstacles. (I want to avoid overwhelming anyone who might read this post with information so I've simplified the coding that the tutor writes.)
In the script the tutor created a struct called CollisionInfo that contains four boolean variables called above,below,left and right:
public struct CollisionInfo{
public bool above,below;
public bool right,left;
}
He creates a reference to the struct at the top of the script by writing "CollisionInfo collisions" and then writes the following code further down the script involving an if statement which uses the "==" operators:
if (hit) {
collisions.above == directionY == 1;
collisions.below == directionY == -1;
}
The hit variable refers to a RaycastHit2D variable. Basically it is saying that if a raycast has hit an obstacle then hit is true. I'm finding it difficult to understand exactly what the two lines of code within the if statement mean? I assume that what it means is that if the directionY variable is equal to 1 then then the boolean value called "above" that resides in the CollisionInfo struct will equal to true?
I've looked for ages online trying to understand what the == operator means in this context but I still don't understand.
I would appreciate if someone would be kind enough to explain what does two lines of code mean.
Kind regards
Answer by sath · May 04, 2020 at 03:17 PM
I believe that you are trying to find something that does not exist - or I have failed as a programmer. The code is wrong. Below is the correct one
if (hit)
{
//means that above is true if directionY is 1
collisions.above = directionY == 1;
//means that below is true if directionY is -1
collisions.below = directionY == -1;
}
So its just set's the current state of collision, up or down
Thanks very much for your helpful answer. The tutor must have made a mistake regarding the equal signs.
Am I right in saying that the line "collisions.above = directionY == 1;" is some sort of ternary operator? The line seems to me to be a kind of short hand if statement?
In this case, collisions.above = directionY == 1;
is equivalent to collisions.above = (directionY == 1);
. collisions.above is a boolean, and it is being set to true if directionY == 1
and false otherwise. I wouldn't say that it's short hand for an if statement, but could be written as one.
if(directionY==1) {
collisions.above = true;
}
else {
collisions.above = false;
}
But what is the significance of using the "==" operator?
Why would you write "directionY == 1" ins$$anonymous$$d of "directionY = 1"?
I know this is a year old question but I was wondering why did the original coder decide to store the four boolean values in a struct as opposed to an enum? Would it have been possible for them to store "above,below,right, left" in an enum?
Well, technically it may be possible that above and right are both true at the same time. It's just the opposite cases which are mutually exclusive. Though of course it depends on where the direction comes from and if it's actually possible to have diagonal movement. If there are only 4 mutually exclusive directions then yes, using an enum with the 4 directions would be enough.
Answer by unity_ek98vnTRplGj8Q · May 04, 2020 at 03:20 PM
This looks like a mistake to me... likely the code is supposed to be collisions.above = directionY == 1;
, which would make sense. The code as is does nothing, there is no assignment or function call.
Thanks for the reply. I withheld a lot of the code when creating my post as I didn't want to overwhelm people with numerous lines of code.