Chapter 4. Unreal Engine, C++, and You

Welcome to Chapter 4! Well done on working through to this point, we are finally ready to open Pandora's box and bring C++ into our Unreal Engine workflow! Unreal Engine has been designed specifically to allow game developers to utilize C++ to its full extent when creating content. By the end of this chapter, you will be able to leverage the low-level nature of C++ to create in-depth functionality that maximizes the potential of UE4. In fact, from this point forward, C++ will be our main avenue when it comes to creating functionality. If you do not already have a basic understanding of, or experience with, C++, I strongly recommend brushing up on the C++ programming language and Visual Studio 2015 IDE before continuing with this book.

This chapter will introduce you to the concepts and syntax that you will encounter when working with C++ and UE4. The relationship they have created between codebase and engine is robust and involved, this chapter will act as an introduction to the workings of this relationship. Through the remaining chapters we will continuously build on this knowledge so that you may master C++ with UE4. However, with power comes responsibility: C++ is a low-level language by nature, therefore by using it you are also exposing your projects to vulnerabilities brought about by buggy and incomplete code. The code provided in this and the following chapters will take steps to show you how to avoid these vulnerabilities.

In short, this chapter will cover the following points:

  • Why you should use C++ with Unreal Engine 4
  • How UE4 utilizes polymorphism and virtual inheritance
  • How to create an UE4 C++ project
  • How to read UE4 C++ code
  • What is a pre-compile stage and how does Unreal Engine 4 utilize this?
  • What are UE macros?
  • How to write functions in C++ that the engine can see
  • How to create C++ objects that can be seen by UE
  • How to write C++ functions that need to be defined in Blueprints

UE and C++

UE was designed specifically with C++ development in mind. In the history of UE, you traditionally only had access to UnrealScript; unless you paid a great deal of money to become an UE developer. UnrealScript is a scripting language developed by the Engine team to allow you to create high-level content that would interface with the Engine. The purpose of this script was to provide a layer of abstraction between the developer and engine. You could say that Blueprint has largely replaced UnrealScript as the primary scripting tool for UE. UE4 is unique in that it provides immediate and direct access to C++ source and development to all users.

The engine itself was written in C++ and to get access to this source all you need to do is sign up to GitHub and register your GitHub login with your Unreal developer account. Through GitHub you will then be able to download and compile the latest version of the engine source. This design of made with and for C++ means that C++ should be your first option when developing with the engine. This chapter will not only detail when and why you should use C++ over Blueprint, but also when you should leverage the communication between the two to enhance the efficiency of your UE workflow.

Visual Studio is the IDE you must use when working with UE. Fear not though, installing the latest version of the engine also includes an installation of the 2015 community edition of Visual Studio. The engine also provides many Visual Studio integration tools that synergize the IDE with the engine. The steps to properly set up your Visual Studio with Unreal Engine can be found here at https://docs.unrealengine.com/latest/INT/Programming/Development/VisualStudioSetup/index.html.

The previous tutorials and guides are a fantastic resource to expand any knowledge you gain over the course of this book.

Why use C++

C++ is a very powerful programming language with many low-level tools that you can utilize as a developer. If you are already familiar with the language you will know of the advantages it provides when it comes to memory management, generic codebase via templated programming, and the power of OOP (Object Oriented Programming) using virtual inheritance and polymorphism. If you are unaware of those concepts I would strongly suggest that you go do some separate research on C++ and how to use it, as these concepts will be assumed as known when covering further topics. I find the following website to be a great resource for introductory C++ concepts—http://www.cplusplus.com/doc/tutorial/.

Knowing how to use C++ with UE4 is essential to becoming a skilled and competent UE developer. This knowledge will allow you to fully utilize the power and limitless potential of UE. This is not hyperbole either, as not only can you use C++ to create content on a project-by-project basis, but you may also edit and add to the engine's C++ source code to create core engine functionality and adaptions you feel are lacking by default.

The biggest advantage of using C++ as your main development tool instead of Blueprint is stability. During the course of a project, you may wish to update engine versions to gain access to up-to-date iterations of engine functionality. If the implementation of your project is heavily based in Blueprint, this upgrade can lead to very serious build-stability issues. As Blueprint is constantly changing and updating with each iteration of the engine, large, core portions of Blueprint may be subject to change.

This happens far less frequently in code and, when it does, it is much easier to debug so that you may make amendments to fix any issues that arise. With regards to debugging, using C++ allows us to debug UE code much the same as you would any other Windows application codebase. We are able to place break points, utilize the call stack, scrutinize memory states, observe variable states at runtime, place data break points, and any other C++ debugging features we are used to.

Another advantage is the speed at which you can implement new functionality using the various interfaces the engine provides to C++ developers. If you are already very familiar with the language, you will relish the toolset provided by the engine and begin to thoroughly enjoy your time working with it. It may even come down to development preference, I personally favor C++ when developing with the engine as that is what I am comfortable with.

Now, only using C++ to create your project is not advised as there are definitely some things that Blueprint does much better than C++. When it comes to establishing object associations and asset assignment, it is far easier to do this in Blueprint as you have the power of the Editor at your disposal. This book will teach you how to effectively combine C++ with the other toolsets of the engine to maximize your development capabilities.

Polymorphism, virtual inheritance and templates

If you do not already know what polymorphism, virtual inheritance or templates are, your C++ journey with UE will be confusing and difficult. Using the link provided previously and the power of Google I would strongly recommend learning up on these topics before continuing with this book.

All of the UE objects that you interact with will have some base established in C++ code. In turn, most of these objects will also feature virtual functionality thus extensibility via C++ code. This will be your main entry point into the Unreal codebase. For example, when you wish to create a character object in code, the first thing you will do is create a class that inherits from UCharacter, thus becoming a child class of UCharacter. This will give you access to the various interfaces, methods, and variables that make up the UCharacter base class.

Through the virtual functions featured in base classes we will be able to have the engine invoke and execute our own custom functionality in-place of, or appending what exists in, the base class. Each facet of integration will be detailed as we progress through the following chapters. The first thing you will notice is that your new custom classes can be seen by the engine, meaning you can create Blueprint abstractions of your custom classes!

The other major C++ feature that UE utilizes is templatization. When adding other engine-based components and accessing Unreal Engine functionality, you will be utilizing UE's template objects and template functions. These template objects and functions extend UE4's customizable nature by providing us with generic feature sets that will be compatible with our custom C++ objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.71.189