UE5 C++ | The basics
Tags: ue, unrealengine, gamedev, unreal
Published: 2023-06-06 09:06:17


Unreal Engine 5 (UE5) is a powerful game engine used to create some of the most visually stunning video games of our
time. If you're an aspiring game developer, the choice between C++ and Blueprint within UE5 can be a crucial one.

Blueprint, UE5's visual scripting language, lets you construct game logic without writing code, making it a fantastic
tool for prototyping. On the other hand, C++ is a full-fledged programming language that offers advanced capabilities
and superior flexibility. Despite its steeper learning curve, C++ grants you more control over game performance and
optimization, a trade-off that could be well worth the investment of your time.

To help you make this important decision, this article series will delve into the nuances of UE5 C++. While there may
not be a wealth of Unreal Engine Library C++ tutorials out there, we'll strive to fill that gap and share our insights
from navigating the UE5 C++ landscape. Strap in as we embark on this exciting journey together!

C++ is notorious for being a challenging language to master, but your determination to bring your own game to life can
fuel your learning. While we won't be teaching C++ from scratch, we'll provide you with essential resources and lay the
groundwork to help you write your own code. Enhancing your C++ skills will not only increase your comfort but also boost
your problem-solving ability. Just remember, this series is intended to complement other courses or books on C++.

## Project Creation

Starting a new C++ project in Unreal Engine 5 is a fairly straightforward process:

1. Open the Epic Games Launcher and navigate to the Unreal Engine tab.
2. Click on the "+ New" button near the top of the screen.
3. Select "C++" as the project type and choose the "Blank" template.
4. Choose a suitable name for your project and select the location where the project files will be stored.
5. Make sure the "With Starter Content" option is selected if you want some assets to play around with, or choose "No
   Starter Content" for a completely empty project.

And that's it! UE5 will now create a new project with some basic C++ code and Unreal-specific project files.

## Project Structure

Upon creating a new C++ project, you'll see a variety of files and folders that Unreal Engine automatically generates
for you. Understanding these files and their purposes is key to mastering UE5 C++ development. Here are the key files
and directories:

- `YourProjectName/` - This is your project root directory.
    - `Binaries/` - This folder contains all the binary files generated during the project's build process.
    - `Config/` - Here you can find configuration files (.ini) that control various aspects of your game.
    - `Content/` - This folder holds all your game's assets like 3D models, textures, sounds, etc.
    - `Source/` - This is where you'll find the source code for your game. Your main game module and any additional
      modules you create will be located here.
    - `YourProjectName.uproject` - This file contains a JSON object that holds all the configuration and settings for
      your project.

## What IDE to Choose

When it comes to choosing an Integrated Development Environment (IDE) for UE5 and C++, there are a few viable options:

- **Visual Studio**: This is the most widely used IDE for Unreal Engine development. Visual Studio offers robust
  debugging tools, IntelliSense for auto-completion, and seamless integration with the UE5 environment. Be sure to
  install the "Game development with C++" workload and the "Unreal Engine Installer" option for the best experience.

- **Visual Studio Code**: VS Code is a lightweight, open-source code editor that can be extended into a fully-fledged
  IDE with extensions. For UE5 C++ development, you'll want to install the "C/C++", "C++ Intellisense", and "Unreal
  Engine C++ Helper" extensions to get syntax highlighting, auto-completion, and other useful features.

- **Rider for Unreal Engine**: This is a relatively new option from JetBrains, the creators of IntelliJ IDEA and
  PyCharm. It offers deep integration with Unreal Engine, extensive language support, a powerful debugger, and a variety
  of other tools. It's worth noting that Rider for Unreal Engine is free to use while it's in its preview stage.

Each of these IDEs has its strengths, so the choice mainly depends on your personal preference and the needs of your
project. It's recommended to try out each one to see which you prefer. Happy coding!

## Hello World!

In a game engine context like UE5, the traditional "Hello, World!" can take on a slightly different form. We will show
the message both in the game console and on the player's screen upon spawning.

Firstly, locate the player's character class. It's usually named something like `YourProjectNameCharacter.cpp`.

Open this file, and within the class constructor, we're going to use `UE_LOG` to print "Hello, World!" to the console:

```cpp
#include "YourProjectNameCharacter.h"

AYourProjectNameCharacter::AYourProjectNameCharacter()
{
    UE_LOG(LogTemp, Warning, TEXT("Hello, World!"));
}
```

In this snippet, `UE_LOG` is a macro that outputs a message to the UE log system. The first parameter `LogTemp` is the
log category, `Warning` is the severity of the message, and `TEXT` is a macro to support internationalization and is
required for all strings in UE.

Next, to display "Hello, World!" on the player's screen, you will need to use the `GEngine`'s `AddOnScreenDebugMessage`
method. This can also be done in the player's character constructor:

```cpp
AYourProjectNameCharacter::AYourProjectNameCharacter()
{

    UE_LOG(LogTemp, Warning, TEXT("Hello, World!"));

		// Checking if GEngine is not null is important, it could crash otherwise, and that's not the behaviour we want.
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Hello, World!"));
    }
}
```

In this code, `AddOnScreenDebugMessage` is a function that displays a debug message on the screen. Here's what the
parameters mean:

- The first parameter, `-1`, is the key used to identify the message on the screen. `-1` is commonly used for temporary
  messages, while other numbers can be used for messages you want to update or clear manually.
- `5.f` is the time in seconds that the message will be displayed on the screen.
- `FColor::Red` is the color of the displayed text.
- `TEXT("Hello, World!")` is the message to display.

[AddOnScreenDebugMessage's method documentation — Unreal Engine Documentation](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UEngine/AddOnScreenDebugMessage/1/)

With this in place, every time a player character is created in your game, "Hello, World!" will be printed both to the
UE log console and as a message on the player's screen!

This series of articles is here to provide you with a solid foundation for your endeavors. As we explore more complex
concepts like the Entity Component System, smart pointers, and garbage collection in upcoming articles, you'll
continuously expand your skill set.

While C++ in UE5 may seem complicated to understand at first, remember that it's a powerful tool that can offer you
unparalleled control and efficiency in your game development process. With each hurdle overcome, you'll find yourself
one step closer to realizing your game development dreams. Remember that every line of code you write is progress.

Stay tuned for more, and happy coding!

https://agdl.dev