Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
What does it mean for two objects to be equal?
-
0:00
According to the rules of the game, towers can't be placed directly on the path.
-
0:04
We need a way to know if a map location is on the path.
-
0:08
Let's add another method to the Path class that will return true,
-
0:12
if the map location passed in, is on the path.
-
0:15
So we'll say public bool IsOnPath and
-
0:19
it will take a MapLocation as a parameter.
-
0:26
And this method, will loop through all of the locations in the path array.
-
0:31
So we'll say, foreach(var pathLocation in _path,
-
0:40
And if the location passed in equals one of the path locations,
-
0:48
We'll return true immediately.
-
0:52
Otherwise, if we get through all of the locations on the path and
-
0:56
we get down here to the end of the four H loop we'll return false.
-
1:01
Because we didn't find a matching location in the path array.
-
1:05
To test this, let's create a map location that is on the path.
-
1:09
For now, we'll just do this in the Game.cs file.
-
1:13
So here we have our path.
-
1:15
Under here, let's create a new MapLocation.
-
1:22
And we'll have it be right at the beginning of the path, so
-
1:26
right here at 0, 2.
-
1:31
Just copy that down there.
-
1:33
Now we can say if(path.IsOnPath and
-
1:37
pass in that location we just created.
-
1:46
Then on the console will print,
-
1:53
location + " is on the path".
-
2:00
And just so we don't continue down into our program we'll return right here.
-
2:06
Let's go ahead and compile and run this.
-
2:18
Hm.
-
2:19
We didn't see our message.
-
2:21
We should have seen this message printed to the console.
-
2:29
And we shouldn't have seen any of this stuff because we would have returned.
-
2:33
We must have a bug in the IsOnPath method.
-
2:36
Let's take a second look.
-
2:37
On the surface it looks correct, doesn't it?
-
2:41
There's something very subtle happening here.
-
2:43
This double equals sign checks to see if two variables refer to the same object.
-
2:50
This is different than two locations on the map being the same.
-
2:54
Let's go back to Game.cs to see the MapLocation objects that should have
-
2:58
caused the IsOnPath method to return true.
-
3:06
Here's the MapLocation that's in the path array, and
-
3:09
here's the MapLocation that we're checking against.
-
3:13
They may look the same, but these are actually two very different objects.
-
3:18
See, here we created a new MapLocation and put it in this array, and
-
3:22
down here we created a completely different MapLocation.
-
3:26
They just so happen to refer to the same location on the map.
-
3:30
It's like they're identical twins.
-
3:32
They aren't the same person, but they look identical.
-
3:36
The equality operator by default,
-
3:38
only checks to see if they are the exact same object.
-
3:42
Or in the case of twins, the exact same person.
-
3:45
On the computer, these two objects sit in different locations in memory.
-
3:50
The equality operator actually just calls the static, system.object.ReferenceEqual
-
3:55
method to determine if two variables refer to the same location in memory.
-
4:00
In which case, they are the same object.
-
4:03
If two variables refer to the same object in memory,
-
4:06
we can say that they have reference equality.
-
4:09
That's all that double equals equality operator checks for.
You need to sign up for Treehouse in order to download course files.
Sign up