Unreal Engine : Beginning C++

In order to understand Unreal Engine you need to know some C++. There’s no getting around this. You can use Blueprints and craft a great game with them, however, as this is a coding blog I will be focusing on C++. Using Blueprints can only take you so far as their functionality is limited.

Setting up our Project

Our first C++ program will be written outside UE4. I will be providing instructions on Windows and using Visual Studio Community Edition. This can be found at the official Microsoft site.

Once you have installed Visual Studio, open it and work through the following steps to get to a point where you can begin to code.

From the File menu, select New Project as shown in the following screenshot

You will get the following dialog:

There are five things to examine in the image above:

  1. Select Visual C++ from the left-hand side panel.
  2. Select Win32 Console Application from the right-hand side panel.
  3. Name your app (This one is named MyFirstApp)
  4. Select a folder to save your code
  5. Click on the OK button

After this an Application Wizard dialog box opens up, as shown in the following screenshot:

  1. Click on Application Settings in the left-hand side panel
  2. Ensure that Console application is selected
  3. Select Empty Project
  4. Click on Finish

Now we are in the Visual Studio environment. This is the place where you will do all your work and code.

However, we need a file to write our code into. So, we will add a C++ code file into our project, as show below.

Add your new source code file as shown in the following screenshot:

You will now edit Source.cpp.

Creating your first C++ Program

We are now going to write some C++ source code. There’s a good reason its called source code: it is the source from which we all build our binary executable code. The same C++ source code can be built on different platforms such as Mac, Windows and iOS, and in theory, an executable code doing the exact same things on each respective platform should result.

Followers of my blog will know that in the not-so-distant past, before the introduction of C and C++, programmers wrote code for each specific machine that they were targeting individually. They wrote code in a language called assembly. But with C and C++ available, a programmer only has to write code once and it can be deployed to a number of different machines by sending code through different compilers.

Write the following code in Visual Studio

Press Ctrl + F5 to run the preceding code in Visual Studio.

The first time you press Ctrl + F5 in Visual Studio you will see this dialog:

Select Yes and Do not show this dialog again.

Understanding this Code

Let’s interpret this program, starting from the first line:

#include <iostream>

This line has two important points to be noted:

  1. The first thing we see is the #include statement. We are asking C++ to copy and paste the contents of another C++ source file, called <iostream> directly into our code file. The <iostream> is a standard C++ library that handles all the code that lets us print text to the screen.
  2. The second thing we notice is a // comment. C ++ ignores any text after a double slash until the end of that line. Comments are very useful to ass in plain text explanations of what the code does.

The next line of code – using namespace std lets you use a shorthand (for example cout) instead of the fully qualified name (which in this case would be std::cout) for a lot of our C++ code commands.

int main() is the application’s starting point. You can think of main as the way of how C++ program knows where to start. If you don’t have an int main() program marker or if main is spelled incorrectly, then your program just won’t work, because the program wont know where to start.

The next two lines print text to the screen:

cout << “Hello, world” << endl;

cout << “I am now a C++ programmer.” << endl;

The cout statement stands for console output. Text between double quotes will get an output to the console exactly as it appears in the quotes. You can write anything you want between double quotes except a double quote and it will still be valid code.

The last line of the program is the return statement:

return 0;

This line of code indicates that the C++ program is quitting. This statement is effecitively returning to the operating system.

Semicolons

Semicolons (;) are important in C++ programming. Notice in the preceding code example that most lines of code end in a semicolon. If you don’t end each line with a semicolon, your code will not compile.

Well, there you go, that’s our first piece of code created!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s