Blueprint Interfaces | Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
blueprint interfaces are an essential Concept in Unreal Engine that will make your life a whole lot easier and in this video we're going to look at when and how to use them so I've taken the starter content and I've made a very basic interaction system that piggybacks off of blueprint interfaces for the sake of demonstrating in this video so here we have a door if we walk up to it we press F it'll open and we can also close it we have a chair that we can take a seat in again by pressing F and if I press F again we'll get up then we have a switch that toggles a lamp and a switch that can start and stop a moving platform and finally we can pick up the first person gun by walking up to it and pressing f so the first thing I want you to ask yourself is what do these things have in common well besides being interactable they really don't have anything in common and that's usually when you'll find that blueprint interfaces are a good fit for doing something they share one piece of functionality which is that they can all be interacted with but other than that they're pretty vast and different so enough talking let's just jump in and demonstrate why that's important why a blueprint interface is great for this and how to set them up so real quick we're going to take a look at the first person character now what I have set up here is just a basic line Trace you don't have to worry too much about what's going on here all that's happening is it's when I press f a line is going to get drawn from the camera 500 units straight forward so whatever Direction I'm looking in we'll draw a line it'll hit something and then it'll give us data about whatever it hit now if you saw my video on casting or if you know about casting in general you'll know that in order to access any of the events functions or variables inside of a given object in the world you'll need a direct reference so in this case if we were trying to make an interaction system via casting we would have to take this hit actor and for example say cast to BP underscore door and then assuming the door had a built-in interact event we could then come off of here and go on interact I didn't actually put a built-in interactive event into the door and you'll see why in a second but the problem with this is we also have a chair right so the only way to really go about adding our chair to this interaction code is we'd have to come off of cast filled and then we'd have to go cast to BP underscore chair and then we'd have to call its interact function if it succeeded and then now we added in a switch so we have to come off of this and cast to BP underscore or a button rather and this is this is just bad first of all this is really badly unoptimized casts are actually expensive because they create what's known as a hard reference meaning they're always loaded and also if you think about it if you go up to the button it has to first check all these other casts before it gets to the button and but besides those those aren't even my biggest problems with this the biggest problem is that anytime you want to add a new interactable object you got to come into here you gotta go to your humongous nest of casts and you gotta add in the new object and that's just not modular it's not scalable it's not practical so to get around doing this we're going to use a blueprint interface so I'm just going to delete these and we're going to hop out of here for a minute and go into the content browser I'm just going to go into the folder I made and to create a blueprint interface what you do is you right click go to Blueprints and blueprint interface now I'm going to name this BPI underscore interactable and then we're going to double click to open this now I feel like this is where people who are new to blueprint interfaces get confused this is just a read-only graph it really doesn't serve any purpose other than to show you what your events and functions look like you can't add anything in here and that's actually kind of the point of blueprint interfaces as we'll see all a blueprint interface is is a list of declared functions and events we take our blueprint interfaces we put it on a class and it will now have those events then we Define the functionality for those events in the class for which this interface is on if that's confusing don't worry we're going to do it right now so we start off with this new function I'm going to rename this on interact and you can see that we can add inputs and outputs just like we can with events and functions in any other class real quick I'm going to add an input I'm going to call it interacting player and we're just going to make this a generic character object reference that way we can get a reference to the interacting player in the blueprints that we're interacting with and that's actually really useful as we'll see in a minute now for demonstration purposes I'm also going to create another function I'm going to call it is interactable and this time we're going to give it an output it's going to be a Boolean and we're going to name the Boolean interactable so one thing real quick to note about interfaces is anytime you declare a function and it doesn't have an output it's going to show up as an event as you'll see in a minute and anytime you do have an output it's going to show up as a function which should make sense because events don't have return nodes and functions do but anyway enough talking let's go see how to use this so in my first person map what I'm going to do is I'm going to open my door BP now I have the code already in here that allows the door to function as a door without the interacting aspect of it we're not going to get too deep into this because this isn't a tutorial about doors it's a tutorial about interfaces and interaction this project file will be available for download at the link in the description so if you want to dig into this stuff more you can feel free to download it and check it out anyways now we're going to add that interact interface to this store and to do so what you're going to do is you're going to click on class settings and then you'll see interfaces over here and you have this drop down to add we'll go in here and we'll type in BPI underscore interactable and if we compile make sure you also compile your interface or you won't see any change now if you look on the left hand side in our my blueprint panel we have is interactable and on interact now if I right click on interact I can implement this event and you'll see we get this on interactive event and it's also outputting and interacting player which I set up this door so that it'll always open away from the player and in order to do that we're going to need a reference to our player so I'll just plug this in here now you might be thinking why is this useful well I'm going to show you the magic right now in the first person character you might be thinking okay great I still have to cast to my door in order to access that interaction event but actually you don't if you drag off this generic hit actor and you type in on interact you'll get that interact interface call and then for interacting player we can pass in self and that's it that's all you have to do now you can just take that interface put it on any item that's interactable Define its functionality inside of it and you never have to touch any of this code there's no need for nested casts none of that this is done the only time you would have to edit this is if you wanted to add new features entirely to the interact system itself so like if you wanted to have a prompt on screen that you know says press F to interact with object then you might need to add some stuff here which would also involve adding new events and whatnot to your interface but I'm getting a little off topic the point is this will work and it will work for any any object that we put the interface on you might be thinking well what if the hit actor that comes through here doesn't have an interface and the answer is nothing it just won't do anything you can if you need to check you can go in and you can do does implement interface which will and you have to select this class drop down called BPI underscore interactable and now you have this Boolean where you can branch and you can check if a given object implements an interface put that right here if we want it won't make any difference it won't make any difference in this case but there are times where you might just want to know in general if something has an interface and this could be useful now remember we also had that other interface function the is interactable and what I'm going to do is I'm going to get rid of this for now and I'm going to call that is interactable function and you can see that we now have access to that output so if I go here I go branch and this could be useful if for example you wanted to make something not interactable after you're done interacting with it when you think about like maybe some of the doors in God of War you can walk up to them open them and they stay open and you don't have to interact with them again something like that and we'll plug that in there so now if we go back to our door and we click on this is interactable function you'll see we have this interactable variable right here we'll set it to true and if I compile hit play walk up to the door and press f it works and if we go to the other side of the door like I said it will open away from the player now all of these objects follow the exact same principle if we look at the rifle for example all I've done is taken the code that's included by default in the first person template and instead of using an on component begin overlap with a sphere Collision I've just made it so that we use this on interact event from the interface so that we can pick it up by pressing F but now I want to focus our attention onto these buttons here because these buttons are making use of two interfaces so applied directly to them we have the BPI underscore interactable interface but we're also going to make use of a second interface so that we can control whatever we want with a button without having to change the button's code so we're inside the button now and as you can see it has the interact interface and I've deconstructed the rest of the functionality so that we could put it back together right now but basically this button might control anything so as I showed before one of these buttons controls a lamp and the other one controls a moving platform which are two very distinct things so what I did was I exposed a generic actor to the details panel that way we could just drag a button into the world and select any given actor so in this case so in this case we'll select the light and now we need to make it turn the light on and off now yes of course like always if I wanted to I could cast to the lamp but the problem is then it's not going to work for the platform so what we're going to do is we're going to create another interface I'm going to go here blueprint interfaces and we're going to call this BPI underscore electricity and for the sake of this video all I'm going to do is add one event and we're going to call it uh on activate okay and we'll compile and now this interface will go on any object that we want a button to control so we'll go into the lamp and all I have here is the lamp static mesh from the starter content and a point light and I have the code here to basically just turn this point light on and off in order to run this code what we're going to do is go to class settings we're going to add that BPI underscore electricity interface and then you'll see we have that event on activate we'll implement it plug it in like so and then back in the button we will run that on activate event from the electricity interface and that's it now we can drag a button into the world we can select any object that we wanted to control just slap that electricity interface onto that object we don't have to change any code in the button and it will work and also real quick I forgot we have to check this is interactable checkbox but now if I compile okay play walk up to this button you'll see that it toggles the light and it's the exact same thing for the platform if I go into the platform add that BPI underscore electricity interface Implement that event you can see I have all the code here already to make the platform start and stop again not important for this tutorial we're just going to plug this event activate right here and then if I take this button I already have the platform selected I hit play and it makes the platform move now to drive all of this home I'm just going to create a brand new interactable object from scratch it's going to be really simple it's just going to print out a name when we walk up to it and press f so in my blueprints folder I'm going to create a new blueprint class we're going to make it an actor and I'm going to call it VP underscore Cube guy all right we're going to open that up and real quick just for Aesthetics I'm going to add some components okay and we're going to give it a string variable called name or actually we'll call it first name and we'll expose it to the details panel so we can set it in the world and then what we want to do is we want to print out this name when we walk up and press F so I'm going to go to class settings add VPI underscore interactable we're going to go into the function and make it interactable and then we're just going to implement the on interact event we're going to take first name and we're going to print string all right we'll drag this in make its first name Brian thank you and now when I hit play walk up to the cube guy press F you'll see it prints out Brian and it was that simple to add a brand new interactable object all right and that's all there is to this video thank you guys so much for watching I hope you found it useful if you did please slap a like on it and consider subscribing for future videos from me if you have any questions comments or concerns leave them in the comments down below and I'll do my best to get back to you and yeah I wish you all the best of luck with your projects
Info
Channel: Tyler Serino
Views: 5,425
Rating: undefined out of 5
Keywords: Blueprint Interfaces, blueprint interfaces ue5, blueprint interfaces unreal, blueprint interfaces unreal engine 5, unreal engine, unreal engine 4, unreal engine 5, blueprints, scripting, making games, ue4, ue5, game dev, game development, unreal engine tutorial, blueprint interface, blueprint
Id: wGmlKbllPcw
Channel Id: undefined
Length: 14min 40sec (880 seconds)
Published: Tue Jun 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.