1 00:00:00,350 --> 00:00:04,940 According to the rules of the game, towers can't be placed directly on the path. 2 00:00:04,940 --> 00:00:08,440 We need a way to know if a map location is on the path. 3 00:00:08,440 --> 00:00:12,520 Let's add another method to the Path class that will return true, 4 00:00:12,520 --> 00:00:15,320 if the map location passed in, is on the path. 5 00:00:15,320 --> 00:00:19,293 So we'll say public bool IsOnPath and 6 00:00:19,293 --> 00:00:23,770 it will take a MapLocation as a parameter. 7 00:00:26,090 --> 00:00:31,510 And this method, will loop through all of the locations in the path array. 8 00:00:31,510 --> 00:00:37,314 So we'll say, foreach(var pathLocation in _path, 9 00:00:40,066 --> 00:00:45,824 And if the location passed in equals one of the path locations, 10 00:00:48,738 --> 00:00:51,020 We'll return true immediately. 11 00:00:52,790 --> 00:00:56,860 Otherwise, if we get through all of the locations on the path and 12 00:00:56,860 --> 00:01:01,970 we get down here to the end of the four H loop we'll return false. 13 00:01:01,970 --> 00:01:05,420 Because we didn't find a matching location in the path array. 14 00:01:05,420 --> 00:01:09,760 To test this, let's create a map location that is on the path. 15 00:01:09,760 --> 00:01:12,177 For now, we'll just do this in the Game.cs file. 16 00:01:13,620 --> 00:01:15,160 So here we have our path. 17 00:01:15,160 --> 00:01:18,108 Under here, let's create a new MapLocation. 18 00:01:22,323 --> 00:01:26,179 And we'll have it be right at the beginning of the path, so 19 00:01:26,179 --> 00:01:27,686 right here at 0, 2. 20 00:01:31,042 --> 00:01:33,584 Just copy that down there. 21 00:01:33,584 --> 00:01:37,877 Now we can say if(path.IsOnPath and 22 00:01:37,877 --> 00:01:42,315 pass in that location we just created. 23 00:01:46,971 --> 00:01:50,546 Then on the console will print, 24 00:01:53,565 --> 00:02:00,786 location + " is on the path". 25 00:02:00,786 --> 00:02:06,824 And just so we don't continue down into our program we'll return right here. 26 00:02:06,824 --> 00:02:09,099 Let's go ahead and compile and run this. 27 00:02:18,828 --> 00:02:19,746 Hm. 28 00:02:19,746 --> 00:02:21,533 We didn't see our message. 29 00:02:21,533 --> 00:02:24,482 We should have seen this message printed to the console. 30 00:02:29,080 --> 00:02:33,360 And we shouldn't have seen any of this stuff because we would have returned. 31 00:02:33,360 --> 00:02:36,460 We must have a bug in the IsOnPath method. 32 00:02:36,460 --> 00:02:37,250 Let's take a second look. 33 00:02:37,250 --> 00:02:41,320 On the surface it looks correct, doesn't it? 34 00:02:41,320 --> 00:02:43,860 There's something very subtle happening here. 35 00:02:43,860 --> 00:02:50,290 This double equals sign checks to see if two variables refer to the same object. 36 00:02:50,290 --> 00:02:54,160 This is different than two locations on the map being the same. 37 00:02:54,160 --> 00:02:58,843 Let's go back to Game.cs to see the MapLocation objects that should have 38 00:02:58,843 --> 00:03:01,648 caused the IsOnPath method to return true. 39 00:03:06,396 --> 00:03:09,244 Here's the MapLocation that's in the path array, and 40 00:03:09,244 --> 00:03:12,160 here's the MapLocation that we're checking against. 41 00:03:13,250 --> 00:03:18,030 They may look the same, but these are actually two very different objects. 42 00:03:18,030 --> 00:03:22,780 See, here we created a new MapLocation and put it in this array, and 43 00:03:22,780 --> 00:03:26,610 down here we created a completely different MapLocation. 44 00:03:26,610 --> 00:03:30,520 They just so happen to refer to the same location on the map. 45 00:03:30,520 --> 00:03:32,850 It's like they're identical twins. 46 00:03:32,850 --> 00:03:36,030 They aren't the same person, but they look identical. 47 00:03:36,030 --> 00:03:38,530 The equality operator by default, 48 00:03:38,530 --> 00:03:42,740 only checks to see if they are the exact same object. 49 00:03:42,740 --> 00:03:45,880 Or in the case of twins, the exact same person. 50 00:03:45,880 --> 00:03:50,130 On the computer, these two objects sit in different locations in memory. 51 00:03:50,130 --> 00:03:55,970 The equality operator actually just calls the static, system.object.ReferenceEqual 52 00:03:55,970 --> 00:04:00,480 method to determine if two variables refer to the same location in memory. 53 00:04:00,480 --> 00:04:03,130 In which case, they are the same object. 54 00:04:03,130 --> 00:04:06,400 If two variables refer to the same object in memory, 55 00:04:06,400 --> 00:04:09,760 we can say that they have reference equality. 56 00:04:09,760 --> 00:04:12,620 That's all that double equals equality operator checks for.