Blueprint Communication in UE5 is easy - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you heard before about the term blueprint communication it's the way blueprints can interact between each other and is the way we make our game or project feel alive in simple terms our user do something and something else respond for example if i were to turn on the lights first i would need to push the light switch and the light switch needs to tell the lights to turn on the goal of this tutorial is to give you an introduction on the subject what types of communication exist and how to apply them through simple examples when you finish this tutorial you will know how to use direct communications interfaces and even dispatchers and also how to choose which type of communication to use and before i forget if you want to support us in any way be sure to check out our store we have a lot of cool stuff like a free blueprints course another to model a castle inside unreal and our marketplace assets that were made with care and effort now let's continue let's talk about blueprint communications what we will learn is types of communication between blueprints we have these three types direct communications and casting interfaces event dispatchers and also we will learn how to choose between them it's very important to know how our blueprints and our code or objects will communicate between each other so yeah this quote is important who am i my name is ramanan purisala i'm a game developer educator currently an author instructor partner for epic games and now that you know me let's get to them to the point what is blueprint communication is that way individual objects can interact with each other here we have two objects we have a sender we have a receiver one is the the one that sends any type of signal in this case we're turning a switch on or off and the receiver is the one that gets the signal and then do something with it why is it useful it allows us to send specific orders to an object like turn on or turn off let's just get relevant relevant information from another objects we could get the health of a character we could get if the object is on or off and we can use that information to drive other type of logic also give us the ability to notify the execution of any event to other objects and this will give us a great advantage to avoid using the tick in order to wait for events and it will became it will become really easy to manage all that stuff so it gives us this ability what do i need for blueprints to communicate well we need a sender and a receiver it could be one receiver at least but it could be also multiple receivers but you will definitely need a sender and a receiver the other thing we need is we need a reference we need a reference to an object for the communications to be successful we need a reference because we need someone to know where am i sending a message or where am i getting the the signal from right these are the main types of communications inside unreal engine and even though the topic is blueprint communication this will also work with c classes so we have the first one direct communication that we will use casting most of the times to get the information we need they are really similar it's a small difference we will see what the difference we also have interfaces as a way we can set send messages that can be implemented in different ways and we also get event dispatchers that are ways that any event i execute can be notifying some other objects that have decided to listen to that a specific event right so we will talk about this more in depth in the next videos and if you just want the tips on how to use any of them or when to use them what questions are useful to ask yourself before using them you can go to the last video of this topic there they i will have a little a little chart that you can also find in that documentation of unreal engine that is really really useful so you can skip to that if you just want a reminder and you already know how to use them but if if this is the first time you're listening to this then we'll have a lot of examples where we will show every single one of these types of communication and practical examples that you can follow so it is really clear and we have that for the theory and the practic so let's talk about direct communication and casting so this type of blueprint communication is a conversation between two blueprints right like any other one the difference is that this is a direct communication what does this mean that the sender gains access to the receiver and can get information update information run functionality inside and you get that specifics of any class you are communicating to it's a one-to-one communication and even if you would do something like communicating to an array of items then it it still comes as one-to-one communication because you're not communicating to every object at the same time so it is not a one-to-many communication it's a one-to-one one-to-one just take that in consideration now like it says here it's likely that you will use casting to access all the information and functionality inside the object and what is casting casting is a programming way of checking the type of blueprint that we are using in our case if we wanted to communicate to a specifically i don't know to a pickup class then we need to cast the actor class that remember that every every class that we can put inside the map is an actor actors has have that ability to place them have a location rotation scale so if we wanted something more specific about the class we would need to cast it so that's what casting means and also it is important to keep in mind the hierarchy of the objects because casting can work to the base object and even though you are using a subclass of the parent class then the casting will still work because we are in in the case that we are casting to for example if we have a pickup class and we have two child blueprints like we created in the in the blueprint quick start course there we had a base class a base blueprint class that represented a pickup and also two child glass one was a coin pickup and the other one was a health pickup if we cast to a pickup class the base class then using that that that same cost for our coin or our health will work but if we cast it to our health pickup class then if we use that same cost for our coin one it won't work and this will be more clear when when we show it to the in the engine and let's let's do a little demo and i have open unreal engine 5 and i'm using the project files of the blueprint quick start course that i'm not really sure if it's been called the same but the project is called blueprints good quick start and here we have our pickup blueprints the base pickup and the two child pickups and what we're going to do is add a character that will be able to pick up these coins or this this other pickup right and my base pickup doesn't have any representation let's use the coin pickup so we can add some coins to our character now we need a character here let's add it by add feature content pack and choose my third person there it is now i don't remember if we had a map but let me create a map here and this project is called master blueprints so let's create a folder called master blueprints and here because we're gonna do a lot of examples let me create a subfolder called direct communication so now we have my a pickup here a coin pickup and we need to to use this uh where is it blueprints third person character right let's save this map file and i'm gonna create a folder inside my direct communications let's put it maps and here i will save this map when using a real five i have i have got it in the avid of saving more frequently than than usual because you don't really know whenever you can run to an unexpected error so let's call it like that right so let's use our third person character if i'm not wrong here there is a game mode that we can use in this map so let's go to settings world settings and here let's choose that game mode third person game mode that way let me put my pickup here if i press play i already have my my character and it should work right it doesn't give me any coin or or anything of the sorts what i'm gonna do is create that communication between these two blue blueprints yeah so in our case we want to communicate this third person character with this coin and i'm gonna copy and paste this third person character i'm gonna i don't want to edit the the the one inside the example map well then the example content so here i'm going to create folder blueprints and just drag and drop it copy here and also i'm gonna copy this this game mode and now i'm gonna use this game mode here i need to rename it just in case so let's let's call it game mode direct communication [Music] and this one let's call it direct con third person character for now blueprint yeah yeah so we have a lot of variations here so we can show the difference between every type of implementation now i need to choose my default pawn let's choose my direct com and it should work exactly great so now what we need to do is do some communication between these two if we remember that first video we said that we needed a sender and a receiver so if we open my pickup we will know that here we already have um why is my text so small let me do something like this there it is we already have a function i believe called on pickup picked up right so this blueprint already has like a event that we can use as a signal right so let's let's use it this is in the case of the coin the coin needs to tell the character that it has been picked up right so here we can do something like this overwrite picked up it's fine if we call this this parent code let's double click and see what's inside it yeah it's just hiding it and we play the pickup sound that's that's fine but here we want the coin to tell the character that it has gained uh a coin right so we need to create that function first in our character we can call it gain coin and we need to tell it how many coins we want to gain and let's call it coins to gain nice we could create here a variable called total coins and now we can add it to the coins to gain pretty simple stuff right and let's print a message in in the viewport just to know that this really happened currentcoins and i'm gonna connect it to the total coins great now we have something that we want to call whenever we pick up a coin so here in our coin pickup we're gonna do a direct communication with casting why why is it with casting well because we need to cast before before sending the the message right because we don't really know w with which other blueprint we're talking here in my pickup i really need to know which other actor has picked up this this pickup so let's let me find it pick pick up here this is the other actor that i need here in my function called pickup let me create a input called actor that picked this up and this is going to be an actor reference actor object reference save compile it and let's give it the same reference here of the actor that has overlapped this pickup now in our coin pickup this will be updated we need to connect this to even though our parent function doesn't it doesn't make use of this input it is good to have everything connected and now with this information we can proceed to do the communication now if here we have an actor object reference and we wanted to call this function called gain coins we won't be able to find it we would need to re remove content context sensitive and this will give me game coins but it asks for a blueprint direct communication third person character if we use it we can't connect it because it expects a direct communication third person character so what we need to do is a cast so let's cast it cast two and let me check again context sensitive cast 2 blueprint direct communication third person character and here is the part that that is a direct communication because we have a reference and we're gonna cast it to get exactly what we want in this case our gain coins now we can connect it like this or we can type gain coins and it should be fine so let's put it like this gain coins coins to gain this coin pick up in the free course we we create a number of coins to give so that's what we are gonna set it up and let's save it and now if i press play and let me choose new editor window pi if i pick it up current coins to pick it up again so the action is working and i believe i made a little mistake here total coins gained coins plus one yeah this was not what what i wanted to do it was this type of the gig coins to gain plus total coins will be the total coins so now it should be working correctly current coins one so yeah now it work works correctly and we have a direct communication between these two blueprints now how could we use a direct communication without casting well the only way you can do that is through the level blueprint that we also talked about in the free course we can access a level blueprint here blueprints open level blueprint and we can select any actor inside our level for example our blueprint or for example uh if i had my character here we can select the character and inside my level blueprint let me open it again i can right click and get a direct reference to that character so here we are not using cast but we can get access to the game coins so this is a direct communication but this is only possible because we are using the label blueprint that already knows what's what's inside it in this case this third person character where we have stored our direct communication so if we needed to create an event here that also gave coins then we can use it this way also now we're not finished with the direct communication when should we use this direct communication when we know who is the sender and who is the receiver when we have a lot of clarity about which blueprints are gonna communicate between each other then it should be fine to use it when we know which data or functionality we want to access for example in our case we wanted to gain access to that function called game coins then we should also use direct communication also with a matter of simplicity when we know exactly what should happen when two objects interact then it should be fine to use direct communication and at the start of any project this should it's not always too clear what should happen when two object interacts and in the interaction of other objects maybe something different should happen maybe especially when you're dealing with the concept of elements like fire eyes water like what could happen if you have all this mapped then it's fine if you use a direct communication but if you're not really sure and or you don't want to waste too much too much time then you should probably avoid it because it limits a lot of interactions if you are casting a lot of stuff you should also avoid this type of communication if you need multiple validations to get the functionality where you're to get the functionality you're looking for like nested if conditions a good example would be if let's go to our to our unreal let's save this let's say we wanted to create an interaction also like where is it here we wanted to create a gain health or no the other way around bitcoin pickup wants to communicate to another character right for example my third person blueprint here then this won't work because a third person character is not a blueprint direct communication third person character what we would need to do is here cast to the third person character and then if this cast fails then we have something like this and here we can call some some action inside my third person character if we have another type of character then we would need to cast and let's do something like let's imagine this is another character then we have something like this like if this fails then another character should be cast and this doesn't give us a lot of options if we wanted to change something then we would need to come here to this function and in this function search for the character we want to modify maybe another function we we need to call here maybe it's not gained coins maybe we want to destroy it or something like that then we need to do this and search carefully right and if we wanted to add another character then we would need to cast add another cast and it could get really really complicated really fast so if you have a lot of conditions here a lot of casts then you should avoid this type of communication and probably use a interface type of communication that we will see next and i believe this is yeah yeah let's go to the interface communication so communication through interfaces these interfaces can define functionality without the need of implementation this sounds like a really abstract concept but it will make a lot of sense in the engine so what this lets us do is send the same signal to many objects and the result will depend on the implementation that each object had for example if we call turn on inside a light blue light bulb blueprint then the light bulb will turn on but if we call the same function defining the interface that it's called turn on but we call it on a car in this case a car engine then it will start that engine if that if that is what we have um implemented or defined inside the car right so each class will have its own kind of implementation of this interface so interfaces don't care if the receiver has implemented or or not we will see an example that if we call for example at the turn on functionality on a rock and we haven't defined any type of behavior of it then it will do nothing this is still one-to-one communication and the sender needs a reference of the receiver to be able to send the message now let's go to the engine and i should have it right here here it is and let's open the map that we created if we created one yeah here's a map nope there's no map here so we created a direct communication folder let's create one for interface communication and let's get all these this folder let's copy to the interface communication copy here and i'm gonna rename it blueprints gamemode interface because i i want to have all the types of communication in different in different files i don't want to keep overriding this stuff right so let's use this map that it has nothing let's go to world settings let's get our interface communication instead of the default pawn being direct communication third person blueprint we should also have interface communication because that's why what i rename it great let's save the map let's save it on master blueprints interface communication and let's create a new folder here called maps and that that's not maps there it is and this map will be called interface com right so if we press play we should have our interface blueprint that it still has all the code that that we put inside that i believe we didn't put anything here so that's that's fine total coins and games gain coins that's that's okay and my pickup where's my pickup let me duplicate it if i have it here going pick up i'll pick up yeah i'm gonna duplicate this no i'm just gonna get the base one and i'm gonna drag and drop it to my blueprints folder and i'm gonna call it interface pickup just just to have a different name now this should still be working let me create that child blueprint class and this will be the coin interface coin i'm gonna call it and pick up mesh i'm gonna set it up the same as my coin that how did i set up my coin here sphere collision i'm gonna copy the sphere collision paste it here or i could also and this could be a lot faster just copy my coin pick up blueprints and i'm gonna copy here this is my coin pickup interface con interface pickup okay i'm gonna delete this interface coin let me close the windows otherwise it won't let me i mean interface coin communication blueprints let's delete this there's one reference let me change the name and let's let's call it just delete delete this and i'm gonna right click no and i'm going to right click the folder and fix any redirector so there is no reference there and this coin pickup i'm going to call it interface going pickup let's double click the interface coin pickup and select and instead of parenting to blueprint pickup let's go to class defaults now class settings and parent class change it to interface pickup it says it will cause some dos let's re-parent it shouldn't cause any loss because even though it's another class i i copy and paste the base so it should be really fine right so now i have this type of setup where i have an interface pickup that will work as a parent and let me open it and this is a child blueprint of that one and this well this child blueprint also has the number of coins to give and the coin size and all the other stuff that we set up earlier right so we already have some functionality here what i want is to change the way this is going to work this event actor begin to roll up and this picked up timer and here in my interface going pick up what i would need to what i need to do is set up the functionality that i need specifically for this pickup but we need to have in mind that this actor begin overlap here that it's the the action of getting the pickup i can't open different well the parent is is this one is it's interface pickup and actor begin overlap so we can see it here here we're getting a class get class and we're here we're checking we're not picking up the same blueprint the same pickup blueprint but we we already have a functionality called picked up that just toggles the collision and plays a sound right so in my child blueprint what i can do is use this like we did before but we don't want to have this type of casting right because it will work right we can change this to cast to interface and third interface communication third person character and it is as as we mentioned in the last video on the direct communication this will work but it is a lot of work to have to cast for every single type of character right so what we're going to do is create an interface let's go here in my blueprint folder we can right click blueprints and let's create an blueprint interface these blueprint interfaces interface i'm going to call it bpi because blueprint interface underscore and pickup actions this is a a type of blueprint that is just a read only type let's double click and you will see what i mean read only we can't add any type of functionality here we can create nodes what we can do is define the functions for example if we want to gain coins then let's type it gain coins if we want to gain health then gain health if we want to gain gain ammo maybe it's a first person shooter gain ammunition so we have this functionality but it is not implemented here in this class like the like the theory set an interface interface defines functionality but it doesn't need the implementation right that implementation will come from the actors that implement the interface so if my uh where is my interface coin pickup here right so if my interface coin pickup wants to call the gain coins functionality inside an actor we really don't do not need to cast anything we just we can delete this and in this actor i want to call that game coins gain and because we have implemented it in an interface where it's not an implementation it's a definition because we have defined it in an interface we can call it from any actor right we can call gain coins because this is my coin pickup and if we leave it like this this actor needs to have implemented this functionality in order to work or to do something right but let's let's go back to the presentation the interfaces don't really care if the receiver has implemented them or not so if we called my in my character that interface communication third person character there is already a game coins but we are not we're not casting to this type of blueprint and calling this functionality so because we have not implemented our interface our bpi pickup actions interface here if we play and i didn't put any any coin in my in in my map let me put my interface coin pick up here it is it was that one yeah yeah it should be the one if we get it and it seems that there is no mesh here right now let's fix that pretty fast cylinder let's search it there it is and there is my coin pickup let's put it here cylinder should be good right compile save and let's close my blueprint coin pickup because it will mess me up so there is something there so it's okay by that if we pick pick this up it it is do nothing it's not showing me anything so even though we're calling an interface functionality because that blueprint has not implemented that a specific functionality then it is doing nothing if you wanted an error message a customer message then what we would have to do is in in wherever i'm calling that pickup action here it is in my interface coin pickup blueprint in my override of my pickup action we can ask the actor if if it has implemented that interface so here we can ask implement does does implement interface and we we ask for the blueprint interface pickup actions if this is true this is only if you need that this er this type of error message if you don't then it's okay if you just leave it like we had before but if it if it is implementing this type of interface then call the game coins if it isn't then we can show a debug log here saying pick up action interface is not being implemented what have i done type pickup action interface has not has not been it's not implemented not implemented there it is pickup action interface not implemented play and it shows that it has not been implemented so let's simplify this let's just leave it like this and let's implement this this functionality this functionality is being called on the actor the actor is this interface communication third person blueprint so here let me go to the event graph and i don't really need to be in the event graph i just need to class settings interfaces and here i'm gonna add my pickup interface pickup actions now let's compile and it will show me an error because there there is already a game coins function here so let me rename it to alt because i'm i want to copy and paste that code all the gain coins right so now what we really need to keep in mind is that if my old version of the functionality here needs a coins to gain how many coins i'm gonna gain then my pickup action my pickup blueprint interface function should also have that that type of of input right so here i need what do we need an integer called coins to give so i'm copying that name gain coins inputs how many coins i'm gonna get and this is an integer the same with health we should we should also say say and give this information of how many health i'm gonna gain and with the ammo the same stuff right and here it should be also an integer because we don't want to give the player half and a bullet so gain health gain coins and gain ammo all of these functions now receive an input right so let's save it compile it let's go back to my coin pickup and now i need to give it how many coins i'm gonna send my my character so i'm gonna drag and drop number of coins to it on top of coins to gain and there it is let's save it compile it and now let's go to to my interface communication third person character in order to implement the function defined right now we have implemented the interface here just by doing class settings and implemented interfaces we have added it so with that we have access to this little menu here interfaces and this will show all the interface functions that we have available and it is good to categorize these interfaces let's put it a category pickup because if they can get lost really really fast when you have more than one interface so let's select pick up for all of them here it doesn't make much sense but if we compile and go to my character now it will it will make sense because because if we add another interface then we will be able to categorize them in a clean way so now to use any of them we just need to gain health for example right click and implement event it is giving me an event because this function does not have an output if whenever i gain health i want to know if my health have reached the minimum or the maximum maybe i can get another an output right i boolean saying is filled and let's compile and if we try to implement implement this type of interface function then here or oh i implemented the the wrong one i'm gonna delete that compile should be fine the game coins was the one that i wanted to right click implement event and now that i changed the gain health and the gain health returns a value i cannot implement it as an event because events do not have return values so if i try to right click and implement it as an event there is a convert function to it but it isn't what what we want to do it doesn't show me the same option implement event right and in this case we can go focus and it will find my implemented event we can just double click my gain health and it will open up as a function which is kind of clean i i like it better this way instead of having like events floating around but if i needed to do something that only any an event could do like i don't know manage a timeline or a delay or something that i don't want to run on the same frame like we talked about in the blueprint in the free blueprint course then i would use an event like this but in most cases what i would like to do is in this case game coins maybe i want to know the total of coins at the end of this action so outputs in their total coins i'm gonna ask here total coins compile save it let's go back to my blueprint character let's delete this because it can't be an event now and just double click gain coins and here i could use functionality that i already had for example this one and i i could call here old game coins right and then i could also hear total coins just return this but what i i gonna want to do is i i don't want too much redundancy here old game coins i don't really need it so let's let's just copy and paste this code go to gain coins connect it and my total coins let me move them to the side and now it should be almost the same stuff right compile save my event graph it's still clean gain health gain coins has been implemented gain health it doesn't have anything inside it and now save compile and let's test it if i get it now it gives me one coin two coins three coins four coins five coins and it is useful because if i go to my pickup this interface is returning the total of coins that my character has right so if i needed to do something with this type of of information then i could and it it could be really really simple if i wanted to add my gain health and gain ammo well i would have to do something very similar right i want to remember if here i have a health pickup yeah i did let's copy it to my blueprint blueprint folder in my interface examples and copy here i'm gonna do exactly the same thing here healthpickup i'm gonna rename reparent the class to my interface pickup and i could leave it like like it is because the thing that is doing all the work is just the the child blueprint the base pickup i believe it is the same but i'm just copying and pasting it just for the sake of clarity so we have like here in the folder uh if you want to know which one of these is apparent this is the parent but it could be the the normal parent or the direct communication pattern because like i said we're doing the the the change of functionality inside the child blueprint so here i want to call my gain health action so here functions i need to override my pickup and now with actor that pick this up i just need to go gain health how much health i'm gonna i'm gonna gain well this blueprint should should have have to end i have to gain variable there it is and this one returns if the bar is filled or not maybe it can give me some shield if it's filled or something like that and i'm gonna set it up to 20 and this will work exactly the same i just need to implement this gain health interface on my interface communication third person character so here game health double click and i can do something so when whenever i pick this up i can i can also return how much help i have or update any type of ui that i i could need right so now that we have a clear example of how we're doing the communication through interfaces and it is a lot clearer that an interface only defines functionality and the implementation it's on the side of the actor that wants to use it right if that a light bulb you want to use it with a light bulb or a car you can implement different types of ways of of returning of or interpreting the signal that has been sent right when to use these types of interfaces modularity well when the sender wants to send a signal to any object and it doesn't care of what the receiver will do with it when it gets it this is really useful when you're doing switches and maybe i can paint a picture here let's say you have this button and please forgive my my drawings but let's say you have a button here this button could be used to activate a gate or a door i'm gonna draw a door if you press it it opens if you release it it closes right it could be used also to operate a light bulb like we said before you will turn on the lights if if you press it or switch on the lights you press it again you could also activate i don't know maybe traps if you activate it then maybe some spikes can pop out the the floor right and you don't really need to define like cast is am i sending the signal to a door then open it or if it's a light bulb then turn it on we can just create one interface and this interface will have the activate a function in it and we do not need to to go into details on the implementation my door will implement activating for for the door it will mean open or close for the both this will mean turn on and for the traps it may be just a thriller trap trigger the the spikes right so it is it will be really powerful for objects that can interact with a lot of other types of actor and you just want them to activate or do something really generic and b auto implementation in some cases that receiver can call implemented interface functions from within itself what does this mean let's go back to the to the engine this means that if for some reason here in my blueprint interface come third i need to gain coins i don't really need to have another function here called all the gain coins let me delete it i can just ask the same functionality right click gain coins and it is not an interface call i mean it is using the definition of the interface but it is calling the implementation inside this character so this will be the same and let me undo my deletion of my function do i have it nope there it is like i can do this to call this function i i don't know if i can do also this yeah i can do it so even though this has been implemented as a part of an interface outside this actor we can still call it from within itself so we don't really need to duplicate to match functions and this will work exactly as we have implemented here now let's go to [Music] to event dispatchers so let's talk about communication through event dispatchers this is the last type of blueprint communication we will see and in this type of communication the receiver is the one that starts listening for the signal of the sender so it's kind of reversed the roles where usually we could have uh in in the example we were working on our pickup that sends a signal to the character in this case the one receiving the signal is the one that needs to pay attention to whatever object has that dispatcher so it it will be something like this but the signal is still being sent this way but just the way that that we set up the type of communication it instead of the object the the descender having a reference to the receiver it's reversed the receiver is the one that needs the reference for the sender so the receiver can do whatever it needs with the received signal and the sender doesn't need a reference only the receiver when the sender sends calls when well when the sender calls the event dispatcher then all the receivers that were listening or that were binded will get the signal it doesn't matter if no one was listening at all so we don't have to worry too much about that a good way to explain this type of communication in a clearer manual manner if with subscriptions right like for example youtube subscriptions if i create a youtube channel let's say this is my channel my channel will send a signal every time i upload a video so if at this point in time one video is uploaded then it will notify all my subscribers if i had zero subscribers then no one is notified but this sender is still sending the signal of an or the signal of an upload but if i had i don't know maybe three subscribers they are actively listening well if you click the bell and all that notification stuff but let's simplify it a bit they are actively listening for any type of activity of this channel right so it shows in the in the sub page so whenever i upload the video then this signal gets sent to those three subscribers and now does each subscriber or each object can then decide what to do with a signal maybe this subscribe subscriber wants to see it immediately then press play if this other subscriber i don't know maybe it doesn't have any time at all and just put it in the watch later and this other subscriber seems it simply ignores it you have the same signal but these three people that were listening or were binded to that event in this case upload event could be another type of event could be posts could be just i don't know shorts whatever it is if the subscriber is actively listening to to to well each event you would need to listen to specifically these types of events then whenever it happens then the subscriber or well the the receiver actor here can react as as it likes so hopefully this is a clearer example of this theory and we will see how how we can implement this in unreal so let's go to the demo and let me open unreal so here in the engine then i'm gonna set up the basic folders that i need in this case we have direct and interface so let's create one for event dispatchers and now [Music] a folder called maps and my blueprints folder that i'm gonna use to duplicate no new folder so i'm gonna do like almost exactly the same blueprints here gonna take i could take all of them yeah let's take all of them accept the interface right so let's drag and drop it to let's drag and drop it to blueprints in inside my event dispatcher folder and copy them this one is gonna be gold event this this one also and let me copy this so i can i can use it on everything so this is a blank map i'm going to use this game mode event dispatcher communication so let's game mode overwrite let's use that let's change it instead of interface communication there should be event dispatcher because we already have renamed it and this interface can pick up uh this one i'm gonna delete i only want one blueprint event dispatcher interface coin pickup i'm just gonna change i'm gonna get the cylinder and then i'm gonna go to class settings parent class and find event dispatcher pickup report i may have lost the static mesh but i have it here on on the content drawer so let's use it great it's fine if it's rotated like this for now so what we want to do is use this new knowledge of event dispatchers right so a type of event that i would want to notify other classes is the event of this pickup being picked up like this like like this function right so if every pickup will have this pickup function i should modify the base where is my my base pickup should be event dispatchers blueprints and then this and i should change the the name of this this blueprint instead of interface event dispatcher yes so we don't get confused now let's go to the base blueprint the base of this blueprint that is the event is pickup we can just click on open parenting editor and here i have my pickup function right that it does something in my case is totally in the visibility and playing a sound right so in order to create the event dispatcher that other blueprints can start to listen to then we just need to go here event dispatchers and click on the plus sign this new event dispatcher usually even dispatchers start with on so if it's picked up it should be on picked up now this does not work as a function we cannot double click this it just shows us read only we just need to call it this calling the event dispatchers it will be like posting a video the upload action so here we already have a function when we pick up something so let's just drag and drop my event dispatcher here we can call bind and bind or unbind all i just need to call it so i'm gonna do this so what this is doing is whenever picked up is called then with all the visibility and collision we play a pickup sound and then we call d7 dispatcher called unpicked up right so right now there is nothing listening to this event dispatcher so let's let's do something interesting i'm gonna put my should work with a coin because the parent has the functionality of having created that event dispatcher and also calling it so now what i'm gonna do is make for example like a little trap that could get triggered by by the player picking up this this pickup kind of like a trap in an indiana jones movie so for example if we wanted to [Music] have a boulder here on top let's click create let's press shapes there should be a sphere here's a sphere let me scale it so it presents a thread we want to activate it whenever we get this pickup right the way we we will activate this i'm not going to create another blueprint i'm just going to on details i can activate simulate physics and this should be removable that's fine simulate physics should be what i need simulate i need false so i'm gonna disable that on the level blueprint i'm gonna select first my up i'm gonna go blueprints open the blueprint i'm gonna right click this is direct communications create reference i could create a reference or i could add an event or blueprint event this dispatcher coin pick and here there is a default category and there i will find my add on picked up unless i i created a category or of itself like for pickups but i didn't so it will be on the default category and add on pickup this will create an event and now i can select my my sphere go back to [Music] to my level blueprint right click and then create reference and i'm gonna activate set simulate physics yeah like this so similar physics the static mesh component that's the one that has the physics and click on simulate so now i'm gonna save the map the map i'm gonna save it on event dispatcher communication maps and how did i call the other maps and my interface com then i'm gonna call it m aventis com right so let's press play new editor window and whenever i pick it up then the trigger is played so that that's the way the [Music] the pickup didn't even know that this boulder existed that this sphere existed but with even dispatchers we just need to inform the other the receivers of the event the receivers only need to start listening to it now this has been with a level the functionality of the event dispatcher with a level blueprint but if we wanted to have a blueprint with blueprint for example let's say i want a counter on how many times i have picked up this this pickup right so let's create a blueprint called counter blueprint glass gonna select adapter blueprint count i'm gonna put it on my level and ctrl e to open the editor or right click edit blueprint now here i'm gonna select the text renderer and let me see if it's big enough right now yeah it is let me rotate it move it and let me see if i can make it bigger here i'm gonna word size 50 should be more than enough yeah that's it and this will display the the number of times i have picked up usually it will start with zero and whenever i want to add one i'm gonna create first a variable called number of times picked up and this will be an integer and i'm going to create a function that's called picked up inside it i'm just gonna edit the text renderer set text that's the one i need and i'm just gonna place this number picked up number this is just transforming it to uh to a text but before that i need to add the number of times this has been picked up so i'm going to do it like this this will add 1 to my integer so yeah now we have a function that works that should work as we expect but we need to start listening to my pickup remember that in this type of communication the receiver needs a reference to the sender so in my case what i would need to do in order for this counter to communicate with this pickup then i need to create a variable so let's right click edit blueprint counter gonna create a variable and this will be a big pick up reference now this can be an actor oh excuse me this can be event dispatcher pickup here i have it when this picture pickup and when i'm going to do this press that i so it's instance editable compile it save it and here what i can do is when i have this blueprint selected if i go to my default category i can select all the pickups that exist in this map or i can use the this to select specifically the typica that i want if i had more than one then i can select easily if i want this one or if i want that so now it like i said it could be a event dispatcher pickup or also it could be an actor if we don't need the functionality because everything inside the map can be an actor but this will mean that i can select the floor i can select all the actors that are in my map in my case specifically i need it to be a pickup of that of that specific type event dispatcher pickup we could we could leave it as an actor and then cast cast the class but it is faster this way now with this in mind what i'm gonna do at the start of this counter is start listening to that event dispatcher to the pickup event so what i have to do with this reference at the beginning play is get the reference and i can type bind or i could type attach let's do it the manual way first so i can press bind and this will show me every event dispatcher that the blueprint has there are already event dispatchers for example for for damage or destroy or end play that comes with every actor but if we go to the default category we will find bind event on picked up so let's select that now with this in mind i can connect events for example i could connect it to this type of event excuse me i would need to create a custom event and doing it like this will fire every time that the uh this referenced this sender is picked up then it would fire the custom event that i could use it to call the picked up coin and let's see if it if it if it's working let's press play and the number is it doesn't seem to be working access none oh oh oh it's i i didn't set up the reference so let's let's do that there it is play now i see three or five and yeah i didn't didn't choose the color right so let's change the color to something a lot more noticeable now we're talking now we don't want to get too tangled on the on these events dispatchers and how the events are connected right so let's say we have code here that we don't want to move and we have something like this and it gets really messy then we can just disconnect it drag and drop from here and type create event now it will ask us to select a function or even an event we can create one here create matching event or we can create also a function or we can use something that already resembles it resembles a signature because if the event dispatcher is giving me for example here on picktap has inputs that i can add new parameter we can add more inputs here then the events connected here also need to have those inputs so if you don't find your event then it's because you are not having the same inputs an example would be bind on take any damage oh that was the unbind excuse me bind event on take any damage this one i can create the event and here let's create a matching function you will see that it's not it's not pointing to my custom event 0 or my custom event 1 because it's not matching the signature so we can create a matching event and this is the the signature that we need let me delete it now this is custom event zero and now it should work as it was working before right but we can ignore this event because we already have a pickup function right so let's call it pick up coin directly no distractor no pick up coin play and now it will still work if for some reason you want to stop listening to this type of event maybe after some delay delay i don't know maybe two seconds i want to stop listening to this big pickup event then we can unbind it and we need also the reference so let's drag and drop it like this it's the same reference but it's i find it a lot more readable this way and here unbind you just need to tell it to unbind which event you have unbind event from on pickup or unbind all events if you unbind all events this is also unbinding events from other blueprints that have been that are that have been listening to the pickup event so usually you don't want to do unbind all because you can remove behavior that you really want so just unbind event from on pickup and you need to select the event you can do exactly like this or you can create another event there is no no problem there something like this pick that up coin let's press play and after two seconds it won't work one two now it shouldn't it work that is the way we have to control the blueprint and to implement it so now that we know how to implement this in unreal when should we use it well when you need a one too many communications the same event sending a signal to multiple receivables we should use event dispatcher this is true one-to-many communication they will get the same signal at the same well almost the same time now simple execution when it's not needed for the sender to get return values from the receivers and this is good to know if you need the receivers to send back a signal then an event dispatcher won't work for you it's like an like any event that doesn't have a return value so if it's not needed then you should use the even dispatcher it's usually really simple to use and you will need to cast that's something that i forgot to add here but usually you you will need to cast the the the glass whenever you are binding the event dispatcher so just take that in consideration generalization when you know who is the sender but have no idea who are the receivers or plan to create them later for example the pickup i know that it's a good idea for the event picked up to be used but i don't really know who will use it maybe it could be a character maybe it could be a trap like we did in the map maybe it can be another system inside my game that needs for example usually the type of system that keep a record of how many pickups have you used maybe how many jumps have you done those are really useful instead of checking every frame if the character has jumped it's better to create just even dispatcher that will tell that class to keep keep the record of how many jumps we have done control when the receivers know when to stop listening the doing the unbind when they know to stop listening to the sender then it's also good to know you you don't really want all of the time to listen to the same event like for all the duration of the game usually you want to unbind it at some point it depends on case to case from your game or your project right so if you do know when they should stop listening then it is better whenever you use this type of communication this even dispatch your communication so now you know how to use these types of communication direct communication interface communication and event dispatchers this is a little cheat sheet where we will use these questions to determine which type to use and this can be really helpful if you [Music] start forgetting what we talk about in each in each video so you can jump to this video anytime you want to remember like how or when especially when to use each type of blueprint communication so good questions to ask yourself is is the sender pushing data to the receivers if there is data involved and the message is clearer then direct communication or interface communication are a good choice between direct and interface communications it will really depend on how connected are those classes so you can if you want to stay make make your cup your code stay decoupled which means like there are not many references between each other and they can work they are easily extractable from the project you can use them otherwise other projects or it is easier to change functionality then you should use interface but some having said that it's it doesn't mean that you should use interface all the way because sometimes you really cannot decouple too much the code otherwise it becomes a mess and there are use cases where direct communications is really fine there is no problem with that the other question is are the are the receivers actively listening for the sender to do something then we already talked about the analogy of a of a youtube channel having subscribers then if the receivers are the one that wants to know that if the sender is doing something then even dispatchers all the way then can the sender get a reference to the receivers then direct communications or interface communications should be fine and if only the receivers can get a reference to the sender then even dispatchers this really depends on your type of game and specifically the the circumstances that the communication is is happening for example if you want to communicate to a player but the player doesn't spawn immediately then you do not have a reference to the player so maybe the player is the one that ha once that should have the should be the the sender of the signal right so it really depends how easy is to get that reference we already saw different types of getting the reference we can use the the node get all types of factor we can use reference and make it instance editable so we can select it on the map or we can just get through maybe a sphere collision the type of blueprint that we are colliding with so it really depends on what you're trying to do but these four questions will help a lot whenever you want to you you're not really clear on which type of communication to use now it is really important to keep consistency on your project so if for for example in this case you can use direct communication and interface communication but if you use a interface communication for pickups then you should inform all your team that if something has to do with pickups you should probably use interface communication and this unless they have a very good reasons not if they have a good reason to not use interface communication then it's fine you can argue that but it doesn't matter it sometimes it becomes it becomes like more of a taste thing like a preference so just try to keep your project consistent and use the same communication for the same types of interactions also here's a actor communication reference table that this can be fine uh where did i find it that this can be found in the unreal document documentation and i have put it here just to be easy to access so communication type direct communications when to use it when communicating with a specific instance of an actor in your level requirements the reference of the actor in the level and an example triggering an event of a specific actor in your level casting then you want to verify that an actor is a certain class to access its properties you need the reference of the actor and then you need to cast to the desired actor example accepts in accessing functionality of child actors that share the same parent class interfaces when adding the same functionality to different actor classes you need a reference to adapter and you need to know the interface class and you can also check if it's being implemented or not example adding an interaction behavior to different types of vector which we already did even dispatchers when triggering an event from one actor to many actors and this is the the most common the the most common way to know if you should use an event dispatcher whenever you need to trigger something from one to many actors actors need to subscribe to the event to react to it and notify many examples notifying many different types of factors that an event has been triggered this is especially useful when when developing should it be called reactive menus well menus that can be populated through data for example if you have a data table where you have i don't know four types of objects and you want four types of buttons to it you can create an actor and that widget and that widget can have an event dispatcher for whenever it's clicked that way you know when each of those created actors on the spot has been clicked well widgets excuse me so it's really useful when dealing with those type of uis or menus that needs need to be populated before being used so having said that you now should have a pretty good idea and be really used to blueprint communications and the three ways that we have for it to to do exactly that so it's just a matter of using them and asking asking yourself those ques these questions so you can decide which one of these types of communications it is a better fit to your need
Info
Channel: UNF Games
Views: 29,831
Rating: undefined out of 5
Keywords: unreal engine 5, blueprints tutorial ue5, how to install unreal engine 5, unreal engine 5 tutorial beginners, tutorial unreal 5, tutorial ue5, how to use unreal engine 5, learn unreal engine free, download unreal 5, how to create a game with unreal engine 5, unreal engine 5 early access, download unreal engine 5, download ue5, introduction unreal engine 5, intro unreal 5, level design unreal engine 5, level design ue5, ue5 level design tutorial, unf games
Id: QC8sTf7bklg
Channel Id: undefined
Length: 100min 2sec (6002 seconds)
Published: Thu Jan 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.