{"id":530,"date":"2022-12-18T16:46:02","date_gmt":"2022-12-18T16:46:02","guid":{"rendered":"https:\/\/merserver.com\/?p=530"},"modified":"2022-12-18T16:46:58","modified_gmt":"2022-12-18T16:46:58","slug":"the-complete-c-guide-for-unreal-engine","status":"publish","type":"post","link":"http:\/\/merserver.com\/index.php\/2022\/12\/18\/the-complete-c-guide-for-unreal-engine\/","title":{"rendered":"The Complete C++ Guide For Unreal Engine"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Reading Time:&nbsp;10&nbsp;minutes<\/h1>\n\n\n\n<p>Level: Beginner \u2013 Intermediate \u2013 Advanced<\/p>\n\n\n\n<p>Version: Any Unreal Engine Version<\/p>\n\n\n\n<p>Help Others Learn Game Development<\/p>\n\n\n\n<p>C++ is the most effective and widely used programming language for creating video games.<\/p>\n\n\n\n<p>It\u2019s also the language Unreal Engine, arguably the most popular game engine in the world, uses too.<\/p>\n\n\n\n<p>However, there are some differences between the traditional C++ programming software developers use and C++ that is used in Unreal Engine to create video games.&nbsp;The concepts are the same, just the syntax is slightly different.<\/p>\n\n\n\n<p>In this post we\u2019ll cover in depth the differences between C++ for software development and C++ for Unreal Engine game development.<\/p>\n\n\n\n<p>Before we start, make sure that you download Unreal Engine and Visual Studio. You can follow the guide in the links below to do that:<\/p>\n\n\n\n<p>For every example we use, first we will show how it\u2019s done in traditional C++ software development language, then we\u2019ll show the same example how it\u2019s done with C++ in Unreal Engine.<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/unreal?utm_source=blog_unreal_m6\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Variables In C++<\/h3>\n\n\n\n<p>Variables are the heart of any software application or video game. We use them to declare the properties in the app or game which will represent the high score, health, speed, ammo count, names of game characters, and so on.<\/p>\n\n\n\n<p>This means that there are different types of variables such as whole numbers, decimal point numbers, strings, and booleans.<\/p>\n\n\n\n<p>This is how we declare them in pure C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ this include will add the library e.g. predefined\n\/\/ code that includes the string variable\n#include &lt;string&gt;;\n\n\/\/ the name space std is where the string is defined\nusing namespace std;\n\nclass Player\n{\n\tint Health = 100;\n\tfloat Speed = 5.5f;\n\tstring Name = \"Wizard\";\n\tbool IsDead = true;\n};<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When declaring a variable, first we declare its type, then we give it a name, then we give it an optional value and we end the declaration usimg a semi colon \u201d ; \u201c.<\/p>\n\n\n\n<p>The type of variable is represented by int, float, string, and bool. We\u2019ll talk about every variable type in second.<\/p>\n\n\n\n<p>And the value of that variable is represented by the value we add after the&nbsp; optional \u201d = \u201d sign. We say optional because we can also declare a variable like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int Health;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>This means we declared a variable of type int, with the name Health and no value. Of course, every variable has a default value, which we\u2019ll talk about as well.&nbsp;<\/p>\n\n\n\n<p>And we can always change the values of any variable we declare hence why they are called variables. Now let\u2019s go line by line very variable we declared and explain what it is.<\/p>\n\n\n\n<p>The variables are declared on lines 10, 11, 12,&nbsp; and 13.<\/p>\n\n\n\n<p>Variable declared on line 10 is called an integer, or short int. Integers are whole numbers meaning they can\u2019t have decimal points in their value.<\/p>\n\n\n\n<p>A float declared on line 11 on the other hand is a decimal point number.<\/p>\n\n\n\n<p>And even if you declare a float value which is a whole number, you still have to include a decimal point by adding at least the dot \u201d . \u201c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ this will show an error in code\nfloat Speed = 5f;\n\n\/\/ this is the correct way to declare a whole float\nfloat Speed = 5.0f;\n\n\/\/ this is the correct way to declare a whole float\nfloat Speed = 5.f;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>The variables declared on lines 2 and 5 are exactly the same and will have the same value. They are both float with the value of 5. The only difference is when we declared the one on line 5, we included the decimal value.<\/p>\n\n\n\n<p>One thing you\u2019ll notice with float variables is that we add \u201d f \u201d at the end of the declaration. The reason why we add \u201d f \u201d at the end of a float declaration is because we have another decimal point type variable called a double.<\/p>\n\n\n\n<p>This is how we declare it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double Mana = 2.4;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>The difference between a float and a double is in the precision. A double is more precise than a float, but these precisions go in the high decimal point values.<\/p>\n\n\n\n<p>An example for that would be a number like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3.19827396781623871628376<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>But there\u2019s almost no case in game development where you\u2019ll need a decimal number that has more than 3 decimal points.<\/p>\n\n\n\n<p>We\u2019re working in the game dev industry since 2015 and we only encountered a decimal number with 2 points at most during our career.<\/p>\n\n\n\n<p>And that\u2019s why float is used more than a double in game development and no game engine is an exception to this.<\/p>\n\n\n\n<p>Next, we have a string variable declared on line 12 in the above example. Strings represent characters and we use them to create variables that in most cases will repsent the names or tags of game objects.<\/p>\n\n\n\n<p>Also, when you declare a value of a string, you need to use quotes (\u201c\u201d):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string Name = \"Wizard\";<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>And the last variable declared on line 13 in the example above is a boolean or short bool.<\/p>\n\n\n\n<p>While all variabels we saw so far can have any number value or character value for a string, booleans can only have one of the two values: true or false.<\/p>\n\n\n\n<p>The IsDead bool value above has a value of true. We can also declare the value of that variable to be false, and that\u2019s it.<\/p>\n\n\n\n<p>Booleans are used for conditional statements and for comparing variables with one another. We\u2019ll see examples of this as well.<\/p>\n\n\n\n<p>Now, let\u2019s take a look at how can we declare these same variables inside Unreal Engine and the syntax differerence between C++ for Unreal, and C++ for software development.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C++ Variables In Unreal Engine<\/h3>\n\n\n\n<p>As we already mentioned, C++ for software development and C++ for Unreal Engine are one and the same language. The concepts and the principles are the same, the only difference is the syntax.<\/p>\n\n\n\n<p>And syntax means the way how the code is written. So let\u2019s take a look at that difference.<\/p>\n\n\n\n<p>Here\u2019s how we declare the same types of variables we saw in the example above inside a C++ class in Unreal Engine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int Health = 100;\nfloat Speed = 5.5f;\nFString Name = \"Wizard\";\nbool IsDead = false;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>As you can see all the variables exactly the same except for the string variable which is named FString.<\/p>\n\n\n\n<p>Aside from the name being different e.g. the string inside Unreal Engine has F in front of it, everything else is the same.<\/p>\n\n\n\n<p>The FString variable functions the same way as the string variable inside C++. We still need to put the value of that string between quotes \u201c\u201d and it\u2019s a character type variable.<\/p>\n\n\n\n<p>And this is that syntax difference. Essentially you\u2019re using the same thing, but you\u2019re writing it in a different way, that\u2019s it.<\/p>\n\n\n\n<p>We can also delcare a double the same way we did in C++ in the example above:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double Mana = 75.5;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Again, we\u2019ll use floats most of the time inside Unreal Engine, but this is just to show you that these variables are the same in Unreal Engine as well as C++.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Math Operations With Variables<\/h3>\n\n\n\n<p>We can perform math operations with variables. This is how we move the characters, shoot bullets, count score and many other things in our games.<\/p>\n\n\n\n<p>For some reason, a lot of people think that you need to be a math genious to do this, but as you\u2019ll see in a moment, all you need to know is basic math operations like addition, subtraction, multiplication and division.<\/p>\n\n\n\n<p>Here\u2019s how that looks like in C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nint main()\n{\n\tfloat Health = 55.0f;\n\tfloat HealAmount = 10.0f;\n\tfloat Damage = 5.0f;\n\n\tstd::cout &lt;&lt; \"The remaining health is: \" &lt;&lt; Health + HealAmount;\n\tstd::cout &lt;&lt; \"\\nThe remaining health is: \" &lt;&lt; Health - Damage;\n\tstd::cout &lt;&lt; \"\\nThe remaining health is: \" &lt;&lt; Health * HealAmount;\n\tstd::cout &lt;&lt; \"\\nThe remaining health is: \" &lt;&lt; Health \/ Damage;\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run this program this is the output we get in the console:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-1.jpg\" alt=\"CPP In Unreal - 1\" title=\"CPP In Unreal \u2013 1\"\/><\/figure>\n\n\n\n<p>These are the basic math calculations we need to know to create games in Unreal Engine.<\/p>\n\n\n\n<p>You would need to know advanced math operations only if you want to work as a graphics developer, physics engine developre, or work on vector math. Otherwise, these basic operations are enough to create pretty much any game you want inside Unreal Engine.<\/p>\n\n\n\n<p>And speaking of Unreal Engine, let\u2019s see how these same math operations look like inside Unreal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n\n\tfloat Health = 55.0f;\n\tfloat HealAmount = 10.0f;\n\tfloat Damage = 5.0f;\n\n\tfloat HealthAddition = Health + HealAmount;\n\tfloat HealthSubtraction = Health - Damage;\n\tfloat HealthMultiplication = Health * HealAmount;\n\tfloat HealthDivision = Health \/ Damage;\n\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Addition is: %f\"), HealthAddition);\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Subtraction is: %f\"), HealthSubtraction);\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Multiplication is: %f\"), HealthMultiplication);\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Division is: %f\"), HealthDivision);\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run the game this is the out we get in the Output Log tab:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-2.jpg\" alt=\"CPP In Unreal - 2\" title=\"CPP In Unreal \u2013 2\"\/><\/figure>\n\n\n\n<p>By performing the same operations on the same values we got the same result both in C++ raw application and in Unreal Engine.<\/p>\n\n\n\n<p>The only difference is that in Unreal Engine it displays six decimal point numbers, but don\u2019t worry about that since that\u2019s the default way how the output works and we can of course limit this to five, four, even one decimal point number. But since that\u2019s not the point of this post we will not dive into that topic.<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuahttps\/\/awesometuts.com\/unreal?utm_source=blog_unreal_m15ts.com\/unreal?utm_source=blog&amp;amp;utm_medium=banner&amp;amp;utm_campaign=unreal_m15\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Functions In C++<\/h3>\n\n\n\n<p>In every program or video game we create we\u2019ll have code that we reuse over and over again.<\/p>\n\n\n\n<p>And if that code has let\u2019s say 100 lines, instead of copy-pasting those 100 lines in every part of the project where we need it, we can group that code in a function and reuse it whenever we need it.<\/p>\n\n\n\n<p>Using the math operations with variables example, we\u2019re going to create functions for every math operation.<\/p>\n\n\n\n<p>Here\u2019s how that looks like in C++ application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nint main()\n{\n\tAddNumbers(55.0f, 10.0f);\n\tSubtractNumbers(55.0f, 5.0f);\n\tMultiplyNumbers(55.0f, 10.0f);\n\tDivideNumbers(55.0f, 5.0f);\n}\n\nfloat AddNumbers(float a, float b)\n{\n\tstd::cout &lt;&lt; \"The Addition value of the numbers is: \" &lt;&lt; a + b;\n}\n\nfloat SubtractNumbers(float a, float b)\n{\n\tstd::cout &lt;&lt; \"The Subtraction value of the numbers is: \" &lt;&lt; a - b;\n}\n\nfloat MultiplyNumbers(float a, float b)\n{\n\tstd::cout &lt;&lt; \"The Multiplication value of the numbers is: \" &lt;&lt; a * b;\n}\n\nfloat DivideNumbers(float a, float b)\n{\n\tstd::cout &lt;&lt; \"The Division value of the numbers is: \" &lt;&lt; a \/ b;\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run the program this is the output we get in the console:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-1.jpg\" alt=\"CPP In Unreal - 1\" title=\"CPP In Unreal \u2013 1\"\/><\/figure>\n\n\n\n<p>We basically took the same operations from the previous example, put them in a function and now we can simply pass two float numbers as parameters that we\u2019ll use inside the function to perform the specific operation.<\/p>\n\n\n\n<p>The functions we declare return a float value and take two parameters. The value that\u2019s being returned as well as the parameter values are a float type.<\/p>\n\n\n\n<p>Of course, we can set these values to be integers, booleans, or strings.<\/p>\n\n\n\n<p>Now let\u2019s take a look at how do we declare the same functions inside Unreal Engine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float AddNumbers(float a, float b);\nfloat SubtractNumbers(float a, float b);\nfloat MultiplyNumbers(float a, float b);\nfloat DivideNumbers(float a, float b);<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>First, we declare the functions inside the .h file of the C++ class inside Unreal Engine. Essentially, in Unreal Engine when you create a C++ class automatically you get a .h and .cpp file for that class.<\/p>\n\n\n\n<p>In the .h file you declare all the variables and functions for that class, and inside the .cpp file you implement the functionality of the variables and functions.<\/p>\n\n\n\n<p>This is a brief explanation as we\u2019ll not dive into that topic more than that for this post since we already have other posts where we explain that in-depth.<\/p>\n\n\n\n<p>We\u2019ll put links of those posts at the bottom of this one and you can check them out after you finish this post.<\/p>\n\n\n\n<p>After we declared the functions in the .h file, we implement them in the .cpp file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Addition is: %f\"), AddNumbers(55.0f, 10.0f));\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Subtraction is: %f\"), SubtractNumbers(55.0f, 5.0f));\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Multiplication is: %f\"), MultiplyNumbers(55.0f, 10.0f));\n\tUE_LOG(LogTemp, Warning, TEXT(\"The value of health Division is: %f\"), DivideNumbers(55.0f, 5.0f));\n}\n\nfloat APlayerCharacter::AddNumbers(float a, float b)\n{\n\treturn a + b;\n}\n\nfloat APlayerCharacter::SubtractNumbers(float a, float b)\n{\n\treturn a - b;\n}\n\nfloat APlayerCharacter::MultiplyNumbers(float a, float b)\n{\n\treturn a * b;\n}\n\nfloat APlayerCharacter::DivideNumbers(float a, float b)\n{\n\treturn a \/ b;\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>The APlayerCharacter is the name of the class which we use when we implement the function functionality like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return value ClassName::FunctionName(parameters if any)\n{\n    implementation of the function\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run the game this is the out we get in the Output Log tab:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-2.jpg\" alt=\"CPP In Unreal - 2\" title=\"CPP In Unreal \u2013 2\"\/><\/figure>\n\n\n\n<p>Since we used the same numbers, the values are exactly the same as in the C++ example and in the previous Unreal Engine example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conditional Statements<\/h3>\n\n\n\n<p>In programming and game development we always have situations where decisions are made.<\/p>\n\n\n\n<p>Should you press the left or the right button, does the player need to go left or right, is the health value lower or greater than zero and so on.<\/p>\n\n\n\n<p>In programming and game development we use conditional statements to create choices in the application or game.<\/p>\n\n\n\n<p>This is how a conditional statement looks like inside C++ app:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (true)\n{\n\t\/\/ code goes here\n}\nelse\n{\n\t\/\/ code goes here\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Conditional statements are also called if else statements. The way if else statements work is you test for a condition, and if that condition is true the clause where that condition is true will be executed.<\/p>\n\n\n\n<p>In the example above, the caluse in the if(true) will be executed and the clause for the else will not be executed.<\/p>\n\n\n\n<p>One thing you\u2019ll notice is the \u201ctrue\u201d part of the if else statement. This is where booleans come into play.<\/p>\n\n\n\n<p>If you remember at the beginning of the post when we talked about the bool variable we said we use it to test conditions in our projects, and this is how that looks like.<\/p>\n\n\n\n<p>Of course, when writing an app or a game we\u2019ll never use if(true) for a statement, instead we would have variables that indicate a state in the game, such as IsAlive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool IsAlive = true;\n\nif (IsAlive)\n{\n\t\/\/ player is still alive don't end the game\n}\nelse\n{\n\t\/\/ player is dead. end the game\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>In the example above the bool declared on line 1 will always have a value of true, but in a game events will happen that can lower the player\u2019s health value to or below zero and then the IsAlive variable will have a value of false and then we can run the game over code.<\/p>\n\n\n\n<p>We can also compare numbers with if else statements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float a = 10.0f;\nfloat b = 5.0f;\n\nif (a &gt; b)\n{\n\t\/\/ a is greater than b\n}\nelse\n{\n\t\/\/ a is less than b\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Again, in the example above a and b will always have a value of 10 and 5, but this is just to demonstrate how the if else statements work.&nbsp;<\/p>\n\n\n\n<p>In a real project the values you declare for your classes will always change and thus the comparisons you make will be dynamic.<\/p>\n\n\n\n<p>Here are all the comparisons we can make with numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float a = 10.0f;\nfloat b = 5.0f;\n\nif (a &gt; b)\n{\n\t\/\/ a is greater than b\n}\n\nif (a &gt;= b)\n{\n\t\/\/ a is greater than or equal to b\n}\n\nif (a &lt; b)\n{\n\t\/\/ a is less than b\n}\n\nif (a &lt;= b)\n{\n\t\/\/ a is less than or equal to b\n}\n\nif (a == b)\n{\n\t\/\/ a is equal to b\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>And here\u2019s another way how we can write these same conditions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float a = 10.0f;\nfloat b = 5.0f;\n\nif (a &gt; b)\n{\n\t\/\/ a is greater than b\n}\nelse if (a &gt;= b)\n{\n\t\/\/ a is greater than or equal to b\n}\nelse if (a &lt; b)\n{\n\t\/\/ a is less than b\n}\nelse if (a &lt;= b)\n{\n\t\/\/ a is less than or equal to b\n}\nelse if (a == b)\n{\n\t\/\/ a is equal to b\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/unreal?utm_source=blog_unreal_m3\"><\/a><\/p>\n\n\n\n<p>Notice in the example above we\u2019re using else if statements and in the example prior to that we were just using if statements.<\/p>\n\n\n\n<p>Another thing to point out is for the last comparison if a is equal to b. We are using the double equal sign \u201d == \u201d to compare the two numbers.<\/p>\n\n\n\n<p>So when you\u2019re comparing two numbers if their values are equal you use the double equal sign \u201d == \u201d but when you declare a value for a variable then you use a single equal sign \u201d = \u201c.<\/p>\n\n\n\n<p>In this case, there\u2019s no difference in the result we get for the conditions we\u2019re testing between the if statements and if else statements, but we can use different conditional statement structures to test different conditions.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float a = 10.0f;\nfloat b = 5.0f;\n\nif (a &gt; b)\n{\n\t\/\/ a is greater than b\n}\nelse if (a &lt; b)\n{\n\t\/\/ a is less than b\n}\nelse\n{\n\t\/\/ a is neither greater than or less than b\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>In the example above, if a is greater than b we\u2019ll execute the code for that condition. If a is less than b we\u2019ll execute the code for that condition. And lastly, if neither of those conditions is true, we\u2019ll execute the code in the \u201celse\u201d clause.<\/p>\n\n\n\n<p>In a practical example this would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (wolf enemy attacks the player)\n{\n\t\/\/ deal 10 damage to the player\n}\nelse if (hawk enemy attacks the player)\n{\n\t\/\/ deal 20 damage to the player\n}\nelse\n{\n\t\/\/ deal 5 damage to the player\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Of course, in our game we would not name the variables like in the example above, but this is just to illustrate different situations we might have in a game.<\/p>\n\n\n\n<p>Another thing to note is that all these comparisons end up being either true or false, hence the booleans involment in conditional statements.<\/p>\n\n\n\n<p>When we test if a &gt; b this will either result in true if a is greater than b, or false if a is not greater than b.<\/p>\n\n\n\n<p>This also means that we can set boolean values like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float a = 5.0f;\nfloat b = 4.0f;\n\nbool c = a &gt; b;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>We are comparing if variable a is greater than b which will result in either true or false, and we set that value to the boolean c variable.<\/p>\n\n\n\n<p>Of course, in the example above the value will be true, so if we were to print the value of the c variable we would see true being printed in the console.<\/p>\n\n\n\n<p>Now let\u2019s go over to Unreal Engine and see how the conditional statements look inside Unreal Engine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float a = 5.5f;\nfloat b = 3.3f;\n\nif (a &gt; b)\n{\n\t\/\/ a is greater than b\n}\nelse if (a &lt; b)\n{\n\t\/\/ a is less than b\n}\nelse\n{\n\t\/\/ a is neither greater nor less than b\n}\n\nif (a &gt; b)\n{\n\t\/\/ a is greater than b\n}\n\nif (a &lt; b)\n{\n\t\/\/ a is less than b\n}\n\nif (a &gt;= b)\n{\n\t\/\/ a is greater than or equal to b\n}\n\nif (a &lt;= b)\n{\n\t\/\/ a is less than or equal to b\n}\n\nif (a == b)\n{\n\t\/\/ a is equal to b\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>As you can see there\u2019s no difference between using conditional statements in a C++ application or Unreal Engine game.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C++ Loops<\/h3>\n\n\n\n<p>Another thing that we do often is apps and video games is repeat a certain mechanic. And we do that with the help of loops.<\/p>\n\n\n\n<p>This is how a loop looks like in C++ app:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nint main()\n{\n\tfor (int i = 0; i &lt; 10; i++)\n\t{\n\t\tstd::cout &lt;&lt; \"The value of i is: \" &lt;&lt; i &lt;&lt; \"\\n\";\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run the app, this is what we\u2019ll see printed in the console:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-3.jpg\" alt=\"CPP In Unreal - 3\" title=\"CPP In Unreal \u2013 3\"\/><\/figure>\n\n\n\n<p>The loop in the example above is called a for loop. And the for loop basically loops over the code that\u2019s between the curly brackets \u201d {} \u201c, in the example above it is calling the std::cout code 10 times.<\/p>\n\n\n\n<p>Why 10 times?<\/p>\n\n\n\n<p>Because the condition of the loop is i &lt; 10, which means it will loop for as long as i has a value that\u2019s less than 10. Then it will increment the i value by 1 using the ++ syntax which basically means take the current value if i and add 1 to it.<\/p>\n\n\n\n<p>As soon as the i value reaches a value that\u2019s not less than 10, the loop will stop looping.<\/p>\n\n\n\n<p>Here\u2019s a more in-depth guide about loops in C++:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-loops\/\" target=\"_blank\" rel=\"noreferrer noopener\">Loops In C++<\/a><\/p>\n\n\n\n<p>Now we are going to take a look at how a for loop looks like inside Unreal Engine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n\n\tfor (int i = 0; i &lt; 10; i++)\n\t{\n\t\tUE_LOG(LogTemp, Warning, TEXT(\"The value i is: %i\"), i);\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run the game, this is what we\u2019ll see printed in the Output Log tab:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-4.jpg\" alt=\"CPP In Unreal - 4\" title=\"CPP In Unreal \u2013 4\"\/><\/figure>\n\n\n\n<p>As you can see the C++ for loop is exactly the same in Unreal Engine as well. And the same explanations are valid for both versions.<\/p>\n\n\n\n<p>Let\u2019s take a look at another type of loop called a while loop.<\/p>\n\n\n\n<p>Here\u2019s a C++ example of the while loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(condition is true)\n{\n    \/\/ execute the code\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>This is how the structure from the for loop would look like in a while loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 0;\n\nwhile(i &lt; 10)\n{\n    std::cout &lt;&lt; \"The value if i is \" &lt;&lt; i &lt;&lt; \"\\n\";\n    i++;\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>When we run the app, this is what we\u2019ll see in the console:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-3.jpg\" alt=\"CPP In Unreal - 3\" title=\"CPP In Unreal \u2013 3\"\/><\/figure>\n\n\n\n<p>While the structure is slightly different, the purpose and use are the same.<\/p>\n\n\n\n<p>The only thing we need to be careful of with the while loop is to make the condition of the loop eventually be false, otherwise, we would have an infinite while loop.<\/p>\n\n\n\n<p>You can read more about that in the link below:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-loops\/\" target=\"_blank\" rel=\"noreferrer noopener\">Loops In C++<\/a><\/p>\n\n\n\n<p>And here\u2019s how a while loop looks like in Unreal Engine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n\n\tint i = 0;\n\n\twhile (i &lt; 10)\n\t{\n\t\tUE_LOG(LogTemp, Warning, TEXT(\"The value i is: %i\"), i);\n\t\ti++;\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>And when we run the game, this is what we\u2019ll see in the Output Log tab:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-4.jpg\" alt=\"CPP In Unreal - 4\" title=\"CPP In Unreal \u2013 4\"\/><\/figure>\n\n\n\n<p>Again, the structure and the explanations are exactly the same. And you also need to be careful about the while loop condition eventually becoming false so that you don\u2019t have an infinite while loop.<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/unreal?utm_source=blog_unreal_m2\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Arrays In C++<\/h3>\n\n\n\n<p>The variables we saw so far can only store one value at a time. This is where arrays come into play.<\/p>\n\n\n\n<p>Arrays can store multiple values at the same time and we access these values using their index number.<\/p>\n\n\n\n<p>Any variable can be an array, be that integer, float, boolean, or a string. But the array can only store the values of the same variable type.<\/p>\n\n\n\n<p>For example, if we create an array of integers, we can only store integers in that array. We can\u2019t store floats, strings, or booleans.<\/p>\n\n\n\n<p>We already covered C++ arrays in another blog post which you can check out by clicking the link below:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-arrays\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arrays In C++<\/a><\/p>\n\n\n\n<p>Using the same example from the post in the link above, this is how arrays are declared in C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int DamagePoints&#91;10];\n\n\/\/ FIRST WE INITIALIZE ARRAY VALUES\nfor (int i = 0; i &lt; std::size(DamagePoints); i++)\n{\n    DamagePoints&#91;i] = rand();\n}\n\n\/\/ THEN WE PRINT THOSE VALUES\nfor (int i = 0; i &lt; std::size(DamagePoints); i++)\n{\n    std::cout &lt;&lt; \"\\nThe value of element is: \" &lt;&lt; DamagePoints&#91;i];\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>And when we run the app we will see random integer values printed in the console.<\/p>\n\n\n\n<p>Now, let\u2019s do the same in Unreal Engine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TArray&lt;int&gt; DamagePoints;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>First, inside the .h file of the C++ class we declare the array. For that we use TArray and between the &lt;&gt; we declare the type of variable this array will store, in this case an integer.<\/p>\n\n\n\n<p>And this is how we initialize the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n\n\tfor (int i = 0; i &lt; 10; i++)\n\t{\n\t\tDamagePoints.Add(FMath::RandRange(1, 100));\n\t}\n\n\tfor (int i = 0; i &lt; DamagePoints.Num(); i++)\n\t{\n\t\tUE_LOG(LogTemp, Warning, TEXT(\"The value Element at index %i is: %i\"), i, DamagePoints&#91;i]);\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>The difference between the C++ array we saw in the example above and the TArray in Unreal Engine is that the TArray is a dynamic array.<\/p>\n\n\n\n<p>Dynamic meaning it\u2019s resizeble and not fixed in size like the normal C++ array. Which means we can add and remove as many elements as we want.<\/p>\n\n\n\n<p>But the essential use is the same.<\/p>\n\n\n\n<p>We can store multiple elements inside the array, and we can use their indexes to access those elements which we saw in the examples above.<\/p>\n\n\n\n<p>Again, you can read more about arrays in the post link below:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-arrays\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arrays In C++<\/a><\/p>\n\n\n\n<p>And we also cover TArrays in more depth inside&nbsp;<a href=\"https:\/\/awesometuts.com\/unreal?utm_source=blog&amp;utm_medium=post&amp;utm_campaign=cpp_for_unreal\" target=\"_blank\" rel=\"noreferrer noopener\">Game Dev Pro<\/a>&nbsp;and Game Dev Pro Rapid Launch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pointers And References In C++<\/h3>\n\n\n\n<p>Every time you create a variable in your app or game, the computer allocates memory for that variable.<\/p>\n\n\n\n<p>Of course, depending on the type of the variable the memory will be larger or lower. And if you fill up the memory, your app or game will crash.&nbsp;This is also known as a memory leak.<\/p>\n\n\n\n<p>In C++, we use pointers and references to point to those spots in memory that holds the variable we declared.<\/p>\n\n\n\n<p>C++ is notorious for its pointers and references because after you finish using them, you need to delete them or simply said you need to destroy the spots in memory that hold the variables you\u2019re no longer using.<\/p>\n\n\n\n<p>Because if these variables pile up in the memory, you\u2019ll get a memory leak and your app or game will crash.<\/p>\n\n\n\n<p>This was done manually for many years, so developers had to be careful not to leave any variable when they were done with it so that it doesn\u2019t cause the program to crash.<\/p>\n\n\n\n<p>But over the years C++ became better at this and it was doing these operations automatically.<\/p>\n\n\n\n<p>Yet, a lot of developers don\u2019t know this and that\u2019s why they\u2019re afraid of pointers and references in C++. And they are even more afraid of pointers and references inside Unreal Engine.<\/p>\n\n\n\n<p>First, we are going to take a look at pointers and references inside pure C++. You can do that by clicking on the links below because we already covered both topics in-depth in these posts:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-pointers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pointers In C++<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-references\/\" target=\"_blank\" rel=\"noreferrer noopener\">References In C++<\/a><\/p>\n\n\n\n<p>Moving forward, now that we know how pointers and references work in C++, let\u2019s take a look at how they work inside Unreal Engine.<\/p>\n\n\n\n<p>Assuming we have a class called PlayerCharacter, this is how we would declare it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>APlayerCharacter* PlayerCharacter;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>This decalres a variable called PlayerCharacter type of APlayerCharacter class. The \u201d * \u201d denotes that this variable is a pointer.<\/p>\n\n\n\n<p>Since this variable is a pointer, we\u2019ll have to dispose of it when we stop using it in the project.<\/p>\n\n\n\n<p>Now here comes the good part. Inside Unreal Engine, all we have to do is mark that variable with the macro UPROPERTY() and this variable will be marked to participate in the memory management process:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPROPERTY()\nAPlayerCharacter* PlayerCharacter;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Just by adding this single line will make the GC or garbage collection management dispose of the PlayerCharacter variable when we stop using it in the project and we don\u2019t have to do anything else.<\/p>\n\n\n\n<p>We can also mark functions for memory management participation by using the UFUNCTION() macro:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UFUNCTION()\nvoid Attack();<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>UFUNCTION() has the same effect for a function as the UPROPERTY() has for a variable and the same rules apply.<\/p>\n\n\n\n<p>You can also use these macros to expose variables and functions to blueprints as well.<\/p>\n\n\n\n<p>For a more detailed guide on how C++ memory management works inside Unreal Engine read our short blog post below:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-memory-unreal-engine\/\" target=\"_blank\" rel=\"noreferrer noopener\">C++ Memory Management In Unreal Engine<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Classes And Objects In C++<\/h3>\n\n\n\n<p>Classes and objects are the basis of everything we do inside C++ and any other programming language.&nbsp;With classes we model the behavior and with objects we execute that behavior.<\/p>\n\n\n\n<p>We already covered C++ classes in-depth in our other blog post which you can read by clicking on the link below:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-classes-objects\/\" target=\"_blank\" rel=\"noreferrer noopener\">Classes And Objects In C++<\/a><\/p>\n\n\n\n<p>If you didn\u2019t know what are classes and objects, the explanations in the post above will make you a pro at that topic and allow you to understand this concept from the group up.<\/p>\n\n\n\n<p>Now, when we know what are classes in C++, how to create them and use them, let\u2019s take a look at how to do the same inside Unreal Engine.<\/p>\n\n\n\n<p>First, when you create a class in Unreal Engine it will prompt you to select the parent class:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-5.jpg\" alt=\"CPP In Unreal - 5\" title=\"CPP In Unreal \u2013 5\"\/><\/figure>\n\n\n\n<p>A parent class is just a class that\u2019s already defined, and when a class has a parent class that\u2019s called inheritance. We\u2019ll cover inheritance after the class part. But for know, just know that in Unreal Engine you first select the parent class.<\/p>\n\n\n\n<p>After you do that, it will take you to another window where you give the class a name and create the new class by pressing the Create Class button:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/CPP-In-Unreal-6.jpg\" alt=\"CPP In Unreal - 6\" title=\"CPP In Unreal \u2013 6\"\/><\/figure>\n\n\n\n<p>This will automatically create two files. One .h and one .cpp file for the class.<\/p>\n\n\n\n<p>This is the .h file of the PlayerCharacter class we created:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Fill out your copyright notice in the Description page of Project Settings.\n\n#pragma once\n\n#include \"CoreMinimal.h\"\n#include \"GameFramework\/Character.h\"\n#include \"PlayerCharacter.generated.h\"\n\nUCLASS()\nclass UE_CPP_API APlayerCharacter : public ACharacter\n{\n\tGENERATED_BODY()\n\npublic:\n\t\/\/ Sets default values for this character's properties\n\tAPlayerCharacter();\n\nprotected:\n\t\/\/ Called when the game starts or when spawned\n\tvirtual void BeginPlay() override;\n\npublic:\n\t\/\/ Called every frame\n\tvirtual void Tick(float DeltaTime) override;\n\n\t\/\/ Called to bind functionality to input\n\tvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;\n};<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>And this is the .cpp file:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/awesometuts.com\/unreal?utm_source=blog_unreal_m12\"><img decoding=\"async\" src=\"https:\/\/merserver.com\/wp-content\/uploads\/2022\/12\/Unreal-Middle-Ad-12.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/unreal?utm_source=blog_unreal_m12\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Fill out your copyright notice in the Description page of Project Settings.\n\n\n#include \"PlayerCharacter.h\"\n\n\/\/ Sets default values\nAPlayerCharacter::APlayerCharacter()\n{\n \t\/\/ Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.\n\tPrimaryActorTick.bCanEverTick = true;\n}\n\n\/\/ Called when the game starts or when spawned\nvoid APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n}\n\n\/\/ Called every frame\nvoid APlayerCharacter::Tick(float DeltaTime)\n{\n\tSuper::Tick(DeltaTime);\n\n}\n\n\/\/ Called to bind functionality to input\nvoid APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)\n{\n\tSuper::SetupPlayerInputComponent(PlayerInputComponent);\n\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>As we already know, inside the .h file we\u2019ll declare all the variables and functions the class will have, and inside the .cpp file we\u2019ll implement the functionality of the class.<\/p>\n\n\n\n<p>So if we want to declare two functions for the PlayerCharacter class, one called Attack and the other called Walk, this is how we would do it.<\/p>\n\n\n\n<p>First, inside the .h file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void Attack();\nvoid Walk();<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Then inside the .cpp file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void APlayerCharacter::Attack()\n{\n\t\/\/ player attack\n}\n\nvoid APlayerCharacter::Walk()\n{\n\t\/\/ player walk\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>As you can see, there\u2019s no difference how we declare and handle classes inside raw C++ and insde Unreal Engine.<\/p>\n\n\n\n<p>When we want to use the class in another class file, we\u2019d also import the .h file of the class we want to use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"PlayerCharacter.h\"<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Now because in Unreal Engine we deal with actors in the level, we don\u2019t create objects the same way we did in C++ by using the equals sign \u201d = \u201d instead we get references to the objects.<\/p>\n\n\n\n<p>But prior to that, we would have to create a reference variable inside the .h file like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>APlayerCharacter* PlayerCharacter;<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>And then, inside the .cpp file we would get a reference to the PlayerCharacter variable.<\/p>\n\n\n\n<p>Most of the time we get a reference to the variable when a collision happens, or we add the reference via the Blueprint editor.<\/p>\n\n\n\n<p>Since that\u2019s a topic on it\u2019s own, we\u2019ll not dive into it here. We\u2019ll leave links at the bottom of this post where you can learn how to do this through practice by creating a game.<\/p>\n\n\n\n<p>But for now, we\u2019ll just show that we can use the attributes of the PlayerCharacter class, the same way we used the attributes of the Player class in the C++ example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void AClassTest::BeginPlay()\n{\n\tSuper::BeginPlay();\n\n\tPlayerCharacter-&gt;Attack();\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>As you can see online 5, we\u2019re using the PlayerCharacter Attack function and any code that\u2019s inside that function would be executed when we call it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inheritance In C++<\/h3>\n\n\n\n<p>The last part of the foundational pillars of C++ coding is inheritance.<\/p>\n\n\n\n<p>Inheritance basically means we can inherit a class we already created and defined.<\/p>\n\n\n\n<p>Of course, we inherit a class by class. So, we create a new class, and inside that new class, we inherit a class we already created.<\/p>\n\n\n\n<p>The class that inherits is called a child class, and the class that\u2019s being inherited is called a parent class.<\/p>\n\n\n\n<p>The child class will inherit all public variables and functions defined in the parent class and it can use them as its own.<\/p>\n\n\n\n<p>Furthermore, it can also edit the functions of the parent class and change their functionality. Of course, this change will only be applied in the child class, it will not affect the parent class.<\/p>\n\n\n\n<p>We already covered inheritance in-depth in the post you can read by clicking the link below:<\/p>\n\n\n\n<p><a href=\"https:\/\/awesometuts.com\/blog\/cpp-inheritance\/\" target=\"_blank\" rel=\"noreferrer noopener\">Inheritance In C++<\/a><\/p>\n\n\n\n<p>The post above will allow you to understand everything about inheritance inside C++.<\/p>\n\n\n\n<p>Now, let\u2019s take a look at how inheritance works inside Unreal Engine.<\/p>\n\n\n\n<p>When we created the PlayerCharacter class, at the very beginning we were prompted to select a parent class for it.<\/p>\n\n\n\n<p>We\u2019re not obligated to inherit a class every time we create a new one inside Unreal, but most of the times the classes we create in Unreal will inherit a predefined class such as Actor, Pawn, Character and so on.<\/p>\n\n\n\n<p>This is because we\u2019ll use the classes inside the game, and for that we\u2019ll have to inherit classes which are already defined that allow us to use our own classes in the game.<\/p>\n\n\n\n<p>When we created the PlayerCharacter class, we selected that it should inherit the Character class. This is the default .h file that\u2019s generated when the PlayerCharacter class was created:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"CoreMinimal.h\"\n#include \"GameFramework\/Character.h\"\n#include \"PlayerCharacter.generated.h\"\n\nUCLASS()\nclass UE_CPP_API APlayerCharacter : public ACharacter\n{\n\tGENERATED_BODY()\n\npublic:\n\t\/\/ Sets default values for this character's properties\n\tAPlayerCharacter();\n\nprotected:\n\t\/\/ Called when the game starts or when spawned\n\tvirtual void BeginPlay() override;\n\npublic:\n\t\/\/ Called every frame\n\tvirtual void Tick(float DeltaTime) override;\n\n\t\/\/ Called to bind functionality to input\n\tvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;\n};<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>At the top on line 6, you\u2019ll see that our APlayerCharacter class is inheriting the ACharacter class. Don\u2019t worry about the A letter in front of the class, that\u2019s added by default.<\/p>\n\n\n\n<p>By inheriting the ACharacter class we also inherit all functions and variables defined in the ACharacter class, as well as all functions and variables in classes from which ACharacter class inherited prior.<\/p>\n\n\n\n<p>Hence we see functions like BeginPlay, Tick, and SetupPlayerInputComponent.<\/p>\n\n\n\n<p>And inside the PlayerCharacter .cpp file, these functions are already defined:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"PlayerCharacter.h\"\n\n\/\/ Sets default values\nAPlayerCharacter::APlayerCharacter()\n{\n \t\/\/ Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.\n\tPrimaryActorTick.bCanEverTick = true;\n}\n\n\/\/ Called when the game starts or when spawned\nvoid APlayerCharacter::BeginPlay()\n{\n\tSuper::BeginPlay();\n}\n\n\/\/ Called every frame\nvoid APlayerCharacter::Tick(float DeltaTime)\n{\n\tSuper::Tick(DeltaTime);\n}\n\n\/\/ Called to bind functionality to input\nvoid APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)\n{\n\tSuper::SetupPlayerInputComponent(PlayerInputComponent);\n}<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<p>Of course, we would have to add the code which will initialize the PlayerCharacter function and so on, but you get the point which is we can use these functions inside the PlayerCharacter class.<\/p>\n\n\n\n<p>Again, as you can see, inheritance is pretty much the same in C++ and Unreal Engine.<\/p>\n\n\n\n<p>And overall, there\u2019s a very small syntax difference between raw C++ and C++ used in Unreal Engine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reading Time:&nbsp;10&nbsp;minutes Level: Beginner \u2013 Intermediate \u2013 Advanced Version: Any Unreal Engine Version Help Others Learn Game Development C++ is the most effective and widely used programming language for creating video games. It\u2019s also the language Unreal Engine, arguably the most popular game engine in the world, uses too. However, there are some differences between&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"http:\/\/merserver.com\/index.php\/2022\/12\/18\/the-complete-c-guide-for-unreal-engine\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;The Complete C++ Guide For Unreal Engine&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-unreal-engine"],"_links":{"self":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":2,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":543,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/posts\/530\/revisions\/543"}],"wp:attachment":[{"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/merserver.com\/index.php\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}