Learn why you should NEVER use Casting in Unreal Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back in this lesson we will reveal why casting note is bad and why you should never use it every tutorial for Unreal Engine out there is with casting but nobody is saying how bad the casting is and why you should never use it in this video we will learn how to check the size of our files and how casting is increasing the size and slowing down your game we will learn which blueprints can be used instead of casting and we will learn how to use collisions properly and without casting okay so let's begin programming open your content drawer I have created a folder called main this one and I moved the third person character into this folder simply for organizational purposes if you want to follow along exactly the same like me you can right click create a new folder and you can name it underscore main everything I'm going to do in this course will be placed in this folder here so let's go to content enter the main folder and I will create another folder right click I will name this folder actors and I will place all the actors in This folder now let's create new blueprint actor right click blueprint class let's go actor we're going to name this actor damage uncore heal save the blueprint the reason why we are naming the actor damage and heal in one is because we will use the same actor to damage or heal the player with just a few clicks which I will show you later on in the course so this actor can be used to damage the player or heal the player they're going to be placed in the level and it's going to work independently so for example we can throw this actor here we can duplicate and this one's going to damage the player and this one's going to heal the player but for this later on in the course I will just delete the sectors let's go to content drawer let's open the actor let's add static mesh just type sphere we're going to choose static mer sphere and let's add sphere Collision again type sphere and let's go sphere Collision let's name the sphere collision damage heal underscore Collision so the moment the sphere Collision is hidden inside this static mesh in order to make it larger you should never use this scale tool here so let's scale this for example five we have scale the sphere the sphere Collision instead we should use here sphere radius so let's make this default so select the sphere and here let's make it 55 let's make it 100 we make it bigger for optimization purposes you should never scale trigger boxes or Collision boxes that the same you should never scale them with this tool you should scale them here you might ask yourself but why there are two scaling tools here one and here another one well for optimization purposes you should use the sphere radius too it's this one here and that's only thing you need to know and don't worry about the rest let's add another component box Collision so these two are the same they are just different shape so let's select the box and let's take it out of the actor so we can see it clearly what's going on once the box is selected you see we have here different scaling tool if you select the radius we have this radius for the sphere but if you select the Box we have X Y and Z again when you have trigger box or Collision box you should never scale them here like this for optimization purposes just remember Collision box and Trigger box never scale with this tool always scale with boxes stand okay now let's delete the Box we're going to stick with the sphere Collision for now okay let's go to the event graph and delete all this nodes for now now select the damage heal Collision this is the sphere and scroll on the right side all the way down and select on component begin overlap for the beginners what this node does is whenever something collide with this Collision box something will happen and I will explain to you in a second now if you want your player to interact with this sphere you've probably seen every tutorial how to use cast node from other actor drag a pin and type cast to third person character what this essentially do is that when your player touches the sphere something will happen and if you want to use information from our player such as help variable so we can damage our player for example we will drag a pin from here and we'll type the name of the variable and that's it job's done but here's the thing nobody says how bad this is for your game and you should actually never use it now let's see why is that hover over the note and read the note it says this will cause the blueprint to always be loaded which can be expensive let me show you what this means so now with this actor we have casting now right now let's go to our third person map open the content drawer right click on the actor and click size map what we see here is the current size of our blueprint don't worry about all these squares just focus on the top here on this one because that's the total memory we need to see so see this is the name of the actor damage heal and it says 200 mbes on the dis size in the top right corner if you select the drop down we have here memory size so this actor takes memory 110 megabytes which is a lot and please remember there is nothing in this actor so here we have absolutely nothing just a sphere and sphere Collision so this shouldn't be 200 megabytes let's go to our size map again this is because we are casting to our third person character and everything that character has such as material textures blueprints all of these will be loaded every time when casting so this is all because of the casting of the third person character simply by calling this note so even though there is nothing in our actor we have just created a sphere Collision in a static mesh our actor uses all the memory from the third person character just because of calling this note now let's close the size map tab going close it go to our content drawer let's go content main right click on our third person character and select size map what do we see here exactly the same memory which was in this actor but this is the third person character so this third person character loaded everything into this actor because of Simply calling the note that's why this note is very very bad because imagine if your third person character is maybe 5 GB or or 10 gab imagine high quality of assets inside and this damage heal this simple small actor is going to be the same size like the third person person character simply with calling this not and imagine if you have hundreds of factors with casting nodes how much they're going to be expensive so if this is 300 megabytes imagine if you have 20 for example blueprints with simple nodes like this they're going to be a lot of memory simply because of casting that's why again you should never cast now let's close the size map again now let's delete the casting compile and save the blueprint now let's check the file size again without this casting note let's go to content drawer actors right click size map do you see the difference this actor is practically nothing in the game considering it's just a few kilobytes and size so it's 300 if you go to memory 800 kilobytes close the size map and let's see another example of the casting now let's right click again on the empty space and type again cast to third person character this time we're not going to connect with anything like this we are going to Simply put on the side so even though this node is simply standing there it's not even connected it will still load everything from the character into the memory let's make sure this is correct we're going to check the size map again you see the damage heal actor is still loaded and this not is not even do anything so there is a simple solution for this how we can interact our player character with this sphere let's delete this casting now and from other actor drag a pin and type equals click on the equal operator now right click and type get player character connect this return value with the select asset what we do with this is we are checking whether some other actor is overlapping with this sphere and if that other actor is our player character since we have a Boolean return value here we must call Branch to check whether this is true or false you can either use a shortcut by holding B and then click or simply right click and type Branch FL control Branch okay so connect this bullion with the condition of this branch and connect the execution pin so instead of using casting and kill the performance of our game we are simply checking if this sphere is overlapping with our player by using using the equals operator now let's see if this really works right click and type print string we're going to put it over here now let's duplicate this print string by selecting and hold control and D to make a copy of it let's type in this print string player character over left we're going to type in this one player character not overlapped now if this condition is true if our player character overlaps with this sphere from through we're going to Simply call this note on the screen we're going to print player character overlapped if something else overlap this sphere we're going to print this message on the screen player character not overlapped let's compile and save now let's check it out open the content draer and drag and drop the blueprint in the world now let's collide with the sphere to see what will happen have a look in the top left corner to see the printed message on the screen player character overlapped let's try again so it's working you see you don't need casting to overlap this sphere now we see that this works with the player character now let's see how we can check if it's working if something else is colliding with this sphere now come up to this plus sign here let's go to shapes and this sphere here drag and drop let's put it a little bit on top so we are going to make this sphere to fall down if you play the game nothing will happen because we need to simulate physics to this sphere now exit the game select the sphere on the right side type simulate check this box simulate physics now let's play the game and see we still didn't get the message on the screen that something else has overlapped this sphere this is because we need to enable overlapping events to this static mesh exit the game with the mesh selected we can type here generate overlap events so there are two options we need to check this one so that way we are telling the engine that this sphere is going to generate overlap events and now when we play the game pay attention to the top left corner to see the printed message on the screen player character not overlaps that means something else overlapped with this other sphere now let's overlap our player character to make sure both conditions are working so you see if something else overlap this sphere something will happen and if our player character overlaps again it's going to trigger the event now again it's going to happen because this ball is coming back you see boom you see we don't need to use casting to interact with other objects let's open the actor one more time to clarify one more thing if you want to use information from our character such as health variable to damage the player you cannot drag a inin from here and type the variable name instead we need to use blueprint interface which we will cover in the following lessons in this lesson we learn how to avoid the casting so in the following lessons we are going to learn how to create and Implement blueprint interface I hope this video was educational I will see you in the next one
Info
Channel: Chankulovski
Views: 4,717
Rating: undefined out of 5
Keywords: unrealengine, unreal engine tutorials, blueprints tutorial, game development tutorials
Id: iVhEL-sWaC8
Channel Id: undefined
Length: 16min 21sec (981 seconds)
Published: Mon May 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.