Unreal Engine - Event Dispatchers Explained!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome to another one of my tutorials so in this video we're going to be covering event dispatchers and explaining what they are and more importantly why and when you should use them so i've set up this little example project here and basically the way it works is when i press f he spawns this little bomb thing and then once it stops moving it will turn red and then after one or two seconds when it's red it will explode and then you can see in the bottom left it says bomb available which means i can spawn as many of these as i want and so we're going to be looking at this project and adding a little bit of stuff to it just to kind of show you guys when and why you'd be using event dispatchers so if you don't know what event dispatchers are i will show you real quickly so if you come over here to any blueprint really any actor there is a section at the bottom underneath your variables called event dispatchers and so this is what i'm talking about so if you've never even seen this before i'd still recommend watching the video because this is a very powerful tool to use and a lot of people don't know about them and never use them but if you do know what they are then you can use them and it makes the design of your program a lot nicer in certain cases so we'll come back to this later but i wanted to give you guys a quick like high level overview just an explanation of what they are and why you should use them because i've seen a lot of tutorials that actually go in and like show you how to come up they don't really explain why you should use these so i think a good analogy for event dispatchers it would be a microwave so if you think about a microwave if you want to go and heat up some food you go and you put your food inside the microwave and then at that point you have two options right so you press start and then you have two options you can either stand there and watch the microwave and watch the timer go down to zero and then know that your foods are ready or you can go and back to the living room or back to your room and play video games or watch tv and just wait for the microwave to alert you that the food's ready when it starts to beep so those are your two different options so an event dispatcher is the second option where you put the food in the microwave and then you leave and you just say hey microwave please notify me when the food's ready as opposed to just standing there and every frame or every second you're just looking at the microwave and saying hey is it done is it done is it done is it done is it done um so those are the two different two differences or when you might want to use an event dispatcher so if you can kind of keep that in mind and if you ever run into a situation in code where you really just want something to notify you when it's ready you don't want to have to stand there and check every frame to see if it's done then that's a good use for an event dispatcher so that being said let's kind of look at this example so what i want to do is let me run this again what i want to do is right now i can just spam these bombs so i want to make it so that i can only spawn a bomb when the previous bomb has destroyed so that way there can only be one bomb at a time so let's say i wanted to do something like that well that would be a perfect use for an event dispatcher because we basically are telling the system just like the microwave please let me know when this bomb gets destroyed i don't want to have to check every single frame to see if it's destroyed i just want to know when it gets destroyed and at that point i'll know i can spawn more bombs again so if you look at our code here all we're doing inside the player is we're saying when the f key is pressed spawn our bp bomb actor and it just spawns it kind of in front of the player and moving forward so one of the things about event dispatchers that you might not know is that you probably are already using them without even realizing it and what i mean by that is a lot of these events over here that you can override are actually event dispatchers that you're binding to so for example we want to know when the bomb gets destroyed because when the bomb gets destroyed we want to be allowed to throw it again so the first thing we're going to do is we have this bomb available over here it's not actually being used anywhere yet i just created it because i knew we would need it so what we're going to do is we're going to drag this guy over here and we're going to set him to false right after we fire the bomb so now you can see when we run this and we press f you can see at the bottom left it says bombs are now unavailable because that little ui is bound to that boolean and if i try to press f again it still works and that's because we need to actually check this so if we come over here before the f and we add a little branch so let's drag this guy in and we'll add a branch we'll say only allow us to spawn a bomb if there's a bomb available so now if we run this again we should be able to press f once and then if we try to press f again it doesn't do anything so i'm spamming f and nothing's happening so that's good but we want to make it so that when the bomb explodes this bullying gets set back to true so that we can fire bombs again so what we could do is we could check it so like inside the event tick we check if the bombs still exist and we do that every single frame and then when the bomb gets destroyed we'll know that no longer exists and then we can set it back to true but we don't want to do that right we don't want to stand in front of the microwave and check every frame when our if our food's done we could just go and wait for it to start beeping at us so that's what we're going to do so what we can do here and this is what i was saying some event dispatchers you probably already use and don't even realize it so you can drag off of this so this is the bomb that we've just spawned and you can say bind to and when you type bind to all of these things that pop up all these different events that you can bind to are actually event dispatchers that just come with unreal so actors already have a lot of event dispatchers hooked up for them so you can see like when it takes damage when it takes point damage radio damage when it's destroyed when it overlaps something so all of these are actually event dispatchers that are being dispatched from inside the engine that you can bind to so that's what i'm saying you probably already used event dispatchers but i'm realizing it um so we actually don't even need to create one for this case we will be creating one later in the tutorial but for right now i just want to show you that you can make use of event dispatchers without even having to create your own so like i said we want to know when it gets destroyed we're going to say bind event to undestroyed and then we'll just hook this up here like so and then what happens here is it creates this little event for you but if you try to compile it it gives you an error because you need to actually provide one of these events to it so to do that you can drag off this guy and you have two different options here you can either say add custom event and when you do that it will create a custom event like normal but it will connect this little pen for you and then you can call this whatever you want so we're going to call this on bomb destroyed and i'll show you guys the second option here in a second i just want to hook this one up first and show you how it looks so the idea is that when the bomb gets destroyed it's going to call this on bomb destroy event and so inside of here what we want to do is we want to set bomb available back to true so i'm going to copy paste this guy and set it to true so now if we go ahead and run this we should see that when the bomb blows up our bomb available becomes green and available again so there we go and now if i try to press f i can spawn another bomb and if i try to press f before it destroys it won't let me so that's a very simple basic use case of using an event dispatcher and then the other option i'll show you guys real quick because some people don't know this so if you don't want to use a custom event here and the reason you might not want to do that is a for organization's sake but b if you're just using a custom event this is actually a public event so anybody who has a reference to the third person character would be able to call this event because it's public and it doesn't really make sense for anybody else to call this event so ideally you'd want it to be private so if you want an event dispatcher or if you want an event that's bound to admit dispatcher to be private you can still do that you can't do it with an event so we have to delete this guy and i'm going to delete this guy as well you have to do it with a function in order to do that you can say create event and it gives you this little green node which is kind of kind of confusing looking at first but it's actually pretty simple so in this drop down here you can select the function that you want to bind this to and then instead of calling an event the the red event it will call this function and you'll notice there's actually options here for creating a matching function or even creating a matching event so since we don't have a function yet i'm just going to say create matching function and you'll notice as soon as i click this it generates this auto function for us it doesn't know what to call it so it just calls it third person character auto gen function but if you look over here on the left we can actually rename it to whatever we want so we can call it on bomb destroyed oh we might have to compile and do this again because it was probably already saved from the last one on um destroyed there we go and then the cool thing is is we can come over here now and we can set this to private so again since this is now private uh nobody else can call this function even if they have a reference to the character which makes it a little bit safer because we obviously don't want anybody else calling this function except for when the bomb gets destroyed and then inside of here we can do whatever we want so we can take in our bombs or bombs available and we can set it back to true and so we compile save and then just to show you if we come back here and we look you can see we're now binding this event to this function on bomb destroyed so now if we play this it should do the same thing as it did before but now we have it inside of a private function which is a little bit better in my opinion so if we press f and it spawns it we can't press f again until it explodes oh that time it didn't actually work uh what did we do you know i've noticed sometimes in unreal engine 5 you have to actually select none here and then select it back to on bomb destroyed i think this is a bug with unreal engine 5 just because it's in preview but sometimes it doesn't call it the first time and you really you really only have to do that once so now if we press play should work so bomb unavailable and then it destroys and then bomb available so now it's working as we expected so the last part of this tutorial is i want to show you how to create your own custom event dispatcher [Music] and how to bind to it and call it so let's say for example going back to our bomb here let's say that instead of waiting until the bomb explodes what if we want to make it so that you can spawn a new bomb as soon as the bomb turns red so right there if we wanted to do that we could use this same setup inside of here where we bind to it but we don't have an event that we can bind to that's called you know on bomb turned red right so that's what we really want so if you want something like that you can always make event dispatchers for yourself so if we go into the bp bomb i know we haven't looked at this guy yet but he's actually really simple um it's all it's doing is just setting the color or is it all it's doing is binding to this on projectile stopped event so if you look up here we have our projectile movement component and what we've done here is we've actually bound to this on projectile stop and this is actually another event dispatcher believe it or not because if you look if we drag in our projectile movement and we say bind event to on projectile stop you can see it's right here so this event that i'm using is actually an it's actually an event dispatcher as well so that's what i'm saying you probably use these without even realizing it but basically the idea is that this event gets called whenever the bomb comes to a stop and at that point we are setting it to red and then we are setting a timer from anywhere from one to two seconds and then after those one to two second throw up we destroy the actor so instead of the or yeah instead of just waiting for the bomb to destroy um we want to call an event dispatcher whenever it turns red so we can create our own event dispatcher by clicking this little plus button and we will call it um on we'll call it unarmed meaning this is when the bomb becomes armed and i'm just calling it on because when it turns red it kind of looks like it's armed right so unarmed and then we press enter here and it saves it and then you'll notice it kind of looks like a normal variable or a function over here and we can actually drag it in just like a normal variable or a function except we get a whole bunch of options here so i'll just explain what these are real quick so calling it is just like you would a normal function so if you actually click this you can see it calls it and it does it just like a normal function would like destroy actor or set timer by event it just calls the calls the event like it triggers the event so this is um this is the same as the microwave starting to beep right it's calling the alert function letting everyone know how that pay the food's done the other options here are bind so this is kind of like kind of the opposite of calling it this is binding to the event and saying hey please let me know when this event gets called so in the microwave example the person who puts the food in the microwave would bind to the you know food ready event or the beeping event and so that the person who put the food in would be notified when the food is ready and the cool thing about event dispatchers is you know an infinite amount of people can bind to these events so if you had you know 10 people over and you were making nachos in the microwave all 10 people could buy into this event and get notified when the nachos are ready and it doesn't matter where they are in the house because they're all going to hear the beeping sound so that's another cool thing about event dispatchers is you know as many people buy to them as you want and then unbind and bind all or unbind all are pretty self-explanatory so if you no longer care about the nachos and you don't want to hear the beep then you can unbind from the event and unbind all just unbinds everybody and then this one actually implements the event so you can see over here this is an example of implementing the on projectile stop event i usually don't really use this one too much i don't know why i'm usually either binding or unbinding to it but this actually implements the event and you can only do this inside of the blueprint that actually defines the event dispatcher and then the final one is what was the final one oh a sign so i'm just going to click this so you can see the difference so if you click assign it actually calls bind so if we look at bind here so this is bind on the right and this is a sign on the left so it actually does the exact same thing but when you click assign it just adds the event for you just as a little convenience thing but they're really the same the same thing so let me delete these so again what we want to do going back to our bomb thing here is we want to make it so that when the bomb is armed or turned red we dispatch this event so what we want to do here is right before we destroy it we just want to say or sorry we want to do it up here so as soon as the projectile stops moving we set it to red and then we want to call this armed event so we'll say unarmed and then we'll say call and then we'll hook this up right here so this is what it now looks like so when the projectile stops moving we set the color to red and then we call on armed and then you don't have to worry about this it's just setting a timer for two seconds or anywhere between one and two seconds and after that amount of time it just goes ahead and destroys the actor so if we go back compile and save we go back to our third person character let's now change this binding here to bind to our unarmed event so i'm going to delete these two guys drag off our bomb and say fine to on armed just like so and so now it looks very similar to how it did before except we're binding to unarmed and again we can say create event and for the drop down here we can select create a matching function for us so the reason we can't use onbom destroyed again well the main reason here is that it has a input parameter to it and our event dispatcher doesn't so it's not going to allow us to bind to this function so that's why we need to say create a matching function and this one we'll call it onbom arm and then inside of here we don't want to set bom available to true and there we go so we don't need this on bomb destroy anymore so i'm just going to delete it since we're not using it and then i'm just going to set this guy private pile save and then let's just look at this again real quick before we run it so all we're doing real simple is responding the bomb we're binding to the on armed event saying hey please let us know when the bomb gets armed and when the bomb does get armed it's going to call on bomb armed and then inside of here we're going to set bomb available back to true so now if you watch it as soon as the bomb turns red the bottom left should turn from unavailable to available so there it goes and then as soon as it turns red i can fire a new bomb off all right so hopefully that makes sense um there's one more thing i want to just point out i'm not going to like go into detail on it because it's really self-explanatory but if you click on unarmed or any any event dispatcher that you make you can add inputs to it so i'll just show you as an example i don't have like a logical thing to show you but you can just hit a plus button just like any other function and it will add an input parameter and you can change the type just like a normal function and you can give it a name just like any function so we could call this you know like my number but this can be whatever you want for unarmed like it doesn't make sense like we don't need to provide anything because we're just saying hey it got armed but you know you could always provide any sort of information that you want like for example the destroy actor one it provides you the actor that was destroyed and so you can pass along information but the important thing to know here about event dispatchers is it only has inputs it doesn't actually have outputs and the reason for that is because when you're calling an event dispatcher like i said before the event dispatcher can be bound to many different people so it doesn't make sense for an event dispatcher to have a return value since it's going out to potentially you know hundreds of different people if it had a return value you'd have to somehow get the return values from all those different people or maybe choose a choose the one you prefer or something like it when it doesn't really make sense right because it's just it's basically just a microwave beeping saying hey like the food's ready nobody's um returning any information to us so i just wanted to point that out so if you do add a if you do add a input here you can see if you just refresh the call you can see my number now shows up and you could pass in whatever you want here so like we'll just pass in eight and then if you go back to the third person character you'll notice if we try to compile this it's going to give us an error and it's giving us an error because the function that we have bound to it on bomb arm if you look at it it doesn't take in an integer so it's going to say hey you can't bind this event dispatcher to this function because the function signature doesn't match so if we want it to match we just need to add an integer here and it doesn't actually matter what you call it since it's an integer as long as the type matches it's okay so maybe you call it my number now if we compile this you can see it's now fine because our signature matches and if you wanted to we can just print this out just you can see that it passes along this eight all the way through the chain i'm just going to print this guy out and then we'll run it and you'll notice if you look at the top left when the bomb turns red it prints out eight so you can always pass information from the person calling the event to the people binding the event but you can't return information back to the caller so that's one difference between event dispatcher functions and normal functions i just wanted to point that out but yeah so that concludes this tutorial slash informational video if you liked the video please leave a like and subscribe i also have a discord if you guys want to join um it's in the link of the description of this video if you guys want to join uh ask questions or just meet new game dev people feel free and there's also information if you want to donate that's always appreciated as well so thanks you guys for watching let me know what you think of this type of video this is kind of a new thing for me usually i do tutorials but this is more of an informative thing so let me know what you think see you guys in the next video
Info
Channel: Reids Channel
Views: 14,184
Rating: undefined out of 5
Keywords: Unreal Engine, Unreal Engine 4, Unreal, Unreal Engine 5, UE4, UE5, Tutorial, How To, Blueprints, Reids Channel, Reid's Channel, Event Dispatchers, Delegates, Events, Broadcast, Multicast, Binding, Bind, Assign, Unbind, Unbind all, Notify, Notifies
Id: zsdtCajVd4g
Channel Id: undefined
Length: 21min 17sec (1277 seconds)
Published: Sat Sep 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.