TIL: Learn to Code in C++ by Developing Your First Game (Section 2, Part 4)

Wow, it’s been over a month since I last tried to follow along with this course, and there’s been an entire new section released in the meantime. Now that I’ve cleared some other things off my to-do list I’m planning on focusing on clearing my way through learning this material and then trying to put some of this newly gained knowledge to the test. Time to get crackin’!

Section: 2 – Bulls & Cows Word Game – Your First C++

Lecture 30: Using using for Type Aliases

Apparently UE has some lines of code that it uses… namely instead of integers it uses int32 and instead of strings, it uses FText. It seems the idea in this video will be to switch from the “standard” C++ code over to something more similar to UE4’s style.

int is not used by all platforms, so we want to use int32 to aid in cross platform development… phones vs 64bit windows are different beasts, so we sometimes need to be specific as to what type of data we’re working with. We also use fstring / ftext. FText tends to be used for user output / interaction.

The first step is to enter the following text after the #includes in main.cpp:

using FText = std::string;

Then replace all instances of “std::string” with “FText”.

We also replace all calls of int with int32 (except for int main() as this is called by the OS).

When doing search and replace (ctrl +h) be sure to set it to Entire Solution to ensure that you don’t miss documents.

You’ll also need to explicitly use “using int32 = int;” in any document that requires the substitution.

Lecture 31: Using struct for Simple Types

A struct is the same as a class, except that it’s members are public.

One very handy feature in visual studio, is that comments can get pulled in when mouse over variables.

In this case, our magic word is a magic word. We set it up to use a constexpr FString since it is known at compile time.

const FString HIDDEN_WORD = "planet";

Because we’re following Unreal Engine standards when coding, the structs should make use of the F prefix. So when we create a struct call BullCowGame, we should actually be making it FBullCowGame.

// receives a VALID guess, increments turn, and returns count.
FBullCowCount FBullCowGame::SubmitGuess(FString) {
// increment the turn number
MyCurrentTry++;

// set-up a return variable
FBullCowCount BullCowCount;

// loop through all letters in the guess and for each letter
// compare letters against the hidden word

return BullCowCount;
}

There is a thread about that on the Unreal forums: here. Where it is explained that:

“Structs are prefixed with F, e.g. FString, FHitResult.
The F doesn’t really stand for anything. The first struct in the codebase was FVector, which was prefixed with F because it was a vector of floats, and the trend caught on.

There’s also an S prefix for Slate widgets (e.g. SListView, SInGameScoreboardWidget)”

Remember to update the F -everywhere-. Not just in one of the files.

Lecture 32: Using if Statements in C++

If statements are pretty common … they’re used to create a condition and what happens if the condition is either true or false. We will use switches for simple logic (if 1 do this, if 2 do this, if 3 do this, etc), but if statements may be dependent on more elaborate logic.

We’ll set up an if statement, but it will be inside of a for statement. So we need to create something like :

int32 HiddenWordLength = MyHiddenWord.length();
for (int32 i = 0; i < HiddenWordLength; i++) {}

Remember that we can access intrinsic functions like length() with the dot notation. Type your string, type ".", and then get access to a bunch of string specific functions, then close it up with "()".

The logic will be:
for(loop all letters of word once to get our letter that we will be comparing){for(loop all letters of word to get our letter that we will compare against){if(do our comparison){}}}.

Remember to also create output for whatever we're doing inside our main.cpp

It will look like:

// submit valid guess to the game
FBullCowCount BullCowCount = BCGame.SubmitGuess();

Back in FBullCowGame.cpp, the if statements are pretty straight forward: if (variable == variable2) {BullCowCount.Bulls++;}

Ben intentionally introduced some bugs into his code here, so in favour of keeping the blog cleaner, I'll wait until finding out the solutions before recording anything here.

Lecture 33: Debugging 101

i, and j, etc are common in for loops but can be ambiguous. It can make things more clear if you use an actual variable such as GuessChar, or HiddenWordChar.

Debug

When I attempt to do the debugging, I end up getting the above error. It looks like it's related to the fact that I'm guessing a word that is shorter in length than the hidden word. Comments in the Udemy video suggest that this will be corrected in a later video.

What I've ended up placing in deviates a bit, but I think it addresses the issue of word length:

WordLength

Lecture 34: A Place for Everything

The idea here, is that with things like the HiddenWordLength, we don't want to have to figure it out multiple times, nor do we want to hard code it. So we try to place things where it makes the most sense so that we can optimize our workflow.

    There is a place for everything, and everything should be in its place
    Think carefully about what you store
    General bandwidth vs. storage
    Unless necessary, don't store results
    Store the birthday, but not the age
    http://gameprogrammingpatterns.com/data-locality.html

This tends to also mean that we try to arrange all getters together, all constants together etc so that it's easy for us to find information when we want to tweak it.

It also means that it's good to get rid of obsolete code rather than keeping it around forever.