Unreal Engine C++ Tutorial - HUD interfaces

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another episode of unreal C++ and this one we're gonna be covering HUDs and I've told everyone I'm gonna be doing hugs for quite a while now and I didn't really get around to it so I'm finally doing HUDs and I have had so many requests in the comments section please do hugs please do HUDs Hudson say boss boss and for a while I kind of shied away from the idea of doing it but finally did a bit of research into how to draw HUDs and stuff and so the the way I'm gonna be doing it is exposing my C++ to blueprints making the HUDs blueprints but then accessing my C++ functions and we're just gonna integrate the two really nicely and you'll see how that all works so this is just a basic first person to employed that I've set up here and we're just going to open up the character but first a little bit of groundwork this is stuff that we need to do before we can continue on the first thing to do is just open up your generic edge so whatever you called your project go into this header file and as you can see we've included engine minimal we actually want to include the full engine just go ahead and save that and then we want to open up this dot CSS file and dot C s means c-sharp we don't really work with su sharp at all with the Unreal Engine other than these build files and target files and things like that so if you don't really understand what sushar pairs or the syntax confuses you that's alright because we don't really work with it that much anyway but the syntax is pretty similar to C++ so anyways all you need to do here is just add another dependency module new understanding new IMG and we'll need to add a couple of private dependencies so to add those just copy and paste that and then just change this to private and in here we're just going to have a slight and slight core so slate and slash core there we go and those are our dependencies done so once you've done those two changes that's basically the groundwork the last thing you need to do now is just come into your project here and go to your Unreal Engine project file right click and then go generate visual studio project files so just go ahead and click that it's going to generate all the project files that we need and once it's finished with that we can now continue on with our tutorial you'll get this thing here the project has been modified just go reload all and now we're ready to get started so once you've got all that done we can get into the tutorial the reason we add those dependencies is because basically it doesn't really understand what interfaces are until we actually tell it in the build file to include that if we didn't include that we could still do it but we would have to basically add a bunch of really long things on airsuite as you can see like game framework / input settings it would be like interfaces / u mg / slight call / whatever and it just saves us a lot of a lot of ground work basically I guess who gets there so we need some information to expose to our HUD and I'm going to make it stamina so the players gonna have some stamina and it's going to drain slowly over time and the HUD is just going to show you it draining over time so to do that we're going to have to add some stamina to our character so I'm just gonna add a private section here and we'll make these your properties so your property edit anywhere category equals stamina float currents stamina and then we'll make another one of theirs called initial stamina so this is the amount of stamina the player starts with and then the players current stamina now you might be thinking well I know that private variables cannot be accessed outside of the class so how are our blueprints supposed to access these if they're private and you're right what we need to do is make it what is called an accessor function so we make a public function that gets these private values for us and this is called encapsulation by the way so if you're ever taking some sort of programming quiz or test or whatever put encapsulation down and the teacher will be very impressed anyways let's see here public so we want to make the axis of function public so coming here accessor function for initial stamina I'll just copy that and then we'll say access a function for current stamina these are going to be new functions and we're going to make some blueprint pure which means the blueprints can access this and the category is obviously going to be stamina and then we just say float get initial stamina very easy we can just basically copy this now and then here get currents data so these are the functions that our blueprint is going to use in order to get the initial and current stamina and send that to our heart so that our heart can display this information we're also going to make a function that just updates the stamina it's gonna be very easy so you function blueprint call the ball and case we ever need a call from blueprints category equals stamina voyage update stemmer will put update currents dinner float stem and then we'll just put a nice little handy come in here so we know what this actually does so we'll say updates the Flyers current stamina now we're going to do something really cool here this is a / M so this is just really helpful if what anyone is even using the blueprints this is gonna show up for them it also shows up if you're writing C++ but also on blueprints so since we have this parameter here floats stamina we can actually describe what stamina does to anyone who is using our function really really handy stuff so we'll say at params stamina and then a small description of what stamina actually does so this is the amount to change the player's stamina by it can be either positive or negative and so now anyone calling us function is going to see this come up especially from blueprints that's really handy and you actually bailed us to understand what stem and it does when you're using this function so very handy stuff and also just add a comment here the player's initial stamina the players current stamina and there we go make sure the comment everything goes it's just going to help you out a lot later when you're looking through your code it's going to stop you from getting confused if you everything nicely commented and if you ever give you a code to anyone else to work on they're going to be able to read your comments and they're gonna be able to see what you're actually talking about it's a really good practice to just learn how to do that now in this case we actually want our character to have a tick function and we're gonna use the tech function to drain the players stamina so if we go yeah virtual void tick float Delta time overwrite so this is just saying override the tick function from the base class and then we can go ahead and implement that so implement that was wait for that little green underline to come up and while I'm waiting for that tip and I'll just implement these create definition you know what that's being oh no that's done okay cool so return initial stamina just save that very easy stuff and then quick actions create definition I like to create the definitions this way because it kind of means that you know since Visual Studio is doing it for you you know it's not missing anything up and it's it's gonna get all the definitions and stuff perfect there's no way you can make any typos but it usually doesn't take this long this is pretty weird so I might just start doing it manually so here we're just gonna return cardstone very easy stuff save that implement tick quick actions create definition of tick and there is something we need to do for tech when we implement tech we need to call these super functions tech and I will explain what that means in a second but yeah you just need to get into the habit of doing it whenever you overriding a tech function so in here we just say super tick and basically since we're overriding tech from the superclass we just call these supers tick function as well so close that and then lastly we want to implement this update card stamina function and this will be very easy to implement basically it's going to take stamina and it's going to change the players stamina by whatever value we give it so we're gonna use that inside of the tick function and basically that will let us slowly drain the players stamina and then we will send it to our HUD and our heart will display it on a little progress bar so you'll see the bar slowly drain as the character runs out of stamina so in here I'm just gonna say current stamina equals current stamina boss stamina so very easy stuff let's go into the tick function which is here right in there tick function we need to update the players energy so we're just gonna make it so it's constantly draining so update player also update courage stamina was it in it now a stamina it was stamina right and then we say negative Delta time and delta x just a constant value that is passed to tick and then we're gonna times that by say we'll go 0.01 no it's probably a little slow well go 0.05 if and then we will times that by the initial stamina what this is going to do is it's going to make our stamina drain nice and slowly and I might want to end up changing that value up actually I might make it point zero one okay so oh and also we need to pass Delta time to the super clauses check function quite easy stuff okay and also we created the initial stamina and the current stamina but we didn't actually initialize them so let's initialize those values initial stamina equals 100 point if current stamina equals initial stamina okay now that we've done all that we're almost ready to start putting our interface together so let's just make a new folder call this heart and inside our heart we'll make a blueprints I was sorry where is it user interface widget blueprint and we'll call this player Hut I guess why not so we want to display our stamina to do that I'm going to use a progress bar and we'll just drag us out and whenever I think of stamina I always think of the color green so I'm gonna use a nice green color to represent the players energy or stamina as you can see we have this value here called percent now this is a value ranging from zero to one so if I put one in here you can see it's going to be a full bar if I put 0.5 it's half full so 0.25 is one quarter and yeah you get the idea by default we're gonna make it equal to not feel at all we're going to bind a value to the progress bar and that's going to bind our stamina so our stamina is going to be basically displayed in this bar before I can access all that stuff I'm gonna have to compile all my code and make sure that's working properly so once I know all the codes there I can bind it in the blueprints Jesus any day now oh my god I've never seen C++ code take this long to compile this is ridiculous I hope nothing is wrong because if it says compiled failed that it's going to be very frustrating to free compile all this stuff yeah it seems slower than usual might be oh wow I don't know the project opened maybe that's it huh hopefully that didn't ruin anything this is truly ridiculous I've never seen civil Pasco take this long to compile there we go finally oh my god right let's open the Hut up we got a progress bar there and we'll go ahead and compile at heart that will compile straightaway right so we have our bar and we want to bind the stamina to the bar to do that we're gonna go bind create binding drag it out and if you just hold alt and then click that it's going to get rid of that link so what we need to do is get the player character and we're using the index 0 which is our player drag that out and right now get player character just returns a generic character or sorry just a normal character we want to actually cast it into a generic character which is a character type keep in mind it won't be generic character for you if you used a different project name if your project name was fire for example you would be casting to fly a character so just keep that in mind because we're not working with more than one character type we can just convert this to a pill cast and we know that it's going to succeed because we know we're casting into a generic character always drag this up and get the current stamina as you can see right there now we just return this and what we want to do is divide this by 100 because our stamina is a value ranging from 0 to 100 but you'll remember that our progress bar only takes a value from 0 to 1 and obviously that's not going to work for us so we need to divide that value by 100 and now Damon is going to work as a value that is from 0 to 1 we'll just drag that in here and it's going to do any conversion it needs to do but since it takes a float and it's already a float I want to convert anything and then we just drag this into here hit compile and go back to the designer so this should all work now if you go to a percent it should say get percent underscore zero cool so that's all set up but if we play it's not going to be using our HUD because we haven't set it up to use a HUD at this point in time what we're gonna do is oh I need to save this as well so if we hit save that's all save now we'll go into a game mode and this would be of that build c-sharp file from earlier comes in so generic and come into a game mode okay so in our game mode in the dot h we need to add some things we need to add the interface and then we need to add the current interface so there's two different interfaces there and I'll explain what all of this does soon and we also need to override it to the begin player function so to do that virtual void override oh sorry at the begin play override and we'll try to come in here so we know what this means override begin play from the base class let's protect it here because we're going to be letting another class and hear it from this so we'll say at you property it defaults only you may not have seen this before what did its default edit defaults only means is we cannot edit it during runtime we can only edit the defaults before we actually run it again blueprint readwrite in case we need to access it from blueprints category equals stamina and then fundly meter equals blueprint predicted equals true and that is because we have a predictive variable but blueprints don't just work straight away we actually need to say blueprint particular equals true and now the blueprints know okay this is a predicted variable we're going to say that a sub class of cloths you use a widget and we'll call this the player how to close and then we're going to edit your property and there's not going to be anything in your property it's just your property class user widget current widget so that's the current widget that's on the player screen and that will allow us to cycle through different interfaces so we wanted to add an inventory later on we could do that the reason that I've added EU property here without any properties inside of that is basically I'm just saying enable garbage collection and stuff like that on this you property object but we don't need to actually declare anything inside of it in this case and then saying class before you use a widget is basically just a for a declaration and I'll explain more on that probably in the C++ basics video but anyways let's create a definition of begin play and end begin play we can first of all what we want to do actually is cool these super classes begin play okay now we need to get the character so a generic character my character equals cast a generic character your game place TEDx get player pour and this is just our way if you're a bit but in the blueprints we search get character controller this is the C++ equivalent of saying that this zero so there we go we now have that player's character now we just trick if HUD class is that what I called it apply a hot class that's what I called them f player HUD class is not equal to a null pointer so if it is equal to something if it has been initialized current widget so the players current which it on the screen equals create widget and create widget right now is not going to be available you'll see any sick if I type in you use a widget and then when you just pass the world to that and our class which is player how to Clause so create widget it doesn't know what that is and that's because we haven't included it so if you come up here now we can now say include blueprint slash user widget edge now if we didn't add those dependencies to our C sharp file we would need to type a lot more stuff than just blueprint slash use a widget on edge so we're just saving ourselves a bunch of work feel I'm just making things a lot easier for us and then once we've created current widget we just null check that as well so if current widget is not equal to null pointer current widget add to viewport there we go right now that's not working and I'm pretty sure that's just because intellisense hasn't loaded all that stuff yet there we go yep that's all working yeah all right very good guys that should be it I believe we just need to do like one or two more things so here we go we have our HUD we have to find that we're going to now lit a blueprint and hear it from our gamer so right click create blueprint class based on generic game mode and we'll just put it in hard I guess we'll call it generic underscore BP for blueprints Korea so we're creating a class and letting it in hear it from App Man game mode we need to compile a C++ code again so it actually recognizes what we are asking for here and again I expect that's gonna take six and a half decades as Unreal Engine does I'm gonna learn some video editing soon so I can just skip ahead of this compile stuff because I understand that this is pretty annoying but anyway said we go guys that's all done we have not the generic blueprint it asks for a player HUD class and all we do is select player Hut which is that hug class that we made earlier compile and save we'll make sure that HUD is compiled yep compile saved done and the last thing right so if we hit play it's still not working and I think it's not there and that's because this game mode that we made this blueprint game mode that inherits for an air man game mode it's not set up to use this game mode so I forget of project sittings maps and modes and then we go to default game mode and change that to generic underscore BP now should work yeah if we have play where you can see our stamina is there and that's slowly draining off and we now have an interface very very cool we could do this with lots more stuff and in another tutorial I will show you how to make this work with other things as well just a proof that this is working let's just change our drain rate for our players stamina to 0.1 so it should now drain 10 times faster so we'll save and then compile and when I play the game characters stamina will drain at a much faster rate so if I have play as you can see it's now going down a lot faster so that's just one very basic hardware type thing in here and in another tutorial I'll actually make this be a little more useful right now it's just draining stamina and it doesn't matter what we're doing a stamina is draining doesn't really make any sense but the idea is just to show you how to make C++ kind of interact with the blueprint side of things and get the HUD Foley working hopefully you guys know how this works now and in future tutorials we will add more functionality to this we will do tics things like that but this is just being a basic introduction to odds hopefully you guys enjoyed that I will put out another part pretty soon so see you guys in the next video
Info
Channel: reubs
Views: 34,480
Rating: undefined out of 5
Keywords: Unreal Engine, C++, Tutorial, How to make games, C++ beginner tutorial, Unreal Engine Beginner Tutorial
Id: Snz99qBh9qQ
Channel Id: undefined
Length: 28min 40sec (1720 seconds)
Published: Sun Mar 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.