How To Interact In Unreal Engine 5 | How To Use Blueprint Interfaces In Unreal Engine 5 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another unreason 5 tutorial so in today's video what we're going to be going over is covering blueprint interfaces and more specifically how to use them to interact now these can be used for absolutely anything not just interacting but that is the main thing I'm going to be covering today as just an example to show how these work so let me hit play and shoot we're going to make today very simply we're just going to create something which will allow us to interact with an open and close this door you see here and we're doing this via blueprint interfaces not doing it in the door blueprint itself this just makes it a lot more efficient and a lot better for us and a lot easier to expand upon further down the line in our game as well so this will be going over and creating today so without further Ado laminate this code and I'll show you how I've done it and once again like I say this can be used for anything it doesn't have to be interacting and if you are doing interacting it doesn't just have to be for a door this can be anything you want so firstly let's go over what a blueprint interface is so on the documentation it is defined as a collection of one or more functions which are name only no implementation that can be added to other blueprints so essentially it's a read-only function which we can call in other blueprints to be able to interact between different blueprints so essentially a good way of thinking about it is like a better and more efficient cast so this will probably make a lot more sense when we actually start getting into using it so let's do that now so what we're going to do first is we want to actually create our blueprint interface so we could open up our content browser right click go to blueprints not blueprint class just just blueprints here and we're going to create a blueprint interface that you see here I'm going to Simply name this one interact interface like so opening it up straight away the name doesn't matter too much but I always like to put interface at the end of my name just so I know what it is this function here I'm just going to call interact that's all we need to do in here because again this is a read-only function it's a read-only blueprint we can't do anything else in here we can't do the code we can only name the function itself however we can add some inputs and outputs on here if we wanted so you can add an input for example if you wanted a specific character a controller you wanted to input a Boolean a float or you wanted to Output a Boolean or a flash or anything along those lines you can just simply press this input button here and do it there like so it is that simple and that will allow you to then transfer different data between different blueprints but we're going to close that because again that's all we need to do then we want to open up where we want to do the code for this interface now this will vary for you depending on what this code is so for me it is an interact interface so it's my interaction code so I'm going to do it in my player blueprint now you might think you don't also want to do it in your door blueprint or your car blueprint or whatever it is you're interacting with that's not true you only want to use whatever is going to be firing this off so for me that's the player blueprint so I'm going to go third person blueprints VP third person character and once we're in here we just want to set up our interaction code so what I'm going to do first is go to edit project settings then we're going to scroll down to input here and we're going to create an action mapping and I'm going to name this one interact as that makes no sense for me I'm going to press this button and then press e as I want it to be on my e key this can be whatever it is for you so e f or left Mouse button whatever makes no sense for you just input that in there but again for me it's going to be the E key then we're going to close this back in our event graph we're going to right click and search for the ACT wrapping we just created so I need mine interact now you notice we've got interact message that's not what we want that's from the blueprint interface which we're going to be using later we want the action event interact here like so and now this is where we're actually going to do our interaction code so I'm going to do it based upon what the player is overlapping not what they're looking at so I'm going to right click and get overlapping actors with the class filter being simply just actor so if we're overlapping any actors out of the array here we're going to get a for each loop with break so we're going to be searching through all of the different actors we're currently overlapping but being able to stop this loop as well out of the array element we're going to get does implement interface the interface in here wants to be our interact interface we just created like so I'll explain why in a second what we're going to do now is hold down B left gate to get a branch with the condition being the return value connecting that into the loop body there so what we're doing is for every actor we have that the player is overlapping we're going to check to see if it implements the interact interface and if it does or doesn't we're going to do different things there and the reason why we're checking this if it has the interact interface is because we only want to try and interact with things that we can interact with so let's say we're also overlapping a war actor we obviously can't interact with the wall I presume you can't anyway so you don't want to try and interact with it because that's obviously not going to work you'll most likely get an error so we only want to do things that have this correct interface in there so false means obviously don't want to do anything true means we want we want to interact with it so how do we now interact with the actor we're overlapping that's very simple we're going to come out of array element once again and get interact this time we can get that message there under the interact interface and then we put that into true of the branch and that now is going to fire off this interact function inside of the blueprint we're currently overlapping so this then works the same way in calling any function it's calling it The Target being the actor we're now overlapping and this is why it makes it a lot more efficient because normally what you do is you do this code or something similar to this in every single blueprint you have in the level that you want to interact with whereas here we just need to do it once in our player blueprint and then each individual blueprint we want to interact with we just have the specific code for that for example the door would be opening and closing a car will be getting in and out whatever it is for you we just do the code in there and actually interacting with it in here so it just makes it a lot more efficient and after this we just need to drag out of interact at the end into break there so once we have found something to interact with we're going to stop searching through the array stop doing the for Loop so again makes it a lot more efficient because otherwise it would also be interacting with everything we're currently overlapping which you might want to do but I imagine you don't for example if you're maybe doing an inventory system you're standing above five items pressing e you only want to pick up one not all five at the same time so we're going to compile and save that and close it and move on to the next part which is a lot quicker this is now actually just firing off the interaction code so we're going to open up the blueprint we want to interact with so again for my example that is going to be a door BP here now I've already set up all the code for interacting with the door as it's nice and simple this is all we need to do and all we need to do now to actually fire this off using the interaction code we just created is we want to go to class settings up at the top and you'll notice we have interfaces implemented interfaces no interfaces here so we obviously want to give it an interface so we're going to press add and search for interact interface the one we just created earlier and we're going to add that on there and now you'll notice a new tab on the left called interfaces and we have our interact interface here so it's our interact function so we're going to double click that and we're now going to get this event in here so if you had an output this will open it as a function so if I were to just create a function it will open it up like this but because I don't have the outputs it's just giving it as an event like so that's now all we need to do is in that function or that event you just connect in the code you want to fire off for your interaction so again for me that's opening and closing a door and it's that simple so now when we try to interact in the player blueprint what we're going to do is search to see if we're overlapping anything if we are fire off the interaction code for that blueprint which will then fire off this event here in whatever blueprint you're overlapping or whatever blueprint you're trying to interact with and it's going to fire off this code and do what we want so we're going to compile and save that now the final thing I need to do because I'm doing this based upon overlaps not if I'm looking at it what I need to do is go to the viewport add a component and I need to add a box because you know I haven't deleted it sorry so what I need to do now is just add a simple box Collision on here so we can actually overlap with this actor again if you're doing it based on a line tray so if you're looking at it you don't need to worry about this this is only if you're going to be overlapping it like I am I'm just going to scale it up to the size we want so the player has to be within this box station here in order to interact with it so this would be the same thing you do if you had your interaction code in here in the first place because you just need to be within this box Collision but we're just doing it slightly differently now and again more efficiently so we're going to compile and save that and that should now be the code fully done and working for us so let's close this and hit play to test it out if I go over here pressing e nothing's going to happen but if I go up to my door and press e we're going to interact with the door and it's going to open and close like so because again we're firing off the interaction code from our player blueprint which is then using an interface to interact with this door here to then fire off our interaction code which again for me is opening and closing perfectly like so so I think that'll be for this video is we've done everything we want to do what we've done is we've set up blueprint interfaces and gone over how they work and hopefully you know understand them a lot better and so you could utilize them within your game and again I've given an example here of an interaction interface as that is also probably the most common place you're going to use them but in one project I'm working on I've got so many interfaces for so many different things because again it's just a really efficient way of doing things so thanks so much for watching this video I hope you enjoyed it and hope you found it helpful and if you did please make sure to like subscribe down below so thanks so much for watching and I'll see in the next one okay [Music] [Music]
Info
Channel: Matt Aspland
Views: 67,254
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, ue5, unreal engine 5, unreal engine 5 tutorial, ue5 tutorial, blueprint interface, blueprint, blueprints, interface, interfaces, how to use blueprint interfaces, ue4 interface, interact, cast to, cast, interact interface, better, efficient, inventory, how to interact, door, open, pick up, close, object, item, ue4 interact, action, mapping, event, function, call, message, system
Id: 5-UJT4U-jeg
Channel Id: undefined
Length: 10min 1sec (601 seconds)
Published: Fri Nov 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.