Unreal Engine 5 Tutorial - C++ Part 1: Create a Class

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign and welcome to the start of our ue5 C plus plus tutorial series over the course of the series we'll be introducing you to how to use C plus in Unreal Engine 5 how to make use of it and how to actually understand it for your own projects in this first episode we're going to talk about how to create your new class and also expose variables over from the source file in C plus over to your blueprint class in the editor so let's get started when starting a new project we're going to start off with this menu screen that you usually get here but we're going to choose uh we'll go for the first person project and we're going to choose a C plus plus option here now you can do it start with blueprint and switch over to 50 plus afterwards you can totally do that um but this would make this setup a bit easier I'd have to restart or anything like that so make this one we'll name it and we'll do my C plus project and click create I'm just going to create the project for us when a project opens up you'll see the usual editor but it should also open your default IDE and for most people this would be Visual Studio but you can use any idea you want I personally use Visual Studio so to get started with our first C plus class you can go up to the top here the tools and here you'll see new C plus class if you're using Unreal 4 you will find in file instead so in button ue5 go to tools new C plus plus and this wizard is going to open up it's going to ask you what you want the parent class to be you can do an empty one completely or based upon an actor porn character anything you want really down here now most of the time your things are gonna be actors but it could also come from other things too but it's up to you and what you decide to do so let's do some basic setup here let's go and put an actor in next and we'll name it and call it one my test actor and we'll click create class that's going to add the header file and source file for our class here now these work slightly differently from the blueprint and typically the workflow is that the C plus class is used to define the class and what they do and the blueprint is made is used by the designer to customize and configure it to do the task I went to but it also extend it to do some extra functionality but typically that's where you find the uses going on here so here we have the generated files that you get from it this is the source file the dot CPP and the test actor H file will header file is here too um uh reload all there we go okay now the difference between the header file and the source file is the header file is sort of like the type of contents for a class so this is where you'll Define like the um variables the functions and so on so forth and the actual source file is where you define how they actually work and what they actually do um you'll also notice when you first load it up you'll get this window come up this is the live window a live coding window and when you compile your game this is what will come out with a successor file um and any errors if they fix okay so once it's finished doing that you're good to go so let's take a look at our code that we're getting here in our class so in the header file you're going to see the includes and in the includes we've got the core minimal the game framework actor.h and my test actor generate.h okay uh obviously massive actor is our one uh the game framework is the act of the H because we inherited from an actor so that's where that's coming from and core mineral is just the generic stuff that all actors and all objects have inside I think it's a minimum they must have and down here we've got the various definitions we've got going on here as I say it's pretty basic it's just an actor but up here you've got the class you can set up here with the Constructor public protected and public again so here they're doing uh public setting up the uh the default function by a my test actor um this is where you define your variables and functions uh typically in here but you may want to protect them in which case down here you've got virtual void begin play override and in public you've got tick so these are the functions that you're probably familiar with if you're doing blueprint they'll come in here by default for you as well um so I won't go through every single thing you're seeing here like every single symbol um go check out the primary series of videos it will help explain a few things that you may not have access to okay okay so then we've got the CPP file go into that and here you can see we're including the header file so that is now including everything that this is including um and in here we've got the Constructor here running and it's just saying it can't tick and then we've got the definition of the begin play which is not really doing anything and a tick which is not really doing anything okay now when these have got set up here with a super and super this just means they're going to run their default stuff that they would normally run because they can play without the time um so you can um override or extend functionality but that's all you really need okay so how do we actually make it show information to our editor because the whole point of using C plus plus is you're trying to put performance heavy stuff in C plus or stuff that you're going to be using regularly you can to find your own custom classes in here and make it configurable in your editor so let's go to the header file and talk about uh properties I'm going to show a property and make it appear in the editor so we're going to do is in our public section here I'm just going to put in a few new lines now you define a property to show an editor with the keywords U property and in Brackets you'll put in edit anywhere these are keywords anywhere these are keywords to help the engine identify what should be um shown in the editor okay we call that reflection system I think they call it reflection system but it looks for these sort of keywords and these uh visibility checks here to say what where these seem to be found don't need to edit anywhere um you can now Define the uh the variable itself if you do it same line or new line target.2 it doesn't really matter but it's all one bit of code on its own so here I'm gonna do um int 32 and do value like so okay so I've defined in my header there is going to be a value which is an integer 32-bit intro okay and I can edit it anywhere and what I want to do is now go into my cpep file and I want it to um set the value in my Constructor here for example so here we can do um value equals 100. okay and when you're happy with that you can I've got it whatever I value yep yeah um and that's it really um and this will make it so that the uh the property here is editable anywhere so they can um yeah make it appear in a certain section of the what you typically want to do otherwise it'll appear like a default blank section so you know how you have categories on the right hand side of your unreal actor class so if I make a new actor here or go into one of these ones um in your class defaults you've got these categories so transform animation mesh so we can make our own ones of those um by putting in our header here a comma and then defining again keyword category equals and in quote marks the name of the category you want to do so we're going to test values okay um okay yeah so let's do take a look at that so once I finish with that I'm going to save all of it okay and then I'm gonna go back to Unreal and we'll do a hot reloading and that is handled by this little button down here which is like a Tetris block basically click this and it'll recompile the engine and hopefully it'll come out with green with no errors wait for it to finish do its thing there you go successfully linked patch so then if I want to add the class now to my scene I can find all my classes by the way my C plus classes folder you can see my test actor is now there and if I want to create a child blueprint of this I'll just right click on it and go create blueprint class to based upon my test actor yeah let's just place it somewhere I'll put in the root and like that so now I've got this blueprint version of it and you can see here on the round side test values it's got its own section with value already set to 100. okay so now my editor can go through a design I can go through and change its values based upon what they want to show it in game um which is pretty neat now if you don't want um then to be able to change it um so if I just right click and hit do value you can see we can't actually change the value in the blueprint code we can only change it in the details so if you want to make it so you can find and change it on the fly during the run time then what you have to do is go into your um code here into the header and here we're going to add on edit anywhere blueprint read write add in this extra keyword let's save it will mean that I can now access it inside the blueprint as well so let's go back and reload this one so let's notice that we load whatever this thing and there we go so now if I go back to my class I made my test actor values still there on the right hand side because of edit anywhere but now I should be able to access value as a verbal okay so it only appears there if I have it's set to blueprint read write in the visual studio okay without that just means I can exit anywhere if I don't put any anywhere or any of this it just means that this is only going to be used in the C plus side of the project okay so you can't access it at all in the editor which is important if you're doing like if you're a tools programmer and you want to put some functionality but you don't want the designer change certain aspects of it because it's critical to the functioning of the class you you can make them hidden like that um also making private variables if you want as well another keyword we can use as well is we can if you want to show it but we don't want to change it so we have edit anywhere but we're going to change that to blueprint read only and we'll make it visible so you can see it visible anywhere they're not not edit anywhere visible anywhere and we're also going to make it transient oh hello okay and then just calculate the same so all these keywords are working together to make something like this so basically the transient uh specifier so read only let's go through them so read only means that you can only get the value you can't set it uh visible means you can only see it you can't change it in the class defaults transient means that the um it won't be saved or loaded from a disk is uh so it's it's a non-persistent value it's just there you don't have to store anywhere or anything like that um and the category we know about is sending the calculator okay so then we're going to save all and then back into our project we'll hop reload recompile the engine and what that's done okay now take a look at the actor you can see here the value is now grayed out I can't change it okay that's because it is visible and read only I can see it but I can't change it same goes for in Blueprint here I go to Value go down I can only get the value I can't set it that's all right about there and there you go we've now got a variable showing from our custom class into our editor allowing game designers to take on what we're setting up and make the game we want to make in the next episode we're going to go through and showcase how to expose functions and events over in our formation Plus Class into our editor blueprint class you can watch the next episode right now over on patreon.com forward slash Ryan laley we can find all my videos only from just one dollar a month as a thank you to all my patrons and usual members for the continued support in the channel thanks so much for watching and I'll see you next time bye everyone thank you [Music] foreign [Music]
Info
Channel: Ryan Laley
Views: 45,088
Rating: undefined out of 5
Keywords:
Id: FBpnOuCgHu4
Channel Id: undefined
Length: 14min 46sec (886 seconds)
Published: Mon Apr 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.