Blueprint Interfaces | More Efficient Interactions | Interact Interface - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another unreal engine 4 tutorial in today's video i'm going to be going over a blueprint interface to create interactions in your game so previously i've made quite a few videos where i'm interacting with things however we're just casting to our character to interact with them and using the input action each time however using a blueprint interface is a lot lot more efficient so i'm going to go over how to set one up today and it's probably a lot more simple than you think it is but first off i'll give a very basic explanation of what an interface is so essentially it's just a blueprint in which we put in a read-only function so what that means is you can't actually create the function in there like the code inside the function it's just the input and output of said function and it's read only so again you can't edit that then you can call that interface and those functions in different blueprints so if i were to call the interact interface function in my character blueprint and then have the event for the interact function in another blueprint they will work simultaneously together i don't know if that makes too much sense however when i get into the video later on and you actually visually see it it should make a lot more sense for you so let me just give you a quick showdown of what we're going to make so we're in first person we press e look at the object and it interacts with it in this case it just destroys it so it looks like it's picking it up again you can do that to be whatever you like but without further ado let's get right into this so let me just delete this stuff and i'll show you how i've done it so what we want to do first is you want to create an interact interface or any blueprint interface you want so to do this we're going to right click go to blueprints and get a blueprint interface i'm going to name this one interact interface like so and open that up straight away again as you see here we have the functions and it says here read only because we can't put anything else in here but this new function i'm just going to name interact and you can add different inputs and outputs on here if you wanted so if you wanted it to be a specific character or if you want a boolean value or a float value on there you can do that and the same for the output so if you maybe wanted to add an output of successful let's say or did interact question mark and then when it's finished you can set whether it did or not interact with it so then you can use that later on so for example if you're making an inventory system you want to know if you did actually interact with it so you know whether or not you did actually pick it up so now i'm going to compile and save because that's all we need to do in there it's very simple all we need to do is create the function and give it a name and then we can close that straight away and open up our character blueprint and the other blooper we want to interact with so again these are the blueprints you want this interface to be implemented on so we want to use our character to interact and then i've just got this interact object here as an example to interact with so we're going to open up that object which here you see is just a cube i've got nothing else in here it's literally just a cube and then i'm also going to open up my character blueprint here which again is not really anything else in here it's just my normal character blueprint if i go to viewport you see there's nothing else in here however i'm using a first person camera and i do have a video on creating that as well if you want to both just first person and third to first back to third again once we've done that we're going to go over to the event graph here and what we're going to do is go on class settings up at the top and you see we have interfaces implemented interfaces we have no interface so we're going to hit add i'm going to search for interact interface or whatever you named it and select the one you just made we compile we now have that interface implemented in here it is that simple i'm going to do the same on my interact object so class settings interfaces add and we do this and you do this on every single blueprint you want to be able to interact with so whether it's a character whether it's an object it's an item it's a door it's a car anything like that you put this interface on there i'm going to save that go back to my character blueprint and what we're going to do now is we also want to create an input action so to do that i'm going to go to edit project settings go down to input and delete what i already have and we're going to create an x mapping if you don't have these ones here already don't worry they're just from previous tutorials so i'm gonna hit the plus action mapping naming this one interact i'm gonna set the key to be an e key you set this to be whatever you like for example e f left mouse button anything like that but for me e is gonna work great and i'm gonna close that like so back in our character event graph we're gonna right click and call that interact action event we just made so it's action event interact there zoom in and now whenever we press e it's going to fire off this code and what i'm going to do is have it so we have to be looking at the object to interact with it so very simply i'm going to come off the press of the interact there and get a line trace by channel the start is going to be our camera the end is going to be just in front of the camera so i'm going to get a reference to my first passing camera here out of this i'm going to get world location so it starts where the camera is and that's going to go in the start out the camera again i'm going to get the forward vector so it goes in the forward facing direction of the camera so in front of the player out of that i'm going to get a vector multiplied by a float this float value i'm going to set as 250 and essentially what this is is this is how far in front of the player is going to go so for me it's going to be 250 units in front of the player then i'm going to add that to the world location so you get a vector plus the vector the return value of that going into the end and that's just to keep it going in a straight line so it's going to draw a line from the camera's location to 250 units in front of the camera if i then change draw to the book type to four duration we can hit play and see what this looks like so i hit e you see we then have that red line there so essentially that is how far it's going to go you might want to increase it so if something's on the floor that's not going to reach it you have to be looking quite close but again you just increase this float value here to get it longer decrease the float value to get it shorter that will work perfectly for you however you want to do it i'm going to change four duration back to non on the draw deep type there then after this we want to hold down b and left click to get a branch connect to the condition to the return value the execution there and that just means it's only going to fire off the code after this if this line trace actually hits something then out of the out hit we're going to break hit result open that up there so we can access all the different things which we have from this line choice we only want to use the hit actor to see which blueprint we've actually wanted to interact with and out of the hit actor we're going to get does implement interface so we can see if it implements the interact interface we've just made so we know whether or not we want to interact with it we're going to close that there move this up then hold down b and left click to get another branch that return value is the condition execution going to the true of the first branch we got because again we only want to fire this off if we hit something and we only want to fire off this code if we want to be able to interact with this blueprint the interface in this is obviously going to be our interact interface we just made like so and then if we come at the hit actor again we should be able to then call interact there interact interface interact message like so go into the true of that branch and what this means is whenever this is called it's essentially like a call function so it's going to call the function of interact which we can set up in our object item like so and again it's only going to do it for the blueprint which we want to interact with but this is that part done so this is how we interact with something we're going to see if we hit something when we press e and if we do hit it so we have to be looking at it it's going to see if it wants to be interacted with and if it does it's going to interact with it and again you can see here we have the did interact there like so because that's what we added on again you don't have to have that you might not have it it's just what i put on as an example we can compile and save and we can also close that because that's all we need to do in there now in our object we want to actually interact with we've already implemented the interact interface so what we can do is on the left here you can see we have interfaces interact if we double click on interact you can see we have this here so this is our function for interact so this is again the same thing we had in the interface how we can now edit it because it's specific for this one blueprint so out of interact all i'm going to do is simply get a destroy actor because that's how i'm simply interacting with it what i'm doing is i'm simply just going to destroy the actor so it looks like you picked it up obviously you can do anything you want here for example you can add 10 health to the player or you can take 10 health off the player or add hunger add stamina at a battery level anything along those lines and i do have some videos going along with examples i've just mentioned and if you want specific help for different things let me know in the comments down below i'm also going to take did interact there so we know that it did successfully interact with it i'm going to compile save close that now we can hit play to test this out we hit play press e we see nothing happens but if i look at this object here and hit e it's going to be destroyed as we've just interacted with it so you see we did this with no casting whatsoever so typically in here what you might have is a box collision with begin and end overlap you have to cast the character each time and sometimes you might need to cast the character to see whether or not you want to do something obviously that isn't that great or in our character blueprint we might have to cast to the object we want to interact with so here would be a cast instead again that's not ideal as casting is quite expensive on the system in the long term so this is a lot more efficient okay let me show you that once again the very simple thing we look at it hit e it's going to interact with it with no casting required whatsoever the interact is a lot more efficient so i think that if this video is we've done everything we want to do i've gone over a basic blueprint interface which we have here again it's literally just a read-only function and we've implemented that into different blueprints so we can interact with each one via this interface so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one you
Info
Channel: Matt Aspland
Views: 14,501
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, 3d modelling, games design, graphic designer, ue5, unreal engine 5, 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: m90ZkbtPA9s
Channel Id: undefined
Length: 10min 6sec (606 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.