How To Use Cast Nodes And Why You Actually Want To Use Interfaces Instead - UE4/UE5 Blueprints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Another amazing video!

๐Ÿ‘๏ธŽ︎ 4 ๐Ÿ‘ค๏ธŽ︎ u/DarthJandis ๐Ÿ“…๏ธŽ︎ Sep 01 2021 ๐Ÿ—ซ︎ replies

Its so confusing for beginner as myself, started ue few months ago and saw tutorials inly on casting.

๐Ÿ‘๏ธŽ︎ 3 ๐Ÿ‘ค๏ธŽ︎ u/ReplyisFutile ๐Ÿ“…๏ธŽ︎ Sep 01 2021 ๐Ÿ—ซ︎ replies

Great video and believed this for some time - Casting, Interfaces, Event Dispatchers and when/how you should use them is one of the most valuable things you can learn early on. While casting can be a great starting point for understanding inter-blueprint communication, it's also a terrible crutch long term and easily misused.

That said I also see a lot of people misusing Interfaces as well, not fully understanding why blind communication is used in the first place. Passing actor refs via a interface is generally a bad idea, but seems to be a common rookie mistake. Whenever you cast to something, a copy of it is added into memory, along with any parent BPs and any other BP that they cast to, along with their parent BPs, on and on. Passing it via an interface means a copy is still loaded into memory/attached to the actor, at that point you might as well be casting. This reference cascade can potentially make a game unplayable due to crazy memory requirements.

There's also wonderful synergies that come with Get Actor/Component With Tag nodes and gameplay tags. I use this a lot for inter-blueprint communication, where you aren't using collisions to get an actor ref.

Look forward to your future videos, you're picking some good topics to cover.

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/FjorgVanDerPlorg ๐Ÿ“…๏ธŽ︎ Sep 02 2021 ๐Ÿ—ซ︎ replies

I am very lazzy and always use multicast

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/DomusLudus ๐Ÿ“…๏ธŽ︎ Sep 01 2021 ๐Ÿ—ซ︎ replies

Thanks

๐Ÿ‘๏ธŽ︎ 1 ๐Ÿ‘ค๏ธŽ︎ u/BULLSEYElITe ๐Ÿ“…๏ธŽ︎ Sep 01 2021 ๐Ÿ—ซ︎ replies

