Casting Explained | Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you've clicked on this video I'm guessing that you're currently in a project trying to cast to something and you can't figure out what the heck to plug into this object pin or you're just tired of this problem in general and want a better understanding of casting well in either case I suggest you sit back take a break from your project because I'm going to explain casting as thoroughly as I can in this video and hopefully going forward you'll be able to use it with ease so let's answer the question we're all here for what the heck is this object pin why is it there and how do I figure out what to plug into it well before I can answer that question there is another topic you have to have a basic understanding of and that topic is inheritance but don't worry I'm going to keep this simple so whenever you go to your content browser right click and choose new blueprint class as I'm sure you have many times before I want you to notice that at the top of this window it says pick parent class so anytime that you create an actor or Pawn or character whatever it is you're creating a child of that class okay so what I've done real quick is I've created this BP underscore Cube guy you can see right here it's parent classes actor you can also see that in the class settings parent class actor okay so that means that by default this Cube guy has all the stuff that an actor has you see all these variables here and if you don't see these you can go here and tick show inherited variables but not only that we can add our own stuff to it to give it custom functionality now the important thing to note here is that while a cube guy is an actor an actor is not a cube guy if that sounds confusing let me make sense of it real quick notice that I've added this first name variable right and these components as I previously mentioned obviously actor doesn't contain any of those things if I go and I right click create a new blueprint class and then create an actor again it's not going to have any of that which makes sense so the point here to take away is that a cube guy is an actor but an actor is not a cube guy now why is this important and why does this help you understand casting well I'm sure you've heard that this object pin takes in a generic type and converts it to a specific reference to an object in the world when people say a generic type really what this object pin takes in is a parent class of some sort so I've added two of these Cube guys to the world and I've exposed that first name variable I've named the first one Bob and I've named the second one Sally and we're in our first person player character right now and we want to get the name of whichever one we walk up to we want to walk up to it and basically say hey what's your first name now when I was first starting out I remember wondering why do I even need this object reference to begin with I mean I I it it knows that I want a cube guy right here it knows that I'm looking for first name what do I need this object reference for I mean that seems kind of pointless when I'm trying to get an object reference to begin with well pretend that you didn't need this right and I just plugged this in to begin play or something right now what would this return we have two Cube guys in the world and they both have a different first name so what what could this possibly return it it can't return anything we need to specifically reference one of the cube guys in the world now the way that I'm gonna do that in this video is I'm just gonna go and add a box Collision to my first person character all right I'm going to drag it forward a little bit and then in the event graph with it selected I'm going to call its on component begin overlap event okay and I'm going to plug this in here so when the Box collides with something we are going to try and get the first name from our Cube guy now notice that this begin overlap event outputs an actor we can plug this into here compile and this will work that says Bob and that says Sally and I now I want to explain this I want to go back to when we were talking about inheritance what we're getting out of here if I hover over this you can see other actor actor object reference if I try to drag off this and type in first name you're going to see it doesn't have any of that that's kind of the whole point of casting this is just a generic actor and during run time when I collide with one of these two guys this actor value gets set to a BP based Cube guy from there we can plug into the uh cast to BP base Cube guy and it's it sees right away okay this does inherit from actor is it a cube guy if it's not this cast is going to fail so let's go and let's create a new blueprint and let's just call this not a cube guy and we'll go ahead and we'll add in a cone drag or cone in drag it up so this is also an actor but it's not a cube guy so if I walk into this we get Bob Sally and obviously nothing from the cone to really demonstrate this let's go back to our first person character and off of the cast field I'm going to print string cast failed compile we got Bob we got Sally and then cast failed okay so what's happening here is obviously this on begin overlap node can't know ahead of time of all the different children classes of actor that exist in the game I mean they haven't even been created yet this comes with the engine and it can't know that you're gonna have a cube guy and it can't have a pin for every single possible child of actor that would just be ridiculous so all it can really give you is a base actor which isn't going to have any of the stuff your child has and so you're not going to be able to get first name unless you cast that's the whole point of casting is to take this generic object type and get a specific reference to one of its children in the world so another thing that this on component began overlap spits out is a primitive component object type and this does not inherit from actors so if I plug this in here and I compile you're gonna see this warning because right away this cast node sees okay BP Cube guy does not inherit from primitive component and it knows right away that this is always going to fail if I were to take this other component and do get owner this is kind of pointless to do but if I get owner you'll see this prints out an object reference so what will happen is is the Collision will pick up the actual Cube or the eyes and then it'll get the owner actor and then I go like this that'll work so if I hit play and you get Bob you get Sally and then you get cash failed so to drive this home even more I'm gonna confuse you a little bit I'm going to create a variable here and this is going to be of type actor and we're going to make it an object reference and I'll just rename this actor object ref we're also going to create a variable and we're going to call it Cube guy and we're going to make this one of type BP underscore Cube guy because whenever you create a class any class that you create is also its own data type so we have this actor object reference here and if I do set and I pull out a cube guy reference two different data types but remember Cube guy inherits from actor so if I plug this in it's going to let me set this actor object reference I still can't pull out of here and get first name I would still have to cast to BP underscore Cube guy to get that because again an actor does not contain that variable now the important thing here is if I pull out actor object reference and I try to set Cube guy that's not going to work because again an actor is not a cube guy but a cube guy is an actor a parent is not a child but the child is its parent and that's the important thing to take away here now one more thing you might have seen when casting so if I pull out this direct BP Cube guy reference and I plug into this object pin you might think well a cube has a cube guy so this should work and it will work but you'll get this note here and it says Cube guy is already a BP Cube guy you don't need to cast a BP underscore Cube guy basically this pin right here is spitting this out this exact type of data out so there's no need to cast to it if you have this so like if I expose this variable go in here select my first person character I can now if I compile I think yeah you'll now see I have this cue guy variable that I can set and I now have a reference to Bob so if I go in it's going to print out Bob now although that works like I said this cast is unnecessary because this is of type BP underscore Cube guy so I can drag directly off of this to get first name plug that into the print string we can remove this cast for the time being and without using a cast we can just get that first name directly from this variable so if I compile hit play you're going to see the same result it prints out Bob of course doing it this way by using a generic actor class makes it Dynamic so we can walk up to any Cube guy and have it print out the name of that that Cube guy so you might be thinking now okay well this is all fine and dandy but I'm not using an odd component begin overlap and the thing I'm trying to cast to is you know I'm maybe you're trying to cast from your widget blueprint or something like that where you can't even do something like this and all I can really say is it's gonna vary how you get this object reference but if casting is the right way to go there's usually a decently straightforward way to get a valid object reference and if you find yourself doing something overly convoluted casting might not be the right way to handle the problem you're trying to solve you might want to look into blueprint interfaces speaking of which something to watch out for that I've seen done a lot when people are just starting out I myself have done this too let's say for example you were trying to create some sort of interaction system well first off I wouldn't use a box overlap for that but that's besides the point let's say you wanted to be able to walk up to a cube guy and have it print his first name and then if you walk up to a door the door will open and if you walk up to like a collectible or a gun it picks the gun up you might be thinking to yourself oh man so what I'm gonna have to do is I'm gonna have to cast the cube guy and then if this cast fails I'm gonna have to cast to the door and then if that cast fails and the answer is no don't do that if you find yourself nesting casts I can guarantee you there's a better way in the example that I just gave that way would definitely be blueprint interfaces I've created another video on blueprint interfaces so there should be a card popping up right now for that if you're unfamiliar with them but if you find yourself nesting cast I definitely recommend you check that out or just learn about blueprint interfaces in general because they will save you so much trouble but anyways that's the end of this video if you have any questions please leave them in the comments down below if you like this video please consider slapping a like on it and for more content from me consider subscribing thanks so much for watching
Info
Channel: Tyler Serino
Views: 17,683
Rating: undefined out of 5
Keywords: Unreal, Engine, Game Development, Game Dev, Casting, UE4, UE5, Unreal Engine 5, Blueprints, Tutorial, Game Design, Make Games, Coding, Scripting, Blueprint Fundementals, unreal engine, unreal engine 5, unreal engine 5.2
Id: DBIlsxj5quA
Channel Id: undefined
Length: 11min 14sec (674 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.