1 00:00:01,090 --> 00:00:01,420 ‫All right. 2 00:00:01,420 --> 00:00:05,650 ‫So in this lecture, you will learn about multicast, delegates and events. 3 00:00:05,650 --> 00:00:07,750 ‫So let's first explore the problem. 4 00:00:07,870 --> 00:00:10,430 ‫Imagine we're developing a video game from scratch. 5 00:00:10,450 --> 00:00:15,490 ‫Our code is a combination of graphics and audio libraries and the logic for our player. 6 00:00:15,490 --> 00:00:17,230 ‫So let's start coding it. 7 00:00:17,350 --> 00:00:19,660 ‫Consider the following simple classes. 8 00:00:19,660 --> 00:00:25,930 ‫So first of all, we have this rendering class which at the start of the game we want to enable the 9 00:00:25,930 --> 00:00:28,330 ‫rendering engine and start drawing the visuals. 10 00:00:28,330 --> 00:00:33,370 ‫So it's going to render and then it's going to start drawing visuals. 11 00:00:33,370 --> 00:00:37,570 ‫And also when the game is over, we want to stop our rendering engine. 12 00:00:37,570 --> 00:00:42,850 ‫So we have this game over method which will then stop the rendering engine. 13 00:00:42,850 --> 00:00:45,190 ‫So here I'm just using console right line statements. 14 00:00:45,190 --> 00:00:49,660 ‫This would of course be a lot more complex code and this would then actually do something. 15 00:00:49,660 --> 00:00:55,540 ‫In our case, we are just trying to understand the concept of multicast delegates as well as events. 16 00:00:55,600 --> 00:00:59,560 ‫That's why we're just going to display it via a red line statement on the console. 17 00:00:59,680 --> 00:01:01,660 ‫So that's our rendering class. 18 00:01:01,660 --> 00:01:04,390 ‫Now let's look at our audio system class. 19 00:01:04,390 --> 00:01:11,080 ‫So here in the audio system, we also have this dart game method which will just start the audio system 20 00:01:11,080 --> 00:01:12,640 ‫and play some audio. 21 00:01:12,640 --> 00:01:16,810 ‫And then we have the game over method which will basically stop the audio system. 22 00:01:17,200 --> 00:01:23,710 ‫And then finally we have a player and the player has a player name and we have this simple constructor 23 00:01:23,710 --> 00:01:29,470 ‫where we just set that name based on what was passed to the object that was created. 24 00:01:29,470 --> 00:01:33,310 ‫And here we also have the start game as well as the game over methods. 25 00:01:33,700 --> 00:01:38,260 ‫So this one is going to spawn the player with an ID and this one is just going to remove the player 26 00:01:38,260 --> 00:01:42,430 ‫with that particular ID that we assigned as the player name. 27 00:01:43,420 --> 00:01:47,800 ‫So these are our three classes and I'm going to keep it rather simple with these. 28 00:01:47,800 --> 00:01:51,280 ‫So let's go ahead and go over to our main method. 29 00:01:51,280 --> 00:01:56,500 ‫So inside of our main method, we are now going to create objects of those systems. 30 00:01:56,500 --> 00:02:02,710 ‫So first of all, the audio system and then of the rendering engine, so very simple, just simple objects 31 00:02:02,710 --> 00:02:03,130 ‫here. 32 00:02:03,130 --> 00:02:07,150 ‫And then we also create two players with the ID. 33 00:02:07,150 --> 00:02:12,220 ‫So we have this player one which is CAO, and then player two, which is going to be De Silva. 34 00:02:12,460 --> 00:02:18,370 ‫So we need to start our rendering engine, our audio system and spawn the players by calling the start 35 00:02:18,370 --> 00:02:20,590 ‫game method from each of the class. 36 00:02:20,590 --> 00:02:22,030 ‫So let's just do that. 37 00:02:22,760 --> 00:02:24,350 ‫And I'm going to do it this way. 38 00:02:24,350 --> 00:02:29,780 ‫So I'm just going to say audio system, start game rendering engine don't start game player one and 39 00:02:29,780 --> 00:02:31,250 ‫player to start game each. 40 00:02:31,370 --> 00:02:36,230 ‫And then we just say game is running press any key to end the game. 41 00:02:36,230 --> 00:02:44,540 ‫So what we want to do now is we want to basically end the game once the player has pressed the button. 42 00:02:44,990 --> 00:02:46,730 ‫So it would of course be a lot more complex. 43 00:02:46,730 --> 00:02:50,360 ‫Let's say the player presses all the for or in the options menu. 44 00:02:50,390 --> 00:02:53,810 ‫He closes the game, then the next code would run. 45 00:02:53,810 --> 00:02:59,690 ‫So the next code would be of course to call the audio system game over, method rendering engine game 46 00:02:59,690 --> 00:03:02,960 ‫over method as well as player one and player two that game over methods. 47 00:03:03,350 --> 00:03:05,210 ‫So let's take care of that. 48 00:03:05,210 --> 00:03:07,670 ‫Let's add that in here as well. 49 00:03:07,670 --> 00:03:12,500 ‫And by the way, we will need a read statement because otherwise it will directly shut it down. 50 00:03:12,500 --> 00:03:21,590 ‫So let me quickly add a read statement here so that we can actually manually press a key to then shut 51 00:03:21,590 --> 00:03:24,890 ‫down everything and basically game over. 52 00:03:24,890 --> 00:03:28,190 ‫So let's run this real quick and see what's going to happen. 53 00:03:28,190 --> 00:03:33,440 ‫So we see the audio system started, audio is playing, the rendering engine started, we now see the 54 00:03:33,440 --> 00:03:35,480 ‫visuals and then the players are spawn. 55 00:03:35,480 --> 00:03:37,910 ‫So player one and player two are spawned. 56 00:03:37,940 --> 00:03:40,040 ‫Now the game is running and we can play around with it. 57 00:03:40,040 --> 00:03:42,230 ‫And then once we want to finish it, we press a key. 58 00:03:42,230 --> 00:03:43,640 ‫So I'm going to press a key. 59 00:03:43,640 --> 00:03:48,920 ‫And you see the rendering engine stopped, the audio system stopped and we removed both players. 60 00:03:48,920 --> 00:03:52,040 ‫So this was quite an effort to set this up. 61 00:03:52,040 --> 00:03:56,870 ‫Now I imagine we have to do this with a lot more things and there could a couple of things could go 62 00:03:56,870 --> 00:03:57,590 ‫wrong, right? 63 00:03:57,680 --> 00:04:03,770 ‫So let's say we add more systems, then this becomes a lot more complex so we can easily forget to call 64 00:04:03,770 --> 00:04:06,230 ‫the start game or game over method. 65 00:04:06,230 --> 00:04:11,870 ‫Or even worse, we could call one of these methods twice, which could cause all kinds of problems and 66 00:04:11,870 --> 00:04:15,020 ‫require us to place a lot of protective code. 67 00:04:15,020 --> 00:04:18,980 ‫So we would have to really make sure, is this object alive? 68 00:04:19,010 --> 00:04:24,620 ‫Does it have called this method or has it had have called it this method and so forth, way too complicated. 69 00:04:24,620 --> 00:04:27,680 ‫And this is where multicast delegates come in handy. 70 00:04:27,710 --> 00:04:33,710 ‫A multicast delegate is a delegate that works as a list where it can store a reference to more than 71 00:04:33,710 --> 00:04:36,560 ‫one method, and that's basically it. 72 00:04:36,770 --> 00:04:41,240 ‫Okay, this sounds rather complex, but let's build it and then it will probably make more sense. 73 00:04:41,240 --> 00:04:45,560 ‫So let's create a static class called Game Event Manager. 74 00:04:45,740 --> 00:04:46,460 ‫So. 75 00:04:47,210 --> 00:04:47,540 ‫All right. 76 00:04:47,540 --> 00:04:49,580 ‫So let's go ahead and create a new. 77 00:04:50,290 --> 00:04:51,370 ‫Class here. 78 00:04:53,840 --> 00:04:58,880 ‫And I'm going to call this one game event manager. 79 00:04:59,810 --> 00:05:00,140 ‫All right. 80 00:05:00,140 --> 00:05:03,530 ‫So this game event manager will now have a delegate. 81 00:05:03,530 --> 00:05:08,510 ‫So I'm going to create a new delegate called Game Event. 82 00:05:08,510 --> 00:05:13,010 ‫So it's not going to return anything, but it's going to be of type delegate and it's going to be public 83 00:05:13,010 --> 00:05:15,200 ‫so we can use it outside of this class. 84 00:05:15,650 --> 00:05:22,070 ‫So we've seen how delegates in general work, and we're going to use this delegate now to allow us to 85 00:05:22,070 --> 00:05:26,450 ‫basically add more than just one event to it. 86 00:05:26,450 --> 00:05:33,860 ‫So now let's create the two variables called on game start and on game end, and they will be of type 87 00:05:33,860 --> 00:05:34,760 ‫game event. 88 00:05:34,760 --> 00:05:37,340 ‫So they will be off the type of our delegate. 89 00:05:37,670 --> 00:05:43,550 ‫So this will be a game event delegate and this will be a game event delegate. 90 00:05:43,550 --> 00:05:46,910 ‫So we have these two delegate variables that we can now use. 91 00:05:47,240 --> 00:05:49,370 ‫So back to our three classes. 92 00:05:49,370 --> 00:05:56,780 ‫Let's add the start game and the game over methods to the on game start and on game over delegates. 93 00:05:56,930 --> 00:05:59,990 ‫Also, there is no need for these methods to be public. 94 00:05:59,990 --> 00:06:06,920 ‫So let's go over to the audio system and now let's make those two methods private because we don't need 95 00:06:06,920 --> 00:06:16,640 ‫to expose them as we are going to use the constructor of the audio system in order to add or subscribe 96 00:06:16,640 --> 00:06:17,660 ‫to the events. 97 00:06:17,660 --> 00:06:25,340 ‫So here the best place to add our methods to the game manager delegates is from within the constructor 98 00:06:25,340 --> 00:06:26,000 ‫itself. 99 00:06:26,420 --> 00:06:27,650 ‫That's what we're doing here. 100 00:06:27,950 --> 00:06:34,130 ‫And when an object gets created, it's methods will automatically be added to the delegates, or in 101 00:06:34,130 --> 00:06:36,860 ‫other words, they will subscribe to them. 102 00:06:36,860 --> 00:06:43,460 ‫So what is going to happen now is that our start game and our game over method are going to be added 103 00:06:43,460 --> 00:06:48,290 ‫to the game event manager on game start delegate. 104 00:06:48,620 --> 00:06:56,240 ‫And we're using the plus equals sign here in order to say, okay, we want to add this method to that 105 00:06:56,240 --> 00:06:57,530 ‫multicast delegate. 106 00:06:57,530 --> 00:07:03,110 ‫So basically we are subscribing to it and we are waiting for that event to be triggered because once 107 00:07:03,110 --> 00:07:06,380 ‫that event will be triggered, our method will be called. 108 00:07:07,460 --> 00:07:14,270 ‫So the cool thing is we can now do the same thing with the rendering engine class and the player class. 109 00:07:14,270 --> 00:07:19,310 ‫So what's important here is I'm using the plus equal sign and not just the equal sign, so I'm not assigning 110 00:07:19,310 --> 00:07:19,790 ‫it. 111 00:07:19,790 --> 00:07:24,890 ‫And we're going to see later what a signing would mean and what it will do for us. 112 00:07:24,890 --> 00:07:28,310 ‫But here we are just adding another. 113 00:07:29,240 --> 00:07:34,460 ‫Method to our event collectors, so to speak, or our event manager. 114 00:07:35,390 --> 00:07:39,410 ‫So it's collecting all of those events or methods that it should then execute. 115 00:07:39,410 --> 00:07:42,530 ‫So it's a multi cast delegate. 116 00:07:42,830 --> 00:07:46,180 ‫Now let's do the same thing with our rendering engine. 117 00:07:46,190 --> 00:07:47,750 ‫I would like you to do it yourself. 118 00:07:47,750 --> 00:07:53,120 ‫So pause the video real quick and try to assign the same code or build up the same code as we have done 119 00:07:53,120 --> 00:07:54,830 ‫in the audio system here. 120 00:07:56,060 --> 00:08:00,980 ‫So first of all, I made sure that my methods are going to be private because we don't need to expose 121 00:08:00,980 --> 00:08:01,860 ‫them anymore. 122 00:08:01,880 --> 00:08:05,990 ‫As that's not necessary, we will never call them outside of this class. 123 00:08:05,990 --> 00:08:09,110 ‫That's why it's always a good practice to make them private. 124 00:08:09,290 --> 00:08:12,080 ‫What we can call, however, is the constructor, right? 125 00:08:12,080 --> 00:08:15,470 ‫So we can create an object of our rendering engine as it is public. 126 00:08:15,470 --> 00:08:22,010 ‫So now we can go ahead and add our start game method to the on game start of our game event manager 127 00:08:22,010 --> 00:08:22,580 ‫as well. 128 00:08:22,580 --> 00:08:24,260 ‫And the same goes for the game over. 129 00:08:24,260 --> 00:08:26,240 ‫So here we're also using plus equals. 130 00:08:26,240 --> 00:08:32,540 ‫So we're saying we are subscribing to the on game start and on game over events. 131 00:08:32,870 --> 00:08:37,340 ‫So we're basically saying once the on game start event will be triggered. 132 00:08:37,340 --> 00:08:39,260 ‫Also called our start game method. 133 00:08:39,260 --> 00:08:39,740 ‫Please. 134 00:08:39,740 --> 00:08:43,190 ‫By our I mean the rendering engines start game method. 135 00:08:43,190 --> 00:08:44,750 ‫So it will then call this method. 136 00:08:44,750 --> 00:08:50,390 ‫And of course if the game is over, then the game over method will be called this one here. 137 00:08:50,720 --> 00:08:53,660 ‫So that's how we are using the approach. 138 00:08:53,660 --> 00:08:55,580 ‫Now a little challenge for you. 139 00:08:55,580 --> 00:08:57,710 ‫Do the same thing in the player class, please. 140 00:08:58,720 --> 00:08:59,080 ‫Okay. 141 00:08:59,080 --> 00:09:00,970 ‫So first of all, I hope you passed. 142 00:09:00,970 --> 00:09:06,190 ‫And well, I'm not going to also make sure that these two methods are going to be private because no 143 00:09:06,190 --> 00:09:09,990 ‫one is supposed to call the start game and game over method from outside it. 144 00:09:10,000 --> 00:09:16,990 ‫But then we already have a constructor here and I'm just going to use this constructor in order to subscribe 145 00:09:16,990 --> 00:09:24,070 ‫the on game start and on game over methods to the on game start events and the on game over events. 146 00:09:25,480 --> 00:09:31,150 ‫So now in our game event manager. 147 00:09:31,920 --> 00:09:37,020 ‫Let's add two public static methods to trigger the events from the main method. 148 00:09:37,230 --> 00:09:40,110 ‫So here we can then. 149 00:09:41,050 --> 00:09:46,990 ‫Call of, well, basically execute this event because now at this point, well, after all of these 150 00:09:46,990 --> 00:09:52,060 ‫rendering engines, audio systems and players are created, once this event is going to be triggered, 151 00:09:52,060 --> 00:09:53,200 ‫it will execute something. 152 00:09:53,200 --> 00:09:56,890 ‫But we haven't really said what should happen if that happens. 153 00:09:56,890 --> 00:09:59,710 ‫So if that event is going to be triggered. 154 00:09:59,710 --> 00:10:04,210 ‫So let's create a static method to trigger on game start. 155 00:10:04,240 --> 00:10:12,370 ‫So this method here will first of all check if the on game start event is not empty, meaning that other 156 00:10:12,370 --> 00:10:19,690 ‫methods already subscribed to it, which basically will be the case once we created a rendering engine. 157 00:10:19,690 --> 00:10:26,830 ‫And this code here is executed, for example, or the audio system is created and this code is executed 158 00:10:26,830 --> 00:10:33,460 ‫or the player is created and this code is executed, then this statement here will be true. 159 00:10:33,460 --> 00:10:39,970 ‫So the on game start event game event will not be empty, which means will not be null. 160 00:10:39,970 --> 00:10:42,310 ‫And then this code here will be executed. 161 00:10:42,310 --> 00:10:49,270 ‫So what it's going to now do is it's going to say the game has started and then it will call the on 162 00:10:49,270 --> 00:10:50,080 ‫game start. 163 00:10:50,080 --> 00:10:53,410 ‫It will trigger all the methods subscribed to this event. 164 00:10:53,410 --> 00:10:59,980 ‫So which means that once we call this method here, so this game event, so to speak, once we call 165 00:10:59,980 --> 00:11:04,180 ‫it this method is executed here. 166 00:11:04,900 --> 00:11:10,630 ‫This method is executed as well as this method will be executed, and we're going to see that in a bit 167 00:11:10,630 --> 00:11:11,380 ‫as well. 168 00:11:12,070 --> 00:11:14,410 ‫So that is the trigger gain start. 169 00:11:14,560 --> 00:11:16,690 ‫So this is how we're going to trigger it. 170 00:11:16,690 --> 00:11:23,350 ‫And by just calling this method and you see we're making it static as we did with the game events themselves, 171 00:11:23,350 --> 00:11:28,780 ‫because we don't need to create a game event manager in order to call these methods. 172 00:11:28,930 --> 00:11:31,930 ‫So that's what we want to achieve and that's why we're making this static. 173 00:11:31,930 --> 00:11:38,230 ‫So you don't need to create an object in order to call these methods, and then let's create another 174 00:11:38,230 --> 00:11:40,630 ‫static method for the own game over. 175 00:11:40,630 --> 00:11:45,070 ‫And I would recommend that you pause the video and you try to build yourself now really quickly. 176 00:11:46,560 --> 00:11:46,780 ‫Okay. 177 00:11:46,830 --> 00:11:47,980 ‫So I hope you passed it. 178 00:11:48,000 --> 00:11:54,300 ‫It's basically the same thing as we've done here, but now it's going to do it with the end game over 179 00:11:54,300 --> 00:11:54,690 ‫here. 180 00:11:54,690 --> 00:12:02,370 ‫So I have this public static void triggered game over method that will trigger the event and if on game 181 00:12:02,370 --> 00:12:03,330 ‫over isn't null. 182 00:12:03,330 --> 00:12:11,820 ‫So if there are methods that subscribes to the event, then this will not be false and then it will 183 00:12:11,820 --> 00:12:16,830 ‫basically be true, which means this code will be executed and on game over will be executed, which 184 00:12:16,830 --> 00:12:25,110 ‫will then call all of the game over methods or all of the methods that assigned or subscribed to this 185 00:12:25,110 --> 00:12:32,850 ‫game over event, which means this method here as well as this method, and finally, of course, this 186 00:12:32,850 --> 00:12:33,390 ‫method. 187 00:12:33,630 --> 00:12:40,830 ‫So now if we are to create a new engine, now we have the rendering engine and the audio engine. 188 00:12:40,830 --> 00:12:47,910 ‫Let's say we create the third engine, then we don't need to call this stuff manually anymore, but 189 00:12:47,910 --> 00:12:54,360 ‫we have sent it up that we can set it up directly in the engine where we say we want to subscribe to 190 00:12:54,360 --> 00:12:59,190 ‫that event and the method will automatically be called once we trigger that event. 191 00:12:59,970 --> 00:13:00,690 ‫All right. 192 00:13:00,720 --> 00:13:06,630 ‫So now let's have a look at our code in the main method, because there is a lot going on here. 193 00:13:06,630 --> 00:13:12,240 ‫So now you see all of this code doesn't work anymore, of course, because all of these start game methods, 194 00:13:12,240 --> 00:13:16,020 ‫they were public before, but now they are not anymore. 195 00:13:16,020 --> 00:13:17,250 ‫So now they are. 196 00:13:18,410 --> 00:13:19,310 ‫Going to be. 197 00:13:19,310 --> 00:13:21,140 ‫Well, they're private, so they're closed. 198 00:13:21,560 --> 00:13:26,330 ‫So now let's go ahead and make sure that we actually trigger this game event. 199 00:13:26,330 --> 00:13:32,720 ‫So here we have the game event manager and we had this trigger game start because that's what we want 200 00:13:32,720 --> 00:13:33,920 ‫to do at this point, right? 201 00:13:33,920 --> 00:13:39,110 ‫So once all of the players and all your systems and everything is prepared, we can go ahead and run 202 00:13:39,110 --> 00:13:39,440 ‫it. 203 00:13:39,800 --> 00:13:44,780 ‫And now we can do the same thing with the game over method down here. 204 00:13:44,780 --> 00:13:49,220 ‫So let's trigger the game over event down here. 205 00:13:49,760 --> 00:13:59,120 ‫So what will happen now is that it will allow us to make sure that we basically have a very much simpler 206 00:13:59,120 --> 00:14:00,320 ‫and cleaner code. 207 00:14:00,320 --> 00:14:05,180 ‫So it just makes sure that our code will work flawlessly. 208 00:14:05,270 --> 00:14:08,390 ‫So now let's say we add another player to it. 209 00:14:08,390 --> 00:14:13,130 ‫We don't have to think about it and call all of these methods. 210 00:14:13,130 --> 00:14:17,330 ‫Right, called the start game method, called the game over method and so forth. 211 00:14:17,330 --> 00:14:23,060 ‫We can just go ahead and create this new player and it will automatically call the methods accordingly. 212 00:14:23,060 --> 00:14:27,890 ‫So you see here the game has started audio system and started playing audio rendering, engine drawing, 213 00:14:27,890 --> 00:14:33,530 ‫visuals and then spawning player still called toggle silver and Dragon Dock. 214 00:14:33,530 --> 00:14:35,930 ‫So now let's press a button. 215 00:14:37,630 --> 00:14:39,820 ‫And you could see the game is over. 216 00:14:39,850 --> 00:14:41,450 ‫The audio system stopped. 217 00:14:41,470 --> 00:14:45,490 ‫The rendering engine stopped and we remove all of the players. 218 00:14:45,730 --> 00:14:50,620 ‫So that is really the power of multicast delegates. 219 00:14:50,620 --> 00:14:52,810 ‫Super, super useful, very powerful. 220 00:14:53,020 --> 00:14:58,180 ‫We can see that our third object player got the start game and game over methods called automatically 221 00:14:58,180 --> 00:15:05,230 ‫because our objects were notified when both the on game start and on game over events got triggered. 222 00:15:05,500 --> 00:15:08,410 ‫Now when using delegates, some things can go wrong. 223 00:15:08,500 --> 00:15:12,490 ‫Let's first see some of the mistakes that can cost us hours to fix. 224 00:15:12,520 --> 00:15:19,350 ‫Let's say by mistake, we, instead of subscribing a method to our events using the plus equal sign, 225 00:15:19,360 --> 00:15:24,100 ‫let's go over to, for example, the player's class instead of using the plus equals sign. 226 00:15:24,100 --> 00:15:27,450 ‫We would have by accident, just used equal sign. 227 00:15:27,460 --> 00:15:30,790 ‫So the compiler doesn't say that this is wrong because this is not wrong. 228 00:15:30,790 --> 00:15:36,610 ‫You can also overwrite the existing event with just one method. 229 00:15:36,700 --> 00:15:41,680 ‫So what we're doing here is we're saying whatever else was subscribed at this point, we're going to 230 00:15:41,680 --> 00:15:42,670 ‫override it. 231 00:15:43,060 --> 00:15:46,030 ‫And that's what, well, basically happens here. 232 00:15:46,180 --> 00:15:53,770 ‫So we will remove all the methods that referenced in the on game start and on game over events and setting 233 00:15:53,770 --> 00:16:00,580 ‫the player the last player object to be precise that was created as the only subscribe to all these 234 00:16:00,580 --> 00:16:01,180 ‫events. 235 00:16:01,720 --> 00:16:06,850 ‫Also, since the on game start and on game over are actually public, we can call them directly from 236 00:16:06,850 --> 00:16:07,570 ‫anywhere. 237 00:16:07,690 --> 00:16:17,350 ‫So let's say we want to call the game event manager on game start from here, then we can do so as well. 238 00:16:17,980 --> 00:16:22,780 ‫So now let's run the code and you can see. 239 00:16:24,130 --> 00:16:27,190 ‫Sporting player with ID still called Dragon Silver Dragon Dong. 240 00:16:27,220 --> 00:16:31,300 ‫The game has started spawning play with a dragon dog and a dragon dong. 241 00:16:31,510 --> 00:16:39,190 ‫So yeah, first the event was triggered by our program Kes and then from our last player, which was 242 00:16:39,190 --> 00:16:44,110 ‫our dragon dog, it also called the On Game Start once again. 243 00:16:45,130 --> 00:16:49,030 ‫So that is basically what happened here. 244 00:16:49,150 --> 00:16:51,700 ‫So we really need to be careful here. 245 00:16:51,940 --> 00:16:55,060 ‫That is something that can be super difficult to fix. 246 00:16:55,150 --> 00:17:00,880 ‫So now we can't mark our events as private because we need them to be public to subscribe to them in 247 00:17:00,880 --> 00:17:02,310 ‫our class constructors. 248 00:17:02,320 --> 00:17:09,070 ‫So now we've been using the name event when talking about delegates because our delegates were literally 249 00:17:09,070 --> 00:17:10,570 ‫behaving like events. 250 00:17:10,570 --> 00:17:18,340 ‫But in fact there is a special type of delegates called events and that is made to prevent these kind 251 00:17:18,340 --> 00:17:19,330 ‫of mistakes. 252 00:17:19,750 --> 00:17:20,230 ‫All right. 253 00:17:20,230 --> 00:17:27,730 ‫So now there is a beautiful keyword that helps us to prevent this error, and that is the event keyword, 254 00:17:27,730 --> 00:17:33,460 ‫because basically this delegate that we created in our game event manager here, this public study game 255 00:17:33,460 --> 00:17:35,830 ‫event is an event. 256 00:17:35,830 --> 00:17:41,650 ‫So we could have just used this event keyword in here and this would prevent us from even making this 257 00:17:41,650 --> 00:17:48,190 ‫mistake because now you see that it's not happy about what we've done here so we can override a delegate. 258 00:17:48,190 --> 00:17:54,870 ‫But when it comes to events, we can only subscribe to them and we cannot just call this event directly. 259 00:17:54,880 --> 00:18:01,690 ‫It will be triggered from the game event manager directly so we can no longer call the event directly. 260 00:18:01,690 --> 00:18:07,450 ‫Since Game Start is now an event and we will be only allowed to call it from within the class where 261 00:18:07,450 --> 00:18:08,590 ‫it was created. 262 00:18:09,620 --> 00:18:13,850 ‫And also it is forcing us to treat it like a list. 263 00:18:13,850 --> 00:18:19,900 ‫So having an event on the left side of the equal sign is not allowed. 264 00:18:19,910 --> 00:18:23,030 ‫So something like this, as we've seen, is not allowed. 265 00:18:23,960 --> 00:18:29,300 ‫All right, I now, if we run the app again, you will see that it works flawlessly. 266 00:18:29,300 --> 00:18:30,980 ‫Let me run the code. 267 00:18:30,980 --> 00:18:33,350 ‫And it went perfectly well. 268 00:18:33,470 --> 00:18:43,730 ‫So this was quite a condensed example and a rather simple one because we looked into delegates, multicast 269 00:18:43,730 --> 00:18:45,740 ‫delegates and events in one video. 270 00:18:45,740 --> 00:18:52,490 ‫But I hope that this video made clear what advantages you have when it comes to working with delegates 271 00:18:52,490 --> 00:18:53,030 ‫and events. 272 00:18:53,030 --> 00:18:58,670 ‫And the cool thing is you have now seen how to create your very own delegates as well as your own events, 273 00:18:58,670 --> 00:19:04,790 ‫because we're going to see a bunch of more events later on when it comes to using the UI. 274 00:19:04,820 --> 00:19:10,250 ‫For example, when you're clicking on something, then an event is triggered and multiple things can 275 00:19:10,250 --> 00:19:10,970 ‫subscribe. 276 00:19:10,970 --> 00:19:16,910 ‫Multiple methods can subscribe to that event that was triggered and then be executed once, for example, 277 00:19:16,910 --> 00:19:21,730 ‫the button is click or any key on the keyboard is clicked. 278 00:19:21,740 --> 00:19:23,960 ‫All right, so now let's sum it up really quickly. 279 00:19:23,960 --> 00:19:31,580 ‫An event is a restricted form of a delegate forced to behave like a list, so we can only add or remove 280 00:19:31,580 --> 00:19:38,840 ‫methods from it so we can subscribe or unsubscribe from it using the plus equal or the minus equals 281 00:19:38,840 --> 00:19:40,790 ‫sign, but not the equal sign. 282 00:19:41,150 --> 00:19:46,370 ‫A delegate allows a direct assignment via my delegate equals my method. 283 00:19:46,460 --> 00:19:51,980 ‫Doing so replaces whatever other methods had been added to it before. 284 00:19:51,980 --> 00:19:53,300 ‫So be careful here. 285 00:19:53,720 --> 00:19:59,150 ‫Also, events can only be invoked by the class that defines them from outside their class. 286 00:19:59,150 --> 00:20:02,780 ‫You can only register and unregistered methods to them. 287 00:20:03,770 --> 00:20:10,880 ‫Okay, so I hope you enjoyed this video and just gave you a good idea of events and delegates and I 288 00:20:10,880 --> 00:20:13,070 ‫hope to see you in the next video.