How To Interact | Blueprint Interface - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another version 4 tutorial in today's video i'll be going over how to interact with something or how to open a door how to pick something up whatever you want to use the interaction for and we're going to be doing it nice and efficiently by using blueprint interfaces so this is a good video if you want to learn how to interact and also a good video if you want to understand how blueprint interfaces work so let me hit play and show you what i made today so in this very basic example what i'm doing is pressing e to interact and interact is going to open this door so i just press e here nothing's happening but if i go up to it press e the door is going to open i can then obviously walk through and i'm also going to be showing you in this how to do it just based on if you are close enough to an object you can interact with it and i'm also going to show you how to interact with something when you are only looking at it so again this video is going to be going over how to interact using blueprint interfaces so without further ado let me delete this code and i'll show you how i've done it so the first thing we're going to do is we obviously want to create our blueprint interface so to do that we're going to right click go to blueprints and then blueprint interface like so and you can name this whatever you like but this is a blueprint interface which for me is going to be doing the interacting so i'm going to name it interact interface as that makes no sense for me and i know i'll be able to easily find it under that name so i'm going to open it up straight away and you'll see this is a read-only document so we can't actually do any code in here all this does is it just allows it to be read so you can only take what's already in here not add code into it so this is a function which i'm going to name interact as that is what that function is going to do now you see in the bottom right as well we can also add inputs and outputs if you want i don't necessarily need to but i'm going to anyway just to show you so let's add an input by pressing the new parameter button there and this is going to be interactor which i believe a spot like that and the variable for this is going to be a character reference as for me the only things which can interact in my game is characters i.e the player and i'm also going to add an output and this one is quite useful and this is going to be success or did interact or anything like that and this is going to be a boolean so the point of these inputs and outputs i've added in is so when we do interact we can see which player is currently interacting with it so if you have a multiplayer game that's where that might be quite useful and the success output is so that you know if it did actually interact so in my example i'm doing a door we will now be able to know if the door did actually open or close or if something went wrong in the code so you should have something which looks a little bit like this again it's read-only so the start of the function and the end of the function or if you haven't added an output you'll just have to start so again it should be like this and we can close that that is all we need to do in there nice and simple and so now i'm going to open up my character blueprint or whatever it is for you that you want to be able to do the interacting so for me is the player which will be interacting with different items so for me that's content third person bp blueprints third-party character now what i'm going to do in here is i'm also going to create an action mapping so the player presses this button to do the interacting so i'm going to go to edit project settings once it loads we're going to go down to input on the bottom left here in here we're gonna hit the plus action mapping and name this whatever we want so i'm gonna name it interact and i'm gonna set this to be the e key now with this you can set multiple keys so e or f you can also set it for different platforms so pc xbox playstation whatever you like and you can also set up key bindings so once you've done that we can close this right click in our event graph and search for that action mapping which i named mine interact there under action event like so so whenever the player presses the button which you set up for this this is going to be fired off now what we're going to do is when we press the interact button it's going to interact with anything that we are currently close to so to do this we can right click and get overlapping actors with the class filter just searching for actor so that's now going to get all the actors that the player is currently overlapping and interact with them one by one to figure out which one to interact with we're going to drag out of overlapping actors and you'll see that is an array so it's going to get multiple actors all the ones we're currently overlapping and we're going to go into a for each loop with break the reason we're doing the break is that when we find one we can interact with we're going to stop the loop because we've already interacted with it we don't need to go through all of them and as the array element we're going to do does implement interface because again we only want to interact with anything which currently has the blueprint interface because that has the code inside of it and we're going to set that up later on the interface for me is obviously the interact interface like so you'll see this is a boolean return value so it's going to be true or false of if it does or doesn't implement the interface so we're going to hold down b left click to get a branch with that as a condition and that going into the loop body of the for each loop with break like so false means it doesn't have the interface so it could then be a wall which we obviously don't want to interact with so we want to go to the next item which might be a gun which we do want to interact with we want to open it up maybe a door would make more sense so you've got the wall don't interact with that then you have the door do interact with that so again that's why we're doing this here true means we are going to interact with it so we're going to come out of array element once again and we're going to do interact or just call the name of the function which you named it in the blueprint interface again minus interact and it should say message in brackets next to it connecting it to true like so and you'll see here we have the input and output so the input of the interactor i'm just going to do get a reference to self and the output of success i'm not going to do anything with but obviously you can do if you want to in the future then also out of this we're going to go into the break of the 40 loop because again we found something to interact with so we can stop searching through the rest of the items which we might be overlapping and that is now that part of the code done we can compile and save that so this is how the player is going to interact with something that they are overlapping and on screen now i will also put an image of the code for interacting something which you are looking at so you have to be looking directly at that object or the item in order to actually interact with it so now let's go on to doing the code for actually doing the interacting so this is going to call the function to interact now we need to actually interact with it and this just saves us from having to do this code in every single blueprint which you might have to do if you're just doing it based on the box collisions and stuff like that blueprint interface is a much easier way of doing it so we'll close this open up the item we want to interact with which for me is just going to be a door and we're going to write the code in here so ignore that that's just from when i restarted it earlier so in here my code is simply a custom event called open door which is going to play an animation for opening a door i do have different videos on this as well if you want it but i've just got this code done already set up for me what we want to do next is go to class settings up at the top go to interfaces and add one under the implemented interfaces and we're going to add interact interface like so and we can compile save and on the left you should see we now have a tab called interfaces and we have all the functions in the underneath that which for me is just interact if we double click we can open it up and you see again we have the input and output so this is basically what it looked like inside of the interface so now we can call that function from inside the interface but it can be different for each and every single item we have so you can have a door there's a certain code and then a gun does another code but you're using the same interface so again it makes it really nice and efficient now in here all i'm going to do is call my custom event called open door and i'm not going to use the interactor because i don't need to and i will tick the success just to show you how you do that and then compile and save now you don't need to do a custom event in here like i have done the only reason i've done that is because i've used a timeline which is a latent node meaning you can't put down a function so i've done it in a custom event like this obviously set this part out however makes the most sense for you and with that done that should be it completely done for us so we'll compile save and close that and hit play to set this out again if i press e here nothing's going to happen if i go up to the door press e you'll see that we have now interrupted with that door and it has opened it up as that is the code which we told it to do so i think that would be it for this video which i don't know if you want to do we've set up so we've created a blueprint interface and which allows us to interact with any item which we've created in my example is a door to open and close it like this when i press the e key so thanks so much for watching i hope you enjoyed and hope you found it helpful and if you did make sure to like subscribe down below so thanks for watching and i'll see in the next one [Music]
Info
Channel: Matt Aspland
Views: 18,194
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, blender, unity, games design, graphic designer, ue5, unreal engine 5, interact, how to interact, open, door, close, pick up, item, blueprint, interface, interfaces, bp, ue4 horror game, pickups, pick up items with e, interact with key, interact with e, interact with items, pick up items, pick up objects, pick up ammo, fps, loot, interactable, how to pick up
Id: u4TBbtq9uwk
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Fri Dec 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.