Amazing. I discovered recently how to properly use the interfaces (I'm a random amateur). This will help many people.

๐Ÿ‘๏ธŽ︎ 1 ๐Ÿ‘ค๏ธŽ︎ u/Sacenpapier1988 ๐Ÿ“…๏ธŽ︎ Sep 01 2021 ๐Ÿ—ซ︎ replies

Whatโ€™s your take on using interfaces vs event dispatchers?

๐Ÿ‘๏ธŽ︎ 1 ๐Ÿ‘ค๏ธŽ︎ u/89bottles ๐Ÿ“…๏ธŽ︎ Sep 02 2021 ๐Ÿ—ซ︎ replies
Captions
hey everyone welcome back to another video so this one's gonna be slightly different um in the last video i uploaded how to create components so you can create reusable code and that kind of thing and um a lot of people on reddit were really happy with that showing the different ways that you can code in unreal and how to efficiently do it on a modular level but one thing i wanted to cover which doesn't seem to be done enough is how to use cast nodes essentially the catch though with this tutorial is i'm going to show you how to use cast nodes how they work and how to think about them but then also why you shouldn't use them and how you should use something else to kind of get it going and work a different way it's a bit of a mess but casting is one of those things that we use for blueprint communication all the time but there are different ways of doing it that don't restrict your code or what you're able to do with it so first thing i'm going to do is create a collision cube which our player can run into and then we're going to use that to talk to other stuff so let's jump into our blueprints folder let's right-click we'll do blueprint class and let's call this actor bp underscore collision zone we can then load that up and we can see here that we can add our components and stuff if you've been using unreal for a little while you you know all of this so we're just going to collision see we've got a box collision let's move it up so it's on the center and i'm going to make this a little bit bigger so let's do 60 by 60 by 60 and i'm also going to make sure if you guys can see it let's move this along just change the video that we actually have visible ticked and hidden in game deselected so what that means is if we jump into our level now we'll actually be able to see this in the scene but before we do that i want to go to our event graph i want to make sure we've got our components selected so our collision and then on the right here we want to do on begin overlap and we're going to use this first thing we'll do we'll drag off and we'll do an equal and then we're going to do get on or get player pawn now we'll do a branch plug that in now we're just going to hook up a print string what this allows us to do is once we overlap our box cube or the box is going to check to see if it is our player which is equal to if it is we're going to go to our true channel and then fire our print string and we'll use this line of code later on to do our casting hit compile and save and now if we drag this into our level we'll do what everybody else does and put it at the stairs now we can press play if we run up upstairs you can see that we overlap our cube and in the top left there it says hello exactly what we want now we actually need something to fire or at least cast two and in this case i'm gonna do a little light that we're gonna turn on and off once we collide with our box i'm doing a light because we can duplicate it more and we'll be able to see how this works so we're going to right click blueprint class actor and then bp underscore light and with this open we're going to add a s point light component yeah let's just do a point light and we'll keep this as it is it should be pretty good we'll check our intensity and stuff in the level because we might actually have to go brighter no that's all right so now we can go back into our light and we start setting up our code so our light is on by default we know that because we can see it in the scene so the first thing we do is turn that off and to do that we need an event begin play we don't want that event event act to begin overlap we're not colliding with the light itself so we don't need that in event tick we're not firing every frame so we don't need that so in this case what we're going to do is we're going to right click and we're going to create a custom event and we're going to call this turn light on what we can do now is drag in a point light reference so a reference to our component and then we can drag off and we can search set visibility and we can use this to turn our light off so new visibility is disabled the thing here is that if we press play and we overlap our component nothing's going to happen apart from a csl hello we're not looking at our light for anything at all this is where casting can come in to be to be helpful so if we go to our collision first thing we're going to do is remove our print string and then we're actually going to use our cast nodes so we're going to drag off true then we're going to cast to our bp light and you can see here if we drag off our blue pin we can fire our event that we have created so turn light on let's try that off turn light on now if we hit compile you see it doesn't work the thing is we need an object and this object wildcard is probably the most confusing thing about cast nodes especially those beginning or just starting out and the idea is what do you use as your object well card that's pretty much the only description that we get this when i got started was probably the most confusing thing about blueprints that i just couldn't wrap my head around until i actually sat down and thought about how these work and pretty much an object wildcard is if you're writing a letter and you want to send something to one this is going to be your address or your mobile number that you send this information to get an object we need we need to create a reference that we can point to and in this case you might be tempted to get our bp light but if we do that let's search let's do this bp underscore light now you see i've already got it here so you just do bp underscore light and we drag it in and we're saying okay we want this cast to go to that light but if we connect it up and then press compile you'll see that we get something called the note this is basically saying bp light is already a bp light we don't need a cast to bp light this is really confusing so what we actually need to do is convert this to an actor so search actor we go all the way down and we do object reference change variable and then if we hit compile you'll see that it actually goes away because what we're doing is we're casting we're getting our light actor which that's what it actually is in the scene is an actor not a light and then we're casting to that to say turn the light on so we're going to make this public and that means in the third person level i can select our collision box and on the right hand side here we can actually select our light blueprint so if we now run up our stairs you see once we collide with the the box we're casting the information to it and we're saying turn off that light we're basically firing this variable so we actually have this but now the question is what if we want this collision box to do more let's say we have our player once we overlap that we want to change the color of our body to something else the reason i'm choosing the player is because i want to show you how to cast to the player itself so we're going to do third person example project we will open up our first third person character and go over to the event graph and in here we're going to create another event called uh so do custom event and then set color now we're going to get a reference to our mesh and we're just going to set material to change that and if we have a look at our mesh we have two materials element zero which is element zero and then we're gonna connect that up and we'll select a random material that we have in the level let's pick something that's going to look good gradient map let's see what happens with that so now we have our set color we're going to go back to our collision box and on here we need to decide whether we want to change our player character and our light or we just want to change the player character and thing is with a cast as long as this has an information and it's cast into the correct place it'll fire so in this case we're going to fire from our cast so once we change our light as long as it's got a reference we're going to go through and then we'll change our player's color essentially so we're going to cast two it will be our third person third person character and then we can drag off we can do set color which is the name of our custom event and you might notice that we've got a object wildcard it's exactly the same if we plug in an actor though so let's do a new actor and we'll just drag it in there you'll see that it works but we need a reference well we don't want to select the player because we're using the game mode so the player spawns at start so what we can do in this case is we can actually search for our object directly and in this case our player so third person character is parent class character and because we know we've only got one in the level we can drag off and we can search get player character and then we can cast to it so we can hit compile and save and now we press play and run up the stairs basis it turns light off and it changes the color of our character but you might notice that we're adding casts for every single thing that we want to do this is not going to be very useful if we have more than one thing so i'm going to delete that random event and one thing i want to show you as well is let's say we have our third person character but we don't want our light to turn off if we jump back into our blueprints or our scene we can select our object and let's say we remove our light our bp light we'll just set this to clear and then we jump in don't see any difference but if we run upstairs you'll see that now nothing happens to either of them which is not what we want we want our character to still change color and if we leave surprises didn't come up with any errors actually then we can go to our collision and you'll see that our code's still exactly the same but this time because we don't have a reference our cast's failing so what this essentially means is casting is perfect for sending information to one object if you know there's something very specific you need to send information to or change or update in that information then a cast node can be excellent for that best way to do it is probably actually convert one of these to a convert to purecast and what that means is it removes the event and you're then positive that no matter what happens this cast will always have the correct variable or the correct object going into it and it's never going to change so that's what you kind of need to think about personally i think if i can use a com if i can use a pure cast then it's solid then you don't need to worry about it and it's great for casting to the player to change information but what happens if we want to actually do this let's say we want to control what changes color and what goes where so we're actually going to use something called an interface uh it's just a little bit easier to work with and i think covering cast i want to show you the difference between the two so remember we pretty much do we're locked to each one of these so cast we've got to have a specific cast for a specific thing we're now going to delete these and the reference we'll be using them in a little bit and we're going to go to our third person example and we're going to create something called an interface so we've got blueprints you'll see here we have a blueprint interface let's call this the bpi underscore uh let's think of like a global name that we can use for everything let's say activate so if a blueprint says activate and the reference is there we're going to turn it on or off so we jump in here and we can go over our functions i have already done a video on creating interfaces in a little bit more depth so if you want that check the link in the corner and in the description for that video as we go over a little bit more with line traces and how to do more communication a little bit of a puzzle style thing so functions this is better to think of as an event think of this as what you're going to fire so event it's not going to be an event this is going to be called our activate so once this is fired we're going to say do something doesn't matter what it is click compile and now in our third person character we're going to replace our set color with our activate node and before we do that we want to go to class settings and on the right here we want to add our new interface so bpi underscore activate or active and what that does is it actually gives us our interface and you can see here we've got our stuff but what we can do is we can compile we want to right click and remember i said think of these as an event so we're going to search event and then activate and you can see here we have our event activate and the target is bpi activate our interface you get this little little message or blueprint icon on the top right that's when you know you've got the right one so we've got our activate event hit compile and save and then we want to do the same thing with our light so class defaults or class settings add interface bpi underscore activate and we want to make sure we hit compile and then we can delete our turn line we can search activate and we want the event we're going to plug that in here and now we hit compile and save we can go back to our collision zone and if we press play actually in the scene you'll see that nothing happens because we're not firing that event but what we can do here is we can get a reference to those actors in the level and it doesn't matter what order we put them in and you see here we've still got our actor blueprint what i'm going to do is i'm just going to rename this to actors to activate so you'll see here that we've got one blueprint i'm actually going to convert this to an array and what that will do is allow us to select multiple actors in the level i can then drag off a reference to it we'll do a four each yep we'll do a for each loop and we'll plug that into true and what we can do now is we can get this array element and we can search for our activate so if we drag off search activate and you see here we've got activate message that's exactly what we want an interface is essentially sending out a mass text if you'd like and if there is a reference to it it'll pick it up it's the easiest way to decide like how to do it so we've got our actors and now what we can do is we can select our stuff in our scene and you see now if we press compile and save we get our actors and we've got the option to actually add stuff but because we made this public we can choose it from inside of our level so in this case our player isn't spawned otherwise we could add that to our list at runtime and then what we can do is we can actually point this to both of those actors so you can see we've got actors to activate let's add two because we've got two in the level we have our light and we have our player and now we press play we can run up the stairs and you see that it does exactly what we had with the cast except it's a little bit more efficient now rather than having rather than adding another cast node which we would have had to do we can drag this around we can have a load of these so we've got another three let's pick our blueprint let's add another three doesn't matter what order we've got in and then we can run up and turn it on or off and this is where the power of interfaces come in handy compared to or compared to casting so casting is fantastic if you have one actor you specifically want to set data for if you have multiple or if you've got a chain of stuff then interfaces are the better way to do it it's basically a way to control stuff and what this also means is we have this actor here we could then drag it around to this side and if we have this one over here we can use this to control separate things so let's clear this list of arrays we can set that and we can press play and then if we run up we'll turn these off and then if we hit this one we'll turn this off we could go ahead and we could create another blueprint so actor bp underscore cube let's change the color of a cube we go to our event graph actually first we need to add a cube which would be very useful cube event graph and then let's say we have our activate again so we go to class settings add vpi underscore interface or activate we make sure we compile first otherwise we will find our event then we do event activate and now we can tell this actor to do something the exact same way otherwise we would have had to add another cast node casting to the bp cube so that would have been three cast nodes with all the wild card references whereas now you don't actually need to do that we can drag in a reference to our cube we could even do on this one a flip-flop so we set material so the first time we overlap it we change our material to red and then the second time we go over it we set our material to let's do orange we can make sure our target is correct and now we just need to drag that into our level and we can hit play so now if we run upstairs we have one collision which changes the color of our character and turns our light off and then this one at the minute we'll only change those lights off because i forgot to set the reference but if we choose our collision we go to our objects select our cube we can then run up and then we can change the color of our cube and then if we run back through it didn't do anything and i realized it didn't do anything because we set it to the same color so if we go back now i've changed it to a white we're going to press play and go up and we turn those lights off then this one we turn to red and then we go back through it it goes back to white and the best part of this we don't need to cast two multiple different things so if we want both cubes or both collisions to activate this we can select that one we can add it to our list of actors and now when we run up the stairs it turns red and when we run through this second one it turns to white and then back and forth so a way to think about this is let's say you have a level with a room the doors open and you want specific things to happen in a specific order you can say the doors open the lights turn on after the lights turn on we activate the enemies through a delay as long as they're pointed at the correct thing and then you can have the player start or you could play a cinematic and then that's the kind of thing that you can do with this it's it's quite powerful so that's basically how to use casting and how it's stuck to basically one object well that's the better way to think about it but there are alternatives out there to make your code easier to use and cleaner and this is the same thing we can just i could just keep going all day and duplicate these and have this cube added in specifically control that one and then this one to control it as well and go through like so this one's went on a little bit longer than i'd like to but i just wanted to show you how to use blueprint casting and how there is actually an alternative out there which most people don't know about to using it i think this is probably going to be very helpful to those just getting started and i'd like to leave you with the thought of in your head think about what you don't need to cast to like if you if you're casting to something can it be done with an interface instead literally like if you're casting to an actor or if you're going to cast two multiple then interfaces all the way like just in the faces of life and love just just use them when you need to in replace of a cast node so i hope this helped a big shout out to everyone on my patreon right now all right awesome stuff and everyone in the discord as well we're well above 800 subscribers so if you need any help with anything then head on over there there's a link in the description and yeah i'd love to help you out just drop me a message be awesome so stay safe and i'll see you next time bye you
Info
Channel: GDXR
Views: 33,293
Rating: undefined out of 5
Keywords: Gamedev, Indie, Indie Gamedev, Indie game Devlog, Game Devlog, Game, Dev, XR, GameDevXR, Blueprints, Coding, Unreal, Engine, VirtualReality, VR, ExtendedReality, tutorial, how to, how, To, develop games, making a game, Dev Log, beginer, Unreal Engine 4, archviz, architecture tutorial, beginner tutorial, basics, blueprint tutorial, blender, beginner tutorial series, coding, devlog, Jonathan, Bardwell, code, unreal engine 5, casting, blueprints, interfaces, vs, UE5, UE4, Interfaces, Are, better, Fortinte
Id: mV2uFh-H5ic
Channel Id: undefined
Length: 23min 36sec (1416 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.