Stop Casting! use blueprint Interfaces Instead!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's say we have a game with a bunch of interactable objects I've got these cubes which need to do things I've got this door which needs to do something I've got this button over here which needs to do with thing and then I've got this sad excuse for a chest which needs to do with thing all interactable by the player and usually you would probably uh go into your player class and I have a little setup here for doing a line Trace when I press the left Mouse button and you will probably get the break results out of that usually and then you would start uh casting all over the place like casting to our uh door sliding and if that cost fails you would cost to uh the button that I've just made I don't know which one it is but it doesn't matter and anytime you make a new interactable object you're going to need to add a new cost here well that is not great from a workflow perspective because if you've got like 100 200 a thousand items that you need to be able to interact with you're going to need to cast a thousand times and that also comes with a performance hit because anytime you add a casting node into your blueprint that creates a hard reference to that other class so now anytime our BP firstperson character is loaded it automatically also loads in sliding doors into memory and if you've got a thousand items that you're casting to You're Now loading in a thousand classes most of which are even in whatever level you're in at that time very very bad for your memory performance so we don't want to do that what we're going to do instead is we're going to be using a blueprint interface an interface is just a way that one blueprint can say hey other blueprint if you have anything to do with this interface I don't know that you do or you don't do your thing that way things aren't as tightly linked together and they can be a lot more modular and a lot more scalable in a bigger scenario so how do we do that well it's actually quite simple you come up here into your Blueprints and there we can make a blueprint interface I'll call this BPI for blueprint interface uh and let's call it player interact if we open that up we come into this graph which you can see is read only you can't make a function here and that is very important because you're not going to be making any functions in here well you're going to be making the functions you're not going to be programming them in all this is is a list of functions that belong on this interface so when we add this interface into one of our blueprints it's going to have access to all of the functions that we make here on the right hand side the actual code that is inside of those functions is going to be put in those blueprints themselves so I'll show you uh we'll say this is player interacted and we can add another function if you wanted to so from there this works like a normal function we can make inputs and outputs it should be noted that if you don't create an output here so you don't have a return node your interface functions will show up as events in the blueprints that you put them on if you do have an output with a return node here it shows up as a function because events can't have a return noes they don't have an output so if you want an output it's going to turn into a function we don't need an output right now so I'm going to just get rid of that and I think when we compile it it might not get rid of the return node I don't know we're going to figure out whether or not this is going to be a function or an event because of that I've never really added an output and then removed it so I don't know what it's going to do here uh but let's add an input for the uh layer that interacted with it and that's just going to be uh an actor well actually let's just make it a first person character object reference because this is always going to be done by the player because that's what we're making this interface for while this prevents casting and just makes things a little bit easier on you as you're making your script and now let's go into our blueprints here I've got some blueprints up here an exploding Cube the SL doors uh the button and the chest so we want to add this interface to These Blueprints and the way we do that is we can go up here into class settings and here we have a list of interfaces so we can say in our implemented interfaces we can simply just add one and there's a whole bunch of ones that unreal just provides you with which can be very very useful uh but we of course made our BPI player interact then on the left hand side here we'll immediately see we have a new section called interfaces and if we right click this we can implement this as an event and now we can hook this up to the code that I pre-made that will spawn emitter at location and we've got this parameter here for the player so let's say that the player has a coin variable so let's get that coin variable and like just increment whenever we explode one of these things so we can put that after spawning the particles before destroying the acto and we'll do the same thing here in the sliding doors we'll go into the class settings into implementable interfaces and then BPI player interact we suddenly have player interacted we can Implement that event and then we can uh play this timeline which will open up the door now for the button the same thing and I think you get the point we're just going to implement this in all these three things and now all these objects can do what they need to do without the player needing to even know what they are and for this chest I'm going to uh do a little bit more here um because for this chest what we're going to do is we will simply get our coin total and set our new coin total to be the coins that are in this chest plus the coins that the player already had importantly the player doesn't need to be able to read the amount of coins in this chest because the chest will just be able to get the information from the player calculate what a new player total should be and then set that okay so we've got these events implemented in all these different blueprints but now how do we access that from the first person character because it's quite nice that we have the these events here but in order to access those events we're still going to need to cast right because we we can't just access an event on a blueprint without casting that's why interfaces are great because no actually we can if we use this hit actor output which will usually use for casting what we can actually do is player interacted we can send a message to that actor and you see it has an input for the player and what this will do is it will try to run the corresponding function on an interface if the blueprint that we're interacting with has it if it doesn't this just doesn't do anything it doesn't crash it doesn't cause problems but if the thing that we're interacting with does have the interface and does have an implementation for that function on that interface it will just run its own code there so let's connect this up uh get a simple reference to self for this parameter Here and Now if we go through we should be able to see I have got zero coins if I walk up to this I explode that Cube and I have a line trace for the uh for the interacting and my coins increase just as I predicted and then I can open up this door I can go to this button and turn on the light and I can go up to this chest and get the coins out of that chest now the chest will always give five coins because there's nothing actually like interesting programmed in there to do whatever but you get a point right we can make an endless amount of objects without needing to go back into our player character here and add a bunch of casting which makes both for a messy workflow and also bad performance like we could very easily just make a copy of this chest now and this will be a storage chest instead so let's call this store chest it just will work now so let's collect these coins let's open this door um just why not turn on the light again collect some of the coins out of this and now we can go in here and store our coins in there let's go take some more coins out of this chest and then we can put them in there and this just works all without needing to do a single cost and one last thing to really show off is if we make a second function here and I don't need any inputs for this but I do have a output for the coins which is an integer going into my chest blueprint here we have this new function I've called it inspect object and this simply just Returns the coin variable on this chest this way we can use interfaces to make things like variable GS which is just a function that gives you the value of a variable without casting either so if we have a bunch of different kinds of chests which all need to be able to tell the player what the amount of coins in it it is or maybe we don't want this only on chests but maybe we also want this on enemy characters when we right click on them we want to see how much money they have in that pocket or something like that we can make whatever function we want on whatever object we want and it will just return a integer for the coins and then in the player character we can also use the inspect object and this will now have a output so again without needing to cast to the chest we can now access a variable on that chest through our interface output so what I did before using the player here to get the coins and then set the coins on the player again is a pretty good way to do specifically this because we could also be interacting with a bunch of other objects which don't have anything to do with coins but as you can imagine it's also pretty simple at this point to say when we inspect the object uh to set our coin total to be equal to like the thing that we're inspecting plus our current coin total and well that's the wrong note but you get a point right so you can also get variable information and make that as complex as you would like through using interfaces and as always if you want to mess around in this project with these simple example objects and play around a little bit more with how this actually works with the interfaces there's a link Down Below in the description to both a membersonly post here on YouTube and a patreon post where you can download the project files I've been using in this example and a very big thank you to all of my patreons you can see them on screen right now if you want to help out supporting the channel there's a link Down Below in the description to the patreon page and a special thanks to my cave Digger tier patreons Serj Thomas
Info
Channel: The Game Dev Cave
Views: 6,029
Rating: undefined out of 5
Keywords: unreal engine, unreal, unreal casting, casting, blueprint interface, unreal blueprint interface, unreal interface, blueprint, coding, programming
Id: 4sLJOorUI8w
Channel Id: undefined
Length: 11min 55sec (715 seconds)
Published: Sat Dec 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.