Why Building a C++ Component in Unreal Engine is Easier Than You Think!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there so you want to create your game in unreal you know a little bit about Blueprints and the general unreal architecture but you never actually touch the C++ side of things well this video series will teach you all about the unreal C++ magic I know we will start with the basics and with every new video we get a bit more advanced in this video we will create our first C++ actor component and use it in our project let's get started first create your unre project and choose a template I will choose the third person template and also make sure to click on the C++ button give it a name and then click on create so when you first created your project you will be greeted by the Unreal Engine uh make sure that you have the C++ classes folder in here and when you open your project in Visual Studio code then you should have all the folders like the config folder and the content folder uh most important for C++ code is the source folder where we create our C++ classes if you're having trouble with the visual studio code setup check out the easy to follow guide in the description link create a first C++ actor component by even using the class wizard provided in the engine or just use your favorite code editor and create a header file and a CPP file if you want to choose C Class wizard you can just right click in the engine and use new C++ class and under all classes you can choose which class it needs to inherit from uh for our example you should choose actor component if you don't want to use the class wizard you can also easily do it in the editor by just right clicking and using new file and you need to name your component and your classes so in this case we want to create a super simple rotation component that will just rotate an actor I will name the file rotating component be sure to use the H as its extension then create a second file with the same name but this time use CPP for its [Music] extension so the CPP file is the implementation file where we Implement our function and the header file is our declaration file where we declare our properties variables and functions okay copy and paste the definition template you can find it inside the description make sure to change all the occurrences of the template name to your desired class name uh here you find my actor component. generated. we just use rotating component in this case generated files in unre engine or autogenerated files that automatically link your C++ code to the engine's features like blueprints and editor tools without you needing to edit them you only need the game API macro if you want to use your class in a different C++ module since that is not needed yet you can remove it the class name will be you rotating compon component also rename the Constructor to ur rotating component this function is a Constructor a Constructor is a special function in a class that sets up new objects when you create an object this function runs automatically to initialize it like setting initial values for its properties The Tick component function works just like the blueprint tick function updating with every tick of the game engine in this case we don't really need begin play so we can just remove it let's go to the tting component. CPP and create our Constructor here to implement a function in the CPP file we first reference the class and then the function's name for example for the Constructor we just type you rotating component followed by a double column and then Ur rotating component again the first U rotating component for the class reference and the second Ur rotating component for the function reference as you can see we have some red squiglet here and this is because of two things so visual studio code and and C++ in general doesn't know what we are referencing until we include every header file that we want to work with so let's include it just do # include and then reference our rotating component. H the red underline under include indicates that Visual Studio code can't locate the header file typically caused by configuration errors to fix this regenerate the visual studio code project files and reload the project you can easily do it by going to your um project folder and then search for the Unreal Engine project file and go here under the options and generate Visual Studio project files as we have done this you can see that uh the rotating component is now highlighted in green this means that it works exactly but the generated file is uh underlined red this is because we didn't recompile the project yet this is a crucial rule tool when you make changes to any C++ file you must recompile the project to see the updates this process requires closing the engine and then recompiling it also only after recompiling the project it will autogenerate the files with this dot generated extension okay as we have tick function we want to be able to tell the uh project that this T tick function can be run this is easily done under the primary component tick and under can a a tick you can just set it to True okay let's also create the tick component in the CPP file we can just copy and paste the um the whole declaration here we don't need the visal and we don't need the overwrite uh we can just post it here remember to reference the class as well if you only use the function name C++ will treat it as a new function so prefix The Tick component function name With Ur rotating component followed by a double colon to to correctly associate with with the class and maybe we can just go ahead and compile the changes now and see if the rotating component appears when we create an actor so go under the run and debug Tab and go under launch third person project editor and either use development or debug game I will just use development in this case click on launch and then it should launch a project okay so we are here under the Unreal Engine and everything should be compiled but how can we check this actually well this should be very easy we just need to go under our content and create a new blueprint class and just go and create an actor in this case we just call it test Cube maybe because we will add a new Cube um go on Ed and use static mesh and click here and maybe search for Cube or any mesh that you like um compile the changes and drag that cube in so now we have a cube okay with this Cube we will add our new rotating component um so click on it search for rotating and you should see rotating under custom click on it and our new rotating component appears when we compile and we go under the play button nothing should happen of course because we didn't do anything actually but as you can see it's very easy to create a new component and uh we have done that we added a new component under the actor now we create our functionality for this actor actually so close the Unreal Engine again and maybe just reload the window because then actually you should see the generator. H file appear and it shouldn't be underlined in red anymore okay for our rotation component we actually need some variables in this case we use a vector and we Define an rotation AIS to create a variable first specify its type then its name followed by a semicolon to modify the variable in the blueprint editor also include a U property specifier the U property mro integrates the variable into unre reflection system crucial for interacting with blueprints and ensuring that pointers to assets like animation Montage sounds and arrays remain valid throughout your class's lifetime the U property macro supports additional options like edit anywhere which lets you modify the property directly from unrealed property window so when we have done this and we create just another variable like for example a Flo variable for the rotation speed and we recompile our changes by clicking on the green triangle then we should see our two variables appear under the property window inside the actor component okay and as you can see when we click on the rotating component and we look at the property window we can see a rotating component here our rotation AIS and our rotation speed so this is working fine we can define a AIS here for example D1 uh in the Z and one in the Y and also rotation speed maybe or 30 as the next step let's build the rotation functionality close unreal again first of all we should call Super for the tick function in unre engine C++ super is a way to use features from a class's parent class for begin play and tick you always need to call Super as the parent actor component class contains crucial functionality in order to rotate the actor we want to create a new rotation the new rotation will be calculated by using the rotation AIS multiplying it with the rotation speed and then multiplying it with the Delta time and then we have our new rotation now we just need to set the new rotation with get owner this is the owner of the actor component so this will be the actor and then you can just go and do add actor local rotation and we create a quarteron from our new Ula rotation we can also use Auto as a variable type the auto data type in C++ is useful because it lets the compiler automatically figure out the type of the variable this means you don't have to explicitly State the variables type which can make your code shorter and easier to read and if we compile our changes you will see that our Cube should rotate now okay let's go and play and as you can see our cube is rotating now maybe we want to also create some default variables we can do this under the Constructor so the Constructor like I mentioned will be the place where you want to set your default values for your variables we can maybe Define the rotation axis um in default as a vector where the x is zero the Y is zero and maybe the one uh the Z is a one so it will rotate on the z-axis and maybe we just do a rotation speed a default rotation speed of 20 it is better if we just go for 20.0 f compile okay so as you can see our rotation speed is the same like we had before and when we reset this property to its default value then it's appearing as a zxs of one and if we reset our rotation speed also it will be changed to 20 so these default Val are coming from the Constructor in the next video we will go through all the different kinds of property specifiers and also go into the details when and why you would need your property afterwards we will also go through U function decorators the unreal event system how you can lazy load your assets and so on stay tuned and thank you so much for watching this video and see you next time
Info
Channel: Scripted Adventure
Views: 3,431
Rating: undefined out of 5
Keywords: unreal engine, c++, unreal engine 5, tutorial, programming, gamedev, game development
Id: sWSbqAAv8Gk
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Wed Dec 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.