‫All right.
‫So in this lecture, you will learn about multicast, delegates and events.
‫So let's first explore the problem.
‫Imagine we're developing a video game from scratch.
‫Our code is a combination of graphics and audio libraries and the logic for our player.
‫So let's start coding it.
‫Consider the following simple classes.
‫So first of all, we have this rendering class which at the start of the game we want to enable the
‫rendering engine and start drawing the visuals.
‫So it's going to render and then it's going to start drawing visuals.
‫And also when the game is over, we want to stop our rendering engine.
‫So we have this game over method which will then stop the rendering engine.
‫So here I'm just using console right line statements.
‫This would of course be a lot more complex code and this would then actually do something.
‫In our case, we are just trying to understand the concept of multicast delegates as well as events.
‫That's why we're just going to display it via a red line statement on the console.
‫So that's our rendering class.
‫Now let's look at our audio system class.
‫So here in the audio system, we also have this dart game method which will just start the audio system
‫and play some audio.
‫And then we have the game over method which will basically stop the audio system.
‫And then finally we have a player and the player has a player name and we have this simple constructor
‫where we just set that name based on what was passed to the object that was created.
‫And here we also have the start game as well as the game over methods.
‫So this one is going to spawn the player with an ID and this one is just going to remove the player
‫with that particular ID that we assigned as the player name.
‫So these are our three classes and I'm going to keep it rather simple with these.
‫So let's go ahead and go over to our main method.
‫So inside of our main method, we are now going to create objects of those systems.
‫So first of all, the audio system and then of the rendering engine, so very simple, just simple objects
‫here.
‫And then we also create two players with the ID.
‫So we have this player one which is CAO, and then player two, which is going to be De Silva.
‫So we need to start our rendering engine, our audio system and spawn the players by calling the start
‫game method from each of the class.
‫So let's just do that.
‫And I'm going to do it this way.
‫So I'm just going to say audio system, start game rendering engine don't start game player one and
‫player to start game each.
‫And then we just say game is running press any key to end the game.
‫So what we want to do now is we want to basically end the game once the player has pressed the button.
‫So it would of course be a lot more complex.
‫Let's say the player presses all the for or in the options menu.
‫He closes the game, then the next code would run.
‫So the next code would be of course to call the audio system game over, method rendering engine game
‫over method as well as player one and player two that game over methods.
‫So let's take care of that.
‫Let's add that in here as well.
‫And by the way, we will need a read statement because otherwise it will directly shut it down.
‫So let me quickly add a read statement here so that we can actually manually press a key to then shut
‫down everything and basically game over.
‫So let's run this real quick and see what's going to happen.
‫So we see the audio system started, audio is playing, the rendering engine started, we now see the
‫visuals and then the players are spawn.
‫So player one and player two are spawned.
‫Now the game is running and we can play around with it.
‫And then once we want to finish it, we press a key.
‫So I'm going to press a key.
‫And you see the rendering engine stopped, the audio system stopped and we removed both players.
‫So this was quite an effort to set this up.
‫Now I imagine we have to do this with a lot more things and there could a couple of things could go
‫wrong, right?
‫So let's say we add more systems, then this becomes a lot more complex so we can easily forget to call
‫the start game or game over method.
‫Or even worse, we could call one of these methods twice, which could cause all kinds of problems and
‫require us to place a lot of protective code.
‫So we would have to really make sure, is this object alive?
‫Does it have called this method or has it had have called it this method and so forth, way too complicated.
‫And this is where multicast delegates come in handy.
‫A multicast delegate is a delegate that works as a list where it can store a reference to more than
‫one method, and that's basically it.
‫Okay, this sounds rather complex, but let's build it and then it will probably make more sense.
‫So let's create a static class called Game Event Manager.
‫So.
‫All right.
‫So let's go ahead and create a new.
‫Class here.
‫And I'm going to call this one game event manager.
‫All right.
‫So this game event manager will now have a delegate.
‫So I'm going to create a new delegate called Game Event.
‫So it's not going to return anything, but it's going to be of type delegate and it's going to be public
‫so we can use it outside of this class.
‫So we've seen how delegates in general work, and we're going to use this delegate now to allow us to
‫basically add more than just one event to it.
‫So now let's create the two variables called on game start and on game end, and they will be of type
‫game event.
‫So they will be off the type of our delegate.
‫So this will be a game event delegate and this will be a game event delegate.
‫So we have these two delegate variables that we can now use.
‫So back to our three classes.
‫Let's add the start game and the game over methods to the on game start and on game over delegates.
‫Also, there is no need for these methods to be public.
‫So let's go over to the audio system and now let's make those two methods private because we don't need
‫to expose them as we are going to use the constructor of the audio system in order to add or subscribe
‫to the events.
‫So here the best place to add our methods to the game manager delegates is from within the constructor
‫itself.
‫That's what we're doing here.
‫And when an object gets created, it's methods will automatically be added to the delegates, or in
‫other words, they will subscribe to them.
‫So what is going to happen now is that our start game and our game over method are going to be added
‫to the game event manager on game start delegate.
‫And we're using the plus equals sign here in order to say, okay, we want to add this method to that
‫multicast delegate.
‫So basically we are subscribing to it and we are waiting for that event to be triggered because once
‫that event will be triggered, our method will be called.
‫So the cool thing is we can now do the same thing with the rendering engine class and the player class.
‫So what's important here is I'm using the plus equal sign and not just the equal sign, so I'm not assigning
‫it.
‫And we're going to see later what a signing would mean and what it will do for us.
‫But here we are just adding another.
‫Method to our event collectors, so to speak, or our event manager.
‫So it's collecting all of those events or methods that it should then execute.
‫So it's a multi cast delegate.
‫Now let's do the same thing with our rendering engine.
‫I would like you to do it yourself.
‫So pause the video real quick and try to assign the same code or build up the same code as we have done
‫in the audio system here.
‫So first of all, I made sure that my methods are going to be private because we don't need to expose
‫them anymore.
‫As that's not necessary, we will never call them outside of this class.
‫That's why it's always a good practice to make them private.
‫What we can call, however, is the constructor, right?
‫So we can create an object of our rendering engine as it is public.
‫So now we can go ahead and add our start game method to the on game start of our game event manager
‫as well.
‫And the same goes for the game over.
‫So here we're also using plus equals.
‫So we're saying we are subscribing to the on game start and on game over events.
‫So we're basically saying once the on game start event will be triggered.
‫Also called our start game method.
‫Please.
‫By our I mean the rendering engines start game method.
‫So it will then call this method.
‫And of course if the game is over, then the game over method will be called this one here.
‫So that's how we are using the approach.
‫Now a little challenge for you.
‫Do the same thing in the player class, please.
‫Okay.
‫So first of all, I hope you passed.
‫And well, I'm not going to also make sure that these two methods are going to be private because no
‫one is supposed to call the start game and game over method from outside it.
‫But then we already have a constructor here and I'm just going to use this constructor in order to subscribe
‫the on game start and on game over methods to the on game start events and the on game over events.
‫So now in our game event manager.
‫Let's add two public static methods to trigger the events from the main method.
‫So here we can then.
‫Call of, well, basically execute this event because now at this point, well, after all of these
‫rendering engines, audio systems and players are created, once this event is going to be triggered,
‫it will execute something.
‫But we haven't really said what should happen if that happens.
‫So if that event is going to be triggered.
‫So let's create a static method to trigger on game start.
‫So this method here will first of all check if the on game start event is not empty, meaning that other
‫methods already subscribed to it, which basically will be the case once we created a rendering engine.
‫And this code here is executed, for example, or the audio system is created and this code is executed
‫or the player is created and this code is executed, then this statement here will be true.
‫So the on game start event game event will not be empty, which means will not be null.
‫And then this code here will be executed.
‫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
‫game start.
‫It will trigger all the methods subscribed to this event.
‫So which means that once we call this method here, so this game event, so to speak, once we call
‫it this method is executed here.
‫This method is executed as well as this method will be executed, and we're going to see that in a bit
‫as well.
‫So that is the trigger gain start.
‫So this is how we're going to trigger it.
‫And by just calling this method and you see we're making it static as we did with the game events themselves,
‫because we don't need to create a game event manager in order to call these methods.
‫So that's what we want to achieve and that's why we're making this static.
‫So you don't need to create an object in order to call these methods, and then let's create another
‫static method for the own game over.
‫And I would recommend that you pause the video and you try to build yourself now really quickly.
‫Okay.
‫So I hope you passed it.
‫It's basically the same thing as we've done here, but now it's going to do it with the end game over
‫here.
‫So I have this public static void triggered game over method that will trigger the event and if on game
‫over isn't null.
‫So if there are methods that subscribes to the event, then this will not be false and then it will
‫basically be true, which means this code will be executed and on game over will be executed, which
‫will then call all of the game over methods or all of the methods that assigned or subscribed to this
‫game over event, which means this method here as well as this method, and finally, of course, this
‫method.
‫So now if we are to create a new engine, now we have the rendering engine and the audio engine.
‫Let's say we create the third engine, then we don't need to call this stuff manually anymore, but
‫we have sent it up that we can set it up directly in the engine where we say we want to subscribe to
‫that event and the method will automatically be called once we trigger that event.
‫All right.
‫So now let's have a look at our code in the main method, because there is a lot going on here.
‫So now you see all of this code doesn't work anymore, of course, because all of these start game methods,
‫they were public before, but now they are not anymore.
‫So now they are.
‫Going to be.
‫Well, they're private, so they're closed.
‫So now let's go ahead and make sure that we actually trigger this game event.
‫So here we have the game event manager and we had this trigger game start because that's what we want
‫to do at this point, right?
‫So once all of the players and all your systems and everything is prepared, we can go ahead and run
‫it.
‫And now we can do the same thing with the game over method down here.
‫So let's trigger the game over event down here.
‫So what will happen now is that it will allow us to make sure that we basically have a very much simpler
‫and cleaner code.
‫So it just makes sure that our code will work flawlessly.
‫So now let's say we add another player to it.
‫We don't have to think about it and call all of these methods.
‫Right, called the start game method, called the game over method and so forth.
‫We can just go ahead and create this new player and it will automatically call the methods accordingly.
‫So you see here the game has started audio system and started playing audio rendering, engine drawing,
‫visuals and then spawning player still called toggle silver and Dragon Dock.
‫So now let's press a button.
‫And you could see the game is over.
‫The audio system stopped.
‫The rendering engine stopped and we remove all of the players.
‫So that is really the power of multicast delegates.
‫Super, super useful, very powerful.
‫We can see that our third object player got the start game and game over methods called automatically
‫because our objects were notified when both the on game start and on game over events got triggered.
‫Now when using delegates, some things can go wrong.
‫Let's first see some of the mistakes that can cost us hours to fix.
‫Let's say by mistake, we, instead of subscribing a method to our events using the plus equal sign,
‫let's go over to, for example, the player's class instead of using the plus equals sign.
‫We would have by accident, just used equal sign.
‫So the compiler doesn't say that this is wrong because this is not wrong.
‫You can also overwrite the existing event with just one method.
‫So what we're doing here is we're saying whatever else was subscribed at this point, we're going to
‫override it.
‫And that's what, well, basically happens here.
‫So we will remove all the methods that referenced in the on game start and on game over events and setting
‫the player the last player object to be precise that was created as the only subscribe to all these
‫events.
‫Also, since the on game start and on game over are actually public, we can call them directly from
‫anywhere.
‫So let's say we want to call the game event manager on game start from here, then we can do so as well.
‫So now let's run the code and you can see.
‫Sporting player with ID still called Dragon Silver Dragon Dong.
‫The game has started spawning play with a dragon dog and a dragon dong.
‫So yeah, first the event was triggered by our program Kes and then from our last player, which was
‫our dragon dog, it also called the On Game Start once again.
‫So that is basically what happened here.
‫So we really need to be careful here.
‫That is something that can be super difficult to fix.
‫So now we can't mark our events as private because we need them to be public to subscribe to them in
‫our class constructors.
‫So now we've been using the name event when talking about delegates because our delegates were literally
‫behaving like events.
‫But in fact there is a special type of delegates called events and that is made to prevent these kind
‫of mistakes.
‫All right.
‫So now there is a beautiful keyword that helps us to prevent this error, and that is the event keyword,
‫because basically this delegate that we created in our game event manager here, this public study game
‫event is an event.
‫So we could have just used this event keyword in here and this would prevent us from even making this
‫mistake because now you see that it's not happy about what we've done here so we can override a delegate.
‫But when it comes to events, we can only subscribe to them and we cannot just call this event directly.
‫It will be triggered from the game event manager directly so we can no longer call the event directly.
‫Since Game Start is now an event and we will be only allowed to call it from within the class where
‫it was created.
‫And also it is forcing us to treat it like a list.
‫So having an event on the left side of the equal sign is not allowed.
‫So something like this, as we've seen, is not allowed.
‫All right, I now, if we run the app again, you will see that it works flawlessly.
‫Let me run the code.
‫And it went perfectly well.
‫So this was quite a condensed example and a rather simple one because we looked into delegates, multicast
‫delegates and events in one video.
‫But I hope that this video made clear what advantages you have when it comes to working with delegates
‫and events.
‫And the cool thing is you have now seen how to create your very own delegates as well as your own events,
‫because we're going to see a bunch of more events later on when it comes to using the UI.
‫For example, when you're clicking on something, then an event is triggered and multiple things can
‫subscribe.
‫Multiple methods can subscribe to that event that was triggered and then be executed once, for example,
‫the button is click or any key on the keyboard is clicked.
‫All right, so now let's sum it up really quickly.
‫An event is a restricted form of a delegate forced to behave like a list, so we can only add or remove
‫methods from it so we can subscribe or unsubscribe from it using the plus equal or the minus equals
‫sign, but not the equal sign.
‫A delegate allows a direct assignment via my delegate equals my method.
‫Doing so replaces whatever other methods had been added to it before.
‫So be careful here.
‫Also, events can only be invoked by the class that defines them from outside their class.
‫You can only register and unregistered methods to them.
‫Okay, so I hope you enjoyed this video and just gave you a good idea of events and delegates and I
‫hope to see you in the next video.