C++ and The Unreal Engine (cont.)

Continuing to learn more and work within the Unreal Engine I have learned a few different things this week that are and will become very useful in many projects. Some smaller things I learned like about the Dot Operator (.) within C++. This Dot Operator works the same as it does in other languages where it is used to access member functions from a class or a struct. So for example if we had a string variable and wanted to know the length of the string we would use the Dot Operator to call the Len() function which would return an int32 . I started to use this Dot Operator when I need to start incorporating “Lives” into the newest game I’m building, like so :

HiddenWord = TEXT("Hello");
Lives = HiddenWord.Len();

The lives are based on how long the word is so using HiddenWord.Len() will allow the length of lives to always change according to the HiddenWord. In the game the HiddenWord is randomized the longer the word the more chances the player will have. The other simple thing that I learned about was having early returns in my if/else statements. Prior to learning about the early returns the game loop used to run the game was just a giant if/else statement that had multiple nested if/else statements within itself and it was just very confusing to read and keep track of. The previous if/else statement that I had within my game previous looked something like this :

if (Guess == HiddenWord)
{
PrintLine(TEXT("You have won!"));
EndGame();
}
else
{
PrintLine(TEXT("You have lost a life"));

--Lives;
if (Lives > 0)
{
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Guess again!\nYou have %i lives left"), Lives);
}
}
else
{
PrintLine(TEXT("You have no lives left");
EndGame();
}
}

There is two if statements within an else statement and another else statement and once it gets to be a bigger project or game this would get very difficult to keep track of. When your program is running through a series of checks like mine is doing where it goes from checking to see if the words are the same to then checking to see if they are the same length and also checking to see if there are lives left to continue playing. Using early returns we can break this up into much simpler and easier to read code. This if/else statement is within my ProcessGuess function and after implementing early returns my ProcessGuess function looks like this :

if (Guess == HiddenWord)  // Checks to see if word is correct
{
PrintLine(TEXT("You have won!");
EndGame();
return;
}

Simply putting return at the end of the if statements will end the process if the that if statement returns true. So checking if the Guess is the same as the HiddenWord at the beginning will end the ProcessGuess function because of the early return statement at the end of the that if statement. If it does not return true that Guess == HiddenWord then the function will continue down the code and move on to the next if statement which will test to see if the length is correct, and so on until it goes through the entire process if needed. It is just a simpler way to write big long if statements to avoid having multiple nested if statements and making much harder to debug and read.

If you noticed some funny looking code in that example, Lives ( --Lives ) this is taking advantage of using the Pre Decrement Operator. There is a Pre Increment / Decrement Operator as well as a Post Increment / Decrement Operator. They have different uses for both aparat from the obvious Increment ( ++ ) and Decrement ( -- ). The Pre Increment / Decrement Operator is used when you want to Increment or Decrement the value of a variable before it is used in an expression. The Post Increment / Decrement Operator is used when you want to Increment or Decrement the value of a variable after it is used in an expression. So for example :

// Pre Increment

The output of this code will be 2 , 2 . Because we put the Increment Operator ( ++ ) before the variable this makes it a Pre Increment. If we were to do the same example but instead of having ++a we use a++ the output would be different. The output would then be 2, 1 , because it will return the value of a first before incrementing it. It is a little tricky and having it as a Post Increment could be giving you an outcome on a value that you aren’t expecting. As I continue to work on this game and get it finished I’m learning more and more things that can be done using C++.

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!