Health and Damage System | No Casting | Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to this tutorial in this video I will show you how to create Advanced Health System and damage system without casting binding or event thick we will use blueprints interface instead as I previously stated in my other videos you should never use casting in your games to be more clear you can use casting for debugging purposes only or simply to test something real quick yes but if you want to prevent your game from crashing and to improve performance you should not have any casting in your Shi final game the choice is yours okay so let's get started I have this third person character here template I'm going to open the content drawer and I have this new blueprint but let's make another one so new blueprint class actor and I'm going to name this damage uncore heal open it up so in the event graph I'm just going to delete this we don't need them so back to the viewport let's add trigger box so actually it's called box Collision here and we can type damage healor Trier you can make this as a rout if you want so compile and save so with this trigger selected on the right side you can type type collision and for the Collision presets go to custom make it ignore all except for the pawn make it overlap so nothing else can trigger this only our player character which is the pawn and we can type here also hidden so we can see it in the game so uncheck this compile and save another optimization advice never scale the actor this trigger box with this tool here always scale it with a box extent here like this okay never scale the trigger boxes with a scale transform here okay so now let's go to the EV graph scroll all the way down we're going to need begin overlap and N overlap like I said every other video is like this so C to third person character for demonstrational purposes because not everybody has hover over this node just hover over and read what it says this will cause the blueprint to always be loaded which can be expensive so the makers of the engine are telling us that this can be expensive so I'm not inventing this on my own I didn't just dream and woke up and telling you this this coming from the developers of this engine okay from the engineers just to make a note okay back to the tutorial so instead of using this if you cast this is going to happen if you are using casting everything you see here all this code will be loaded into the memory even this the character components and this fire particles so everything all these components all these functions here all these variables like everything that contains this player character but if you're using blueprint interface this node here this node will send a message to exit only this line of code only this and nothing else from the character so logically which way do you think it's a better way for performance loading all of this huge code you see or just this small part over here okay back to our blueprint that was one of my project I'm working on it has complex blueprint I just wanted to show you what's going to happen if you use the casting node again the choice is yours so let's delete this casting what we are going to use instead is to right click and get player character so from here you can type equals and from other actor like this and call a branch so to Cola Branch you can hold down B and left click or right click and type Branch okay so I'm going to delete [Applause] this now without casting we are checking if our player character is overlapping this trigger box but we cannot use any variables from here but we can use blueprint interface to use any functions that we have created in our player character which we're going to do it now so if this is our player character let's type print string just going to type here player overlaps just to make sure this is working now I'm going to throw this actor in the level so I'm to play the game in the top left corner we see the message player overlapped so it's working okay back to our blueprint we can make this as a function because we're going to use for the end overlap so select these three right click collapse to function and type player overlaps we can open this and for the through we can right click and type return note so this function is going to be successful when the true statement is fired and select this overlap and we can type here player we can exit here so we can select this and contrl D so when we exit this box something's going to happen when player character exit this box so from other actor in the player now let's make sure this is still working so I'm going to type here player entered again I'm going to select this contrl D and player exits now when I enter player entered and player exits so enter exit okay now let's go to our third person character so from here I'm going to create a variable it's going to be called health make it as a float type and compile and save so health is going to be 100 and compile and save now before I have created widget blueprint so in the content draw I have widget and I have called this widget blueprint in the player character so when the game starts we are seeing the widget blueprint on our screen and I have promoted these two variable so we can use things from the widget now let's create a function so I'm going to name this function set player health and this set player Health you cannot use it from here from the player character so if you type the function's name so set play layer help it's not coming out but if you cast it will coming out so this is the problem you have to go around the cast now let's go back to our third person character now we are in the health function so drag and drop the health and choose get drag up in and type plus I'll explain everything how it works just follow now so take this pin and input over here so when you select we can just type it here health and we have to set the health so drag and drop set Health like this and now we want to create progress bar so if you open the layer Hut we need here a progress bar so drag and drop progress bar something like this I'm going to name it player help underscore progress bar so percent is going to be one many tutorials out there are telling you to use binding here this one but you should never use this option because this runs on a event tick function so when you create this this will run on event tick meaning if your computer is running on a 100 frames per second 100 FPS this binding will run 100 times in 1 second and that's going to happen throughout your whole game and that is a lot if you want to learn in more details why event tick is bad and you should never use it I have a specific video for that check the link in description okay I'm going to delete this binding now so let's continue with the right setup so the percent bar goes from zero to one but our health is from 0o to 100 now in order this to work properly from this pin you have to type divide or simply the division operator so divide by 100 and because when the game starts I have promoted my widget to variable it's over here player Hut so I'm going to drag and drop here and choose get and now I'm going to have access to all the elements here but to have access they need to be as a variable so when you select this progress bar on the right side you see here it's variable if you drag a text for example it's not variable so we cannot access it but if you make it as a variable then you can access this text and you can manipulate it I'm going to Del this now so again when you select this it's called player Health progress bar so back to our character we are in the set player Health function so drag a inin and type the progress bar name so progress bar get player Health progress bar we need the get not the set and from here drag a in and you can type set percent now compile and save now this set percent is this one over here so you see percent and because this goes from0 to one that's why we are dividing 100 and we are going to put the final input over here so that way we are going to have from zero to 100 instead of 0 and one so we have to make this mathematical operation to work this properly and connect over here I'm going to make a reer note now if you go to the event graph I'm going to type right click so keyboard I'm going to keep this one so I'm going to make a copy of it so this is going to be two now I'm I'm going to drag and drop the function and going to duplicate this means we we are setting the health but we are not just setting it but we are updating it to positive or negative value because we have this here plus and we have inputed in this here it means that you can type here minus 10 for example and here 10 so this means every time when I press one I'm going to take minus 10 from my health when I press two I'm going to add 10 to my health because this is positive number this is negative that's how we are manipulating this operator so with this plus you can make it also minus actually so let's go back now let's see if it's working now when I press one it should take minus 10 but if I keep on pressing now when I come to zero if I keep on pressing it's going to go into minus now when I press two I'm actually adding Health but you're not seeing because it's minus now when I go over for example 100 if I keep on pressing it's going 150 200 and so on and if I press one I have to see that until comes to one so let's fix this problem let's go back to our third person function there's a simple solution for this just put this here on the side so the final result of this minus or plus Health we want to type here clamp so we have here clamp float and the final result is going to go in the health we want minimum to be zero and maximum to be 100 so with this setup when you press one it will never go under zero and if you press two or if you heal the character it will never go over 100 so let's make a simple test I'm going to right click and type return note so you can either get the variable and put it like this or you can use this here and put it over here let me delete it just to show you so from this here in the return value and look what it says retre the value of the variable can use instead of separate get node so you can use instead of this this is the get note and me personally I don't want to clutter the blueprints with nodes like I don't want things to be here the less you have the better so if you want to see this more clearly you can double click and make a rear out not notes like this okay now from here you can select this output you can type here help return come to the event graph now when you use this function let's say we want to damage the player if you come to zero so equals and call a branch holding B and left click if it comes to zero here we can kill the player so I'm going to just type print string so player that from false we don't need to do anything and from the healing let's say when we heal the player if it's equal to 100 we can also call a branch and we can simply copy and paste this health is full now let's test it out now going to press one I'm damaging the player player de so every time when I press it's going to be zero and when I press 21 I'm straight healing the player because it doesn't go less than zero doesn't go into minus so when I go plus he is full keep on pressing two it's not going to go over 100 now let's see how we can use this function to damage our player it's actually very simple so I'm going to leave this here just for the debugging purposes so we have made one function with two different functionalities we can either heal or damage the player now let's open our content drawer go to content I have this this folder main I will make another folder called interfaces I like to color coding this so open the folder right click go to blueprints interface I'm going to call this player uncore interface open it up now for this we're going to need to create one function we are going to call damage heal player and we need only input of float type this health update so compile and save now we have created interface with a name player uncore interface now let's go to the player character and go to class settings and here we can add that interface so type the name player uncore interface compile and save nothing happens now if you go on the left side we have this yellow icon double click and we have event damage heal player now we can get this function that we've created and we can manipulate this this function through this interface this means every time when you are calling this interface whatever is going to be here like a blueprint scode is going to be executed so this health update float it's coming from this input here just as a reminder okay we don't need this anymore so back to our player character so connect this two this is all you have to do drag a pin from here and type equal color bench so when we damage the player when we come to zero Health we're going to kill the player so I'm going to copy this so player is dead if not but if the paer still has Health nothing's going to happen so go to damage heal actor so this is the actor can damage or heal us from here the right click get player character now because this interface lives in our player character we have implemented it here in the class settings now we can call that fun function so we have to call this name so damage heal player so from here damage heal player so we have damage heal player message and now we are having access to the health update and health update comes from this one and it's automatically connected with our function in that way we are calling actually everything over here with simply calling this one note now let's say you want to damage the player right so he update if you want to damage the player type minus 10 now we are having this actor here in the world now when I play the game you d the player now when I come to zero we're going to see player is dead so the function works properly now we have this another PR string because of the exiting of the box it's this one I'm going to disconnect and if you want to heal the player let's say for 20 you can heal the player but because I I have full health nothing's going to happen so I'm going to press one to damage my player from before and now this box will heal us for 20 now if you want to make this for example this to heal and another actor to damage you simply duplicate it so we have one actor two instances let's go back to the damage heal we can promote this to variable so drag a pin from here and promote the variable and type damage heal it's very important to expose this variable so click here now this is going to be connected with the whole interface that comes from the player character from here and and this variable is separate so it's not going to impact anything of of here it's going to work only for this blueprint so let's say damage here is going to be like 10 so this is the default value what is the good thing here with the exposing if you go back to the level so when you select it we have here damage heal the default value is 10 but let's say I want this to be a fire and to damage me so I'm going to type minus 20 for example and this one is going to be 10 so that way this is going to damage us for 20 this is going to heal us and this will kill us if you come to zero in the future videos I'm going to make a death function so when the player comes to zero we are going to kill the player and let's go back to the damage heal I you see we don't have any casting any hard references and when you go to the damage heal actor right click we have 73 kilobytes and in the memory 19 kilobytes so this is practically nothing in the game okay I'm going to just delete this and if you want to put here for example a fire so select this instance actually let's go to the blueprint let's make a another component so type particle so particle system we can type damage hcore particle compile and save leave it here blank so when we go to the level select damage he component here so whatever you change here it's not going to impact this instance or the original actor it's going to impact only what is this instance so select this particle and let's type fire I want to use the default fire and I'm going to select this select the particle type healing you can use your own assets for Marketplace since this is too big let make it a bit smaller now when I go in the game we have fire the damages and heal us now let's kill the player one more time and that's it also in the future videos I'm going to create how to make damage overtime so when you're standing in the Box the player is going to be damaged over time and also healing over time okay I hope this video clears things up if you have any questions Dr them down in the comment thanks for watching I will see you in the next one
Info
Channel: Chankulovski
Views: 1,281
Rating: undefined out of 5
Keywords: unrealengine, blueprints, gamedev, tutorial, gamedesign, programming
Id: DmpRbWwJCzY
Channel Id: undefined
Length: 27min 52sec (1672 seconds)
Published: Sat May 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.