Event Dispatchers | Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this video we're going to talk about event dispatchers what they are how to use them and most importantly when and why you would use them so put simply event dispatchers are just one of the forms of blueprint Communication in Unreal Engine much like interfaces or casting but they work a little bit differently there are some huge advantages to using event dispatchers and you've probably actually used them without even realizing it which we'll talk about a little bit later in this video but without further Ado I'm going to stop talking and I'm just going to get right into an example for the first example what I've done is I've gone into the first person character and I've just added a little bit of simple code here so when we press the left Mouse button we're going to spawn in the first person projectile class that comes with this template if you want to pause and copy this all this is doing is spawning it at the location of the camera as well as the rotation of the camera a little bit in front of the camera so that there's no Collision issues now inside the first person projectile everything is as it is by default so if I hit play this is what I've got so far I've chosen to bypass the first person gun just for the sake of Simplicity now real quick I'll gloss over this all this is doing is checking if we're hitting a physics object and applying impulse so it bounces around if we do now what I want to do is I want to kind of represent sort of a hit marker system so if I hit one of these physics objects I'm just going to print an x on the screen now you might be thinking one way we could do this is to maybe store a reference to the character inside of the projectile and then call an event on the character but there are some downsides to doing that namely the projectile is then always going to need a valid reference to the first person character to work properly but say later on in your game you decide you want to make something like a turret that also shoots these first person projectiles well now that's going to be a bit clunky and probably throw some errors also if this is a first person shooter of some sort and the projectile collides with one of the cubes after the player dies and gets destroyed the player might become invalid and you could wind up with errors there and overall it's just not the best way to do things a better way would be to use event dispatchers so how do we do that well inside the first person projectile what we're going to do is go to the my blueprint panel and right here you'll see event dispatchers we're just going to add one and we're going to call it on projectile hit now if we drag this out you're gonna see we get a list of a bunch of different options but the only one we need to worry about right now is call and this just calls that event dispatcher much like a function or an event and in this case we're going to call it if we are in fact hitting a physics object so right before the destroy actor I'm going to plug this in and if you hit play of course nothing is going to happen because we haven't actually bound to this event dispatcher yet so what does that mean well if we go back into the first person character when we spawn this projectile in we're actually getting an object reference to the projectile we spawned out right here so what we can do is we can drag off of this and we can do bind to on projectile hit and we get this node here and we get this weird looking delegate pin here and you may have noticed that when you create custom events so let's do that now and let's just call this projectile hit these also have one of those pins but on the right side and this is the output delegate and that's what you use to bind an event to an event dispatcher so all I have to do is plug this in right here and what this means is that anytime this gets called from the projectile this event is going to run so what we'll do is we'll print string and I'm just going to print out an X again not a full-fledged uh hit marker system you would use uis and sounds and all that but that's outside of the scope of this video we're just keeping it simple so now if I hit play and I shoot something that's not one of those cubes nothing happens but if I hit the cube you'll see that it prints X now again this is super useful because this is a method of communication that is what's called Loosely coupled or decoupled meaning this projectile it has no stored references or dependencies on the player or anything else that's using it really so again we could go and we could make a turret that uses this we could use it with different guns it doesn't really matter it doesn't have any dependencies on the things that are using it and I tend to find that when you're spawning things in and you need to do something based off of that thing you're spawning in that is one of the times that event dispatchers come in handy now there's a little bit more to using these so let's look at that now if we come back into our uh projectile let's just delete this for a minute and let's click on it over here you see we get some options in the details panel and namely at the top we have inputs so instead of determining whether or not we're hitting a valid Target inside of the projectile itself we can actually pass the hit result through and then do that in the first person character So to avoid any errors I'm going to delete this out of the first person character as well I'm going to compile this and actually you can't compile it without this being bound to something so I'm just going to go ahead and delete that and I'm going to click the new parameter button in the details panel here and you see it's a parameter just like a function or an event in this case we're going to make it a hit result and I'm just going to call it projectile hit I'm going to compile and now when I call this projectile event dispatcher you see that we have a projectile hit pin here we can just reroute this and plug it in like so and then back in the first person character we can bind to event on projectile hit now here's the thing if I make a custom event and I call it projectile hit or whatever and I try to plug this in here this is actually not going to work because the event signature has to match meaning it has to have the same outputs so we could go in and we could add that here manually but another thing you can do is just drag off of this event delegate go add event add custom event and you'll see that we get an event that matches and is now Bound To That on projectile hit and again we can call this projectile hit we could then break this and check if uh is simulating what is the name of it ah you have to cut you have to do it from the component my apologies is simulating physics and if that's true it will print string we'll print the X and just to demonstrate this we'll get rid of the check inside of here and that in that case we won't have any impulse and the cube won't move or anything but just for demonstration purposes I'm just unplugging these and you'll see it works and if I shoot the wall nothing happens now before I move on to my next example and use case of uh event dispatchers I just want to show you that there's even more options when you're using these uh bind events so this is kind of messy having this right here like this what we could actually do is just delete this and off of this event delegate if we go to event dispatchers you'll see this create event node and from here we get this drop down now if we have any functions or events which by the way yes you can also bind functions using this node but if we have any functions or events that match the signature of this event dispatcher we can bind to them or we can just again create matching event like so and now we have an event that's bound but it's not directly plugged into that uh by node so it's a little bit cleaner so on projectile hit and I can move all this code down here and it's going to be the exact same thing all right so this is just one of the many uses where event dispatchers might come in handy another place where you'll find that they come in handy is when you have a many to one relationship so what that means is you have a bunch of different actors that might rely on one event being fired so in this case over here I have three lamps and I have a pressure plate and when I step on the pressure plate I want all three lamps to turn on and when I step off the pressure plate I want them to turn off now again you might be thinking that inside of this pressure plate we could simply store an array to a bunch of lamps and maybe cast or use a blueprint interface but again there are some problems with that just like with the projectile it makes this just a little bit less modular it has a dependency on either a direct reference to the lamps or an interface but also one of the problems with this is in this case you probably wouldn't wind up with this issue but let's say we want to add a whole bunch of other lamps well now we got to go here we got to add to our array add all those lamps in and it's just again it's not modular a better example of something like this and why it would be useful is say you had a day night cycle which by the way I'm making a tutorial on how to make a day night cycle and if it's out there will be a card but let's say you have a day night cycle and when it hits Knight a bunch of different things in your world might have to happen maybe some of the NPCs go to bed maybe a bunch of lights turn on who knows at the time of making your day night cycle itself you're not really going to know right off the bat what things need to do stuff based on whether or not it's night and even if you do it would be such a pain to have to go through add a specialized interface to everything or add everything to an array it would be so much better to just make your day night cycle and then not have to worry about all the dependencies and instead worry about them on a case-by-case basis so we're simplifying that a little bit and just using a button here instead of making a whole day night cycle but it's the same principle so how do we achieve that sort of modularity and decoupled approach in this case well instead of the platform storing a reference to the lights will have a light store a reference to the platform so let's go into this BP lamp that I created and all it is is a point light and the uh lamp static mesh that comes with the starter content and by default I have the point light set to zero so that it's off now what we can do is we could just create a variable called trigger and we can store a reference to BP underscore button which is the name of the button blueprint that I created the pressure plate as I called it and we'll just expose this to the details panel now inside of our button we will create an event dispatcher and we will call it on button toggled and we can delete these and in the button I just have a static mesh and a box Collision and for the Box Collision I'm just going to right click add event and add a component on begin overlap and then what I'm going to do might seem a little confusing I'm going to cast to the first person player and this is just simply to make sure that it's the player that's stepping on the box and not some random actor from the world like the projectile for example I'm not getting any data from here and there might be better ways to do this but for the sake of Simplicity that's what I'm going to do as a check and then we're going to call this on button toggled but actually before I call this I want to pass through a Boolean variable called is active and when we begin overlap we're going to call this and we're going to set that to true and we'll also add an event for end overlap again we'll cast to the first person character we'll call this event dispatcher and we will set this to false all right great so that's all we have to do for our button blueprint we don't have to touch anything else in here let's go to the lamp and on event begin play we will take our trigger reference and it's always good practice to make sure it's valid reference and then again we will just call bind to event on button toggled all right and finally I'm going to drag off of here event dispatchers create event and I'm going to create a matching event and we'll call this on button toggle and right here we have this is active we can do a branch take our Point light and we will set intensity so if it is active we'll set the intensity to I don't know a thousand and if it's not active we will set it to zero so now all we have to do is set our reference that we exposed for each of these buttons and if I hit play and I step on the platform they turn on and they turn off and again this is great because we can use this platform for anything else too it doesn't just have to work with the lamps it doesn't rely on any interfaces it doesn't rely on casting it doesn't know anything about the lamps it's the lamps that are listening for that event to run in this button and I could go and add more of these and because I already set that reference making copies like this it's still there you could add a whole bunch of them and I don't have to change anything inside of the button just walk over and they're on and now they're off so so far we've covered two examples that highlight two scenarios where you might want to use event dispatchers one being when you spawn an object in another being when you have a many-to-one relationship now there's another time where I find myself using event dispatchers and this is where you've probably used them without even realizing it so real quick I'm just going to create a blank actor and I'll just call it BP example and we've actually already used it in this tutorial funny enough but if you go and you add a collision component and you go to the event graph like you saw I can add an on component begin overlap for this I can also just click on this scroll over on the details panel and you'll see that we have this plus button and this is actually an event that's bound to a dispatcher now that dispatcher and the code for this box Collision is written in C plus but it's the exact same thing and that's why that exists over here when you have components the whole point of components are to be modular scripts that you can add onto your different actors so if this box collision had a direct reference to say the character or a button well then it wouldn't really be as useful as it is we can use this box Collision on anything that needs to have an overlap event and that's why aventus batchers are pretty much Essential for components and I'll demonstrate this right now by creating a custom component it won't be a collision component instead we'll just make a subclass of static mesh component so if I go in here let's see it is right here static mesh component I'm just going to call this my custom mesh and components really overall not just actor components but static mesh components all the different components in unreal deserve a video of their own because they're super powerful when you make your own subclasses of them but I'm getting off topic in here what I'll do is I'll create an event dispatcher and I'll call this something that we know for sure we made so on custom mesh begin play and I'll just call this off of begin play and compile and if I go into my example blueprint I can add my custom mesh like so and you're going to see over here in the details panel under events we now have a plus button for that event dispatcher just like we had for the on component begin overlap for the Box and if I add that and I type print string our com components event dispatcher is working and I didn't know how to mesh or anything like that but I guess we can add a cube and if I go ahead now and add this example blueprint into the world as soon as I hit play we should see our components event dispatcher is working so a few other quick tips with event dispatchers as you saw when we drag these out we get a whole bunch of different options and you can mess around and play with these if we click on for example assign you'll see that it gives us an event dispatcher with an assigned event which is cool which by the way yes you can use event dispatchers inside of the same actor where they're declared there's nothing wrong with that if you find a use for doing so and as we saw you can bind to event dispatchers you can also unbind from an event dispatcher and the way that you would do that so let's look at the projectile here we could just drag off of this return value and type unbind from on projectile hit you have unbind all events in case you have multiple events bound to it or you can just do unbind event and we can create event and we can unbind that on projectile Hood so when you're unbinding make sure you're selecting an event that's actually already Bound in this case if we ran this this would never work because it's getting bound and then Unbound again and yet that's really it I hope you guys found this tutorial useful if you did please consider slapping a like on it if you have any questions comments or concerns leave them in the comments down below and I will see you in the next video best of luck on all your projects
Info
Channel: Tyler Serino
Views: 2,676
Rating: undefined out of 5
Keywords: Event Dispatchers, event dispatchers ue4, event dispatchers ue5, event dispatchers unreal 5, event dispatchers unreal, event dispatchers unreal engine, event dispatchers catching up, how to uise event dispatcher ue4, how to use event dispatcher ue4, blueprints, tutorial, unreal engine tutorial, game dev, blueprint communication, programming, unreal engine, unreal engine 4, unreal, game development, blueprint
Id: r20VEPH_e0o
Channel Id: undefined
Length: 19min 9sec (1149 seconds)
Published: Tue Jul 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.