36,59 €
Write efficient, reusable scripts to build custom characters, game environments, and control enemy AI
Key Features
Book Description
Unreal Engine 4 (UE4) is a popular and award-winning game engine that powers some of the most popular games. A truly powerful tool for game development, there has never been a better time to use it for both commercial and independent projects. With more than 100 recipes, this book shows how to unleash the power of C++ while developing games with Unreal Engine.
This book takes you on a journey to jumpstart your C++ and UE4 development skills. You will start off by setting up UE4 for C++ development and learn how to work with Visual Studio, a popular code editor. You will learn how to create C++ classes and structs the Unreal way. This will be followed by exploring memory management, smart pointers, and debugging your code. You will then learn how to make your own Actors and Components through code and how to handle input and collision events. You will also get exposure to many elements of game development including creating user interfaces, artificial intelligence, and writing code with networked play in mind. You will also learn how to add on to the Unreal Editor itself.
With a range of task-oriented recipes, this book provides actionable information about writing code for games with UE4 using C++. By the end of the book, you will be empowered to become a top-notch developer with UE4 using C++ as your scripting language!
What you will learn
Who this book is for
If you are really passionate game developer looking for solutions to common scripting problems, then this is the book for you. Understanding of the fundamentals of game design and C++ is expected to get the most from this book.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 479
Veröffentlichungsjahr: 2019
Copyright © 2019 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Acquisition Editor:Larissa PintoContent Development Editor:Arun NadarTechnical Editor:Rutuja VazeCopy Editor: Safis EditingProject Coordinator:Kinjal BariProofreader: Safis EditingIndexer:Rekha NairGraphics:Alishon MendonsaProduction Coordinator:Tom Scaria
First published: October 2016 Second edition: March 2019
Production reference: 1290319
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78980-950-3
www.packtpub.com
John P. Doran is a passionate and seasoned technical game designer, software engineer, and author based in Peoria, Illinois.
For over a decade, John has gained extensive hands-on expertise in game development, working in various roles ranging from game designer to lead UI programmer. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development.
John is currently an instructor in residence at Bradley University. Prior to his present ventures, he was an award-winning videographer.
William Sherif is a C++ programmer with more than eight years' experience in the programming world, ranging from game programming to web programming. He also worked as a university course instructor (sessional) for seven years. Additionally, he released several apps on the iTunes store, including Strum and MARSHALL OF THE ELITE SQUADRON. In the past, he has won acclaim for delivering course material in an easy-to-understand manner.
Stephen Whittle is a game developer and educator with nearly 10 years' development experience, most of which has been done using the Unreal Engine. He is a community contributor to the engine, having features or bug fixes included in almost every major version of the engine since its public release.
Agne Skripkaite is a UE4 software engineer with a particular interest in Virtual Reality (VR) applications. They have a BSc physics degree with honors from the University of Edinburgh and became a full-time engineer partway through a physics PhD program at Caltech. Over the last few years, Agne has developed for room-scale and seated VR games as part of two-engineer team, along with working in large development teams. They have also served as a user comfort and motion sickness mitigation expert for seated VR applications.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Title Page
Copyright and Credits
Unreal Engine 4.x Scripting with C++ Cookbook Second Edition
Contributors
About the authors
About the reviewer
Packt is searching for authors like you
www.PacktPub.com
Why subscribe?
Packt.com
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Get in touch
Reviews
UE4 Development Tools
Introduction
Technical requirements
Installing Visual Studio
Getting ready
How to do it...
How it works...
Creating and building your first C++ project in Visual Studio
Getting ready
How to do it...
How it works...
There's more...
Changing the code font and color in Visual Studio
Getting ready
How to do it...
How it works...
There's more...
See also
Extension – changing the color theme in Visual Studio
How to do it...
How it works...
Formatting your code (Autocomplete settings) in Visual Studio
Getting ready
How to do it...
How it works...
Shortcut keys in Visual Studio
Getting ready
How to do it...
How it works...
Extended mouse usage in Visual Studio
How to do it...
How it works...
Installing Unreal Engine 4 (UE4)
Getting ready
How to do it...
How it works...
There's more...
Creating your first project in UE4
How to do it...
Creating your first level in UE4
Getting ready
How to do it...
UE4 – hot reloading
Getting ready
How to do it...
UE4 – logging with UE_LOG
Getting ready
How to do it...
How it works...
UE4 – making an FString from FStrings and other variables
Getting ready
How to do it...
Creating Classes
Introduction
Technical requirements
Making a UCLASS – deriving from UObject
Getting ready
How to do it...
How it works...
There's more...
See also
Creating a blueprint from your custom UCLASS
Getting ready
How to do it...
How it works...
Creating a user-editable UPROPERTY
Getting ready
How to do it...
How it works...
There's more...
See also
Accessing a UPROPERTY from blueprints
How to do it...
How it works...
Specifying a UCLASS as the type of a UPROPERTY
Getting ready
How to do it...
How it works...
TSubclassOf
FStringClassReference
Instantiating UObject-derived classes (ConstructObject< > and NewObject< >)
Getting ready
How to do it...
How it works...
There's more...
Destroying UObject-derived classes
Getting ready
How to do it...
How it works...
Creating a USTRUCT
Getting ready
How to do it...
How it works...
There's more...
Creating a UENUM( )
How to do it...
How it works...
Memory Management, Smart Pointers, and Debugging
Introduction
Technical requirements
Unmanaged memory – using malloc( ) / free( )
How to do it...
How it works...
Unmanaged memory – using new/delete
Getting ready
How to do it...
How it works...
There's more...
Managed memory – using NewObject< > and ConstructObject< >
Getting ready
How to do it...
How it works...
There's more...
See also
Managed memory – de-allocating memory
Getting ready
How to do it...
How it works...
Managed memory – smart pointers (TSharedPtr, TWeakPtr, TAutoPtr) to track an object
Getting ready
How to do it...
How it works...
There's more...
Using TScopedPointer to track an object
Getting ready
How to do it...
How it works...
Unreal's garbage collection system and UPROPERTY( )
How to do it...
How it works...
Forcing garbage collection
Getting ready
How to do it...
Breakpoints and stepping through code
Getting ready
How to do it...
How it works...
Finding bugs and using call stacks
Getting ready
How to do it...
How it works...
Using the profiler to identify hot spots
How to do it...
How it works...
Actors and Components
Introduction
Technical requirements
Creating a custom Actor in C++
Getting ready
How to do it...
How it works...
Instantiating an Actor using SpawnActor
How to do it...
How it works...
Creating a UFUNCTION
How to do it...
How it works...
Destroying an Actor using Destroy and a Timer
How to do it...
How it works...
Destroying an Actor after a delay using SetLifeSpan
How to do it...
How it works...
Implementing the Actor functionality by composition
Getting ready
How to do it...
How it works...
See also
Loading assets into components using FObjectFinder
Getting ready
How to do it...
How it works...
Implementing the Actor functionality by inheritance
How to do it...
How it works...
See also
Attaching components to create a hierarchy
How to do it...
How it works...
Creating a custom Actor Component
How to do it...
How it works...
Creating a custom Scene Component
How to do it...
How it works...
See also
Creating an InventoryComponent for an RPG
Getting ready
How to do it...
How it works...
See also
Creating an OrbitingMovement Component
How to do it...
How it works...
Creating a building that spawns units
How to do it...
How it works...
Handling Events and Delegates
Introduction
Technical requirements
Handling events that have been implemented via virtual functions
How to do it...
How it works...
There's more...
Creating a delegate that is bound to a UFUNCTION
Getting ready
How to do it...
How it works...
See also
Unregistering a delegate
Getting ready
How to do it...
How it works...
Creating a delegate that takes input parameters
Getting ready
How to do it...
How it works...
See also
Passing payload data with a delegate binding
Getting ready
How to do it...
How it works...
See also
Creating a multicast delegate
Getting ready
How to do it...
How it works...
Creating a custom Event
Getting ready
How to do it...
How it works...
Creating a Time of Day handler
How to do it...
How it works...
Creating a respawning pickup for a First Person Shooter
How to do it...
How it works...
Input and Collision
Introduction
Technical requirements
Axis Mappings – keyboard, mouse, and gamepad directional input for an FPS character
Getting ready
How to do it...
How it works...
See also
Axis Mappings – normalized input
Getting ready
How to do it...
How it works...
Action Mappings – one-button responses for an FPS character
Getting ready
How to do it...
How it works...
See also
Adding Axis and Action Mappings from C++
Getting ready
How to do it...
How it works...
Mouse UI input handling
Getting ready
How to do it...
How it works...
UMG keyboard UI shortcut keys
Getting ready
How to do it...
How it works...
Collision – letting objects pass through one another using Ignore
Getting ready
How to do it...
How it works...
Collision – picking up objects using Overlap
Getting ready
How to do it...
How it works...
Collision – preventing interpenetration using Block
Getting ready
How to do it...
How it works...
There's more...
Communication Between Classes and Interfaces: Part I
Introduction
Technical requirements
Creating a UInterface
How to do it...
How it works...
See also
Implementing a UInterface on an object
Getting ready
How to do it...
How it works...
Checking if a class implements a UInterface
Getting ready
How to do it...
How it works...
See also
Casting to a UInterface implemented in native code
Getting ready
How to do it...
How it works...
Calling native UInterface functions from C++
Getting ready
How to do it...
How it works...
See also
Inheriting UInterfaces from one another
How to do it...
How it works...
Overriding UInterface functions in C++
Getting ready
How to do it...
How it works...
See also
Implementing a simple interaction system with UInterfaces
Getting ready...
How to do it...
How it works...
Communication Between Classes and Interfaces: Part II
Introduction
Technical requirements
Exposing UInterface methods to Blueprint from a native base class
How to do it...
How it works...
See also
Implementing UInterface functions in Blueprint
How to do it...
How it works...
See also
Overriding C++ UInterface functions through Blueprints 
How to do it...
How it works...
Calling Blueprint-defined interface functions from C++
How to do it...
How it works...
Integrating C++ and the Unreal Editor: Part I
Introduction
Technical requirements
Using a class or struct as a blueprint variable
How to do it...
How it works...
There's more...
Creating classes or structs that can be subclassed in Blueprint
How to do it...
How it works...
Creating functions that can be called in Blueprint
How to do it...
How it works...
See also
Creating events that can be implemented in Blueprints
How to do it...
How it works...
Exposing multi-cast delegates to Blueprint
How to do it...
How it works...
See also
Creating C++ enums that can be used in Blueprint
How to do it...
How it works...
Editing class properties in different places in the editor
How to do it...
How it works...
See also
Making properties accessible in the Blueprint editor graph
How to do it...
How it works...
Responding to property changed events from the editor
How to do it...
How it works...
Implementing a native code Construction Script
How to do it...
How it works...
Integrating C++ and the Unreal Editor: Part II
Introduction
Creating a new editor module
How to do it...
How it works...
Creating new toolbar buttons
How to do it...
How it works...
Creating new menu entries
How to do it...
How it works...
Creating a new editor window
How to do it...
How it works...
See also
Creating a new Asset type
How to do it...
How it works...
See also
Creating custom context menu entries for Assets
How to do it...
How it works...
Creating new console commands
Getting ready
How to do it...
How it works...
See also
Creating a new graph pin visualizer for Blueprint
How to do it...
How it works...
Inspecting types with custom Details panels
How to do it...
How it works...
Working with UE4 APIs
Introduction
Technical requirements
Core/Logging API – defining a custom log category
Getting ready
How to do it...
How it works...
There's more...
Core/Logging API – FMessageLog to write messages to the Message Log
Getting ready
How to do it...
How it works...
Core/Math API – rotation using FRotator
Getting ready
How to do it...
Core/Math API – rotation using FQuat
Getting ready
How to do it...
How it works...
There's more...
API – rotation using FRotationMatrix to have one object face another
Getting ready
How to do it...
How it works...
GameplayAbilities API – triggering an actor's gameplay abilities with game controls
Getting ready
How to do it...
How it works...
There's more...
See also
GameplayAbilities API - Implementing stats with UAttributeSet
Getting ready
How to do it...
How it works...
There's more...
GameplayAbilities API – implementing buffs with GameplayEffect
Getting ready
How to do it...
How it works...
There's more...
GameplayTasks API – making things happen with GameplayTasks
Getting ready
How to do it...
How it works...
There's more...
HTTP API – downloading webpages using web requests
Getting ready
How to do it...
How it works...
There's more...
HTTP API – displaying downloaded progress 
Getting ready
How to do it...
How it works...
Multiplayer Networking in UE4
Introduction
Technical requirements
Testing your game as a client and a server simultaneously
How to do it...
How it works...
Replicating properties over the network
How to do it...
How it works...
There's more...
Replicating functions over the network
How to do it...
How it works...
See also...
Handling UI network events
Getting ready...
How to do it...
How it works...
See also...
AI for Controlling NPCs
Introduction
Technical requirements
Implementing a simple following behavior
Getting ready
How to do it...
How it works...
Laying down a Navigation Mesh
Getting ready
How to do it...
How it works...
Creating a Blackboard
How to do it...
How it works...
Creating a Behavior Tree
How to do it...
How it works...
Connecting a Behavior Tree to a Character
Getting ready
How to do it...
How it works...
Creating a BTService
Getting ready...
How to do it...
How it works...
Creating a BTTask
Getting ready...
How to do it...
How it works...
User Interfaces - UI and UMG
Introduction
Technical requirements
Drawing using Canvas
Getting ready...
How to do it...
How it works...
See also...
Adding Slate Widgets to the screen
Getting ready
How to do it...
How it works...
Creating screen size-aware scaling for the UI
Getting ready
How to do it...
Using the In-Editor method
Using the Config file method
How it works...
See also
Displaying and hiding a sheet of UMG elements in-game
How to do it...
How it works...
Attaching function calls to Slate events
Getting ready
How to do it...
How it works...
Using Data Binding with Unreal Motion Graphics
How to do it...
How it works...
Controlling widget appearance with Styles
How to do it...
How it works...
Creating a custom SWidget/UWidget
Getting ready
How to do it...
How it works...
Other Books You May Enjoy
Leave a review - let other readers know what you think
Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to unlock the power of C++ scripting while developing games with UE 4.21. This book starts off by showing how to add and edit C++ classes from within the Unreal Editor. It will delve into one of Unreal's primary strengths: the ability for designers to customize programmer-developed actors and components. It will help you understand the benefits of using C++ in conjunction with the many tools included with this powerful game engine. With a blend of task-oriented recipes, this book will provide actionable information about writing code for games with UE4 and manipulating the game and the development environment using C++. Toward the end of the book, you will be empowered to become a top-notch developer with Unreal Engine 4 using C++ as the scripting language!
This book is intended for game developers who understand the fundamentals of game design and C++ and would like to incorporate native code into the games they make with Unreal.
The readers will likely be programmers who want to extend the engine or implement systems and actors that provide designers with control and flexibility when building levels.
Chapter 1,UE4 Development Tools, outlines basic recipes to get you started with UE4 game development and the basic tools used to create the code that makes your game.
Chapter 2,Creating Classes, focuses on how to create C++ classes and structs that integrate well with the UE4 Blueprints Editor. These classes will be graduated versions of regular C++ classes, called UCLASSES.
Chapter 3,Memory Management, Smart Pointers, and Debugging, takes the reader through using all three types of pointer, and mentions some common pitfalls regarding automatic garbage collection. This chapter also shows readers how to use Visual Studio or Xcode to interpret crashes or confirm that the functionality is implemented correctly.
Chapter 4,Actors and Components, deals with creating custom actors and components, what purpose each of these serves, and how they work together.
Chapter 5,Handling Events and Delegates, describes delegates, events, and event handlers, and guides you through creating your own implementations.
Chapter 6,Input and Collision, shows how to connect user input to C++ functions and how to handle collisions in C++ from UE4. It will also provide default handling of game events such as user input and collision, allowing designers to override when necessary using Blueprint.
Chapter 7,Communication Between Classes and Interfaces: Part I, shows you how to write your own UInterfaces, and demonstrates how to take advantage of them within C++ to minimize class coupling and help keep your code clean.
Chapter 8, Communication Between Classes and Interfaces: Part II, continues the content covered in Chapter 7, discussing in more detail how to get UInterfaces to work in collaboration with Blueprints.
Chapter 9,Integrating C++ and the Unreal Editor: Part I, shows you how to customize the editor by creating custom Blueprint and animation nodes from scratch.
Chapter 10, Integrating C++ and the Unreal Editor: Part II, shows you how to implement custom editor windows and custom detail panels to inspect types created by users.
Chapter 11,User Interfaces – UI and UMG, demonstrates that displaying feedback to the player is one of the most important elements within game design, and this will usually involve some sort of HUD, or at least menus, within your game.
Chapter 12,AI for Controlling NPCs, covers recipes to control your NPC characters with a bit of Artificial Intelligence (AI).
Chapter 13,Working with UE4 APIs, explains that the API is the way in which you, as the programmer, can instruct the engine (and thus the PC) what to do. Each module has an API for it. To use an API, there is a very important linkage step, where you must list all APIs that you will use in your build in theProjectName.Build.csfile.
Chapter 14, Multiplayer Networking in UE4, discusses how to replicate properties and functions over the network, while also testing your game as a client and server simultaneously.
This book assumes familiarity with the C++ programming language, an understanding of what different programming operators mean, and basic knowledge of the concepts of object-oriented programming
Basic familiarity with using the Unreal Engine editor is beneficial, but not required
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Unreal-Engine-4.x-Scripting-with-C-Cookbook---Second-edition. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789809503_ColorImages.pdf.
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
In this chapter, we will outline some basic recipes for getting started in Unreal Engine 4 (UE4) game development, and the basic tools that we will use for creating the code that makes our game. This will include the following recipes:
Installing Visual Studio
Creating and building your first C++ project in Visual Studio
Changing the code font and color in Visual Studio
Extension – changing the color theme in Visual Studio
Formatting your code (Autocomplete settings) in Visual Studio
Shortcut keys in Visual Studio
Extended mouse usage in Visual Studio
UE4 – installation
UE4 – first project
UE4 – creating your first level
UE4 – hot reloading
UE4 – logging with UE_LOG
UE4 – making an FString from FStrings and other variables
Creating a game is an elaborate task that will require a combination of assets and code. To create assets and code, we'll need some pretty advanced tools, including art tools, sound tools, level-editing tools, and code-editing tools. In this chapter, we'll discuss finding suitable tools for asset creation and coding. Assets include any visual artwork (2D sprites, 3D models), audio (music and sound effects), and game levels. Code is the text (usually C++) that instructs the computer on how to tie these assets together to make a game world and level, and how to make that game world play. There are dozens of very good tools for each task; we will explore a couple of each, and make some recommendations. Game editing tools, especially, are hefty programs that require a powerful CPU and lots of memory, and very good, ideal GPUs for good performance. Protecting your assets and work is also a necessary practice. We'll explore and describe source control, which is how you back up your work on a remote server. An introduction to UE4 programming is also included, and we will also explore basic logging functions and library use. Significant planning is also required to get these tasks done, so we'll use a task planner software package to do so.
As listed on UE4's FAQs page, it is recommend to have a a desktop PC with Windows 7 64-bit or a Mac with macOS X 10.9.2 or later, 8 GB of RAM, a quad-core Intel or AMD processor, and a DX11-compatible video card. UE4 will run on desktops and laptops below these recommendations, but performance may be limited.
For those using a Mac computer, Visual Studio for Mac currently does not support C++. You'll need to use a different IDE, such as Visual Studio Code or Xcode, instead.
Visual Studio is an essential package for code editing when editing the C++ code for your UE4 game.
We're going to set up a C++ coding environment to build our UE4 applications. We'll download Visual Studio 2017, install it, and set it up for UE4 C++ coding.
Begin by visiting
https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx
. Click on
Download VS Community 2017
. This downloads the ~1,250 KB loader/installer:
Launch the installer, and continue through the installer until you get to the window where you select the components of Visual Studio 2017 that you want to add to your PC. Keep in mind that the more features you select, the larger your installation will be.
Support for C++
is now an optional part of Visual Studio and isn't installed by default, so we h
ave to
select that we want it installed. Under the
Workloads
section, scroll down to the
Mobile and Gaming
heading and check the
Game
devel
opment with C++
option:
After you have selected the tools you'd like to add on to Visual Studio, click the
Install
button. The installer tool will download the required components and continue setup. After finishing installation, the installer may ask you to restart your computer. Go ahead and do so.
After you download and install Visual Studio 2017, launch it. You will be presented with a
Sign in
dialog box:
You canSign inwith your Microsoft account (the one you use to sign into Windows 10) orSign upfor a new account. After you've signed in or signed up, you will be able to sign into Visual Studio itself. It may seem odd to sign into a desktop code editing program, but your sign-in will be used for source control commits to your repositories. On first signing into Visual Studio, you can select (one time only) a unique URL for your source code repositories, as hosted on Visualstudio.com (https://visualstudio.microsoft.com/).
Visual Studio is an excellent editor, and you will have a fantastic time coding within it. In the next recipe, we'll discuss how to create and compile our own code.
In order to compile and run code from Visual Studio, it must be done from within a project.
In this recipe, we will identify how to create an actual executable running program from Visual Studio. We will do so by creating a project in Visual Studio to host, organize, and compile the code.
In Visual Studio, each group of code is contained within something called a Project. A project is a buildable conglomerate of code and assets that produce either an executable (.exe runnable) or a library (.lib or .dll). A group of projects can be collected into something called a Solution. Let's start by constructing a Visual Studio solution and a project for a console application, followed by constructing a UE4 sample project and solution:
Open Visual Studio and go to
File
|
New
|
Project...
.
You will see a dialog, as follows:
Select
Visual C++
in the pane on the left-hand side. In the middle pane, hit
Windows Console Application
. Name your project in the lower box, and then hit
OK:
Once the application wizard completes, you will have created your first project. Both a solution and a project will be created.
To see these, you need
Solution Explorer
. To ensure that
Solution Explorer
is showing, go to
View
|
Solution Explorer
(or press
Ctrl
+
Alt
+
L
).
Solution Explorer
is a window that usually appears docked on the right-hand side of the main editor window, as shown in the following screenshot:
The Solution Explorer also displays all the files that are part of the project. This default solution already contains a few files, and we can add and remove new files in this section from here. As your project grows, more and more files are going to be added to your project. In the Source Files folder, you'll also notice a file created called FirstProject.cpp, which will look as follows:
// FirstProject.cpp : This file contains the 'main' function. Program execution begins and ends there.//#include "pch.h"#include <iostream>int main(){ std::cout << "Hello World!\n"; }// Run program: Ctrl + F5 or Debug > Start Without Debugging menu// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files// 2. Use the Team Explorer window to connect to source control// 3. Use the Output window to see build output and other messages// 4. Use the Error List window to view errors// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Press
Ctrl
+
Shift
+
B
to build the project, then
Ctrl
+
F5
to run the project.
Your executable will be created, and you will see a small black window with the results of your program's run:
Building an executable involves translating your C++ code from text language into a binary file. Running the file runs your game program, which is just the code text that occurs in the main() function between { and }.
Build configurations are styles of build that we will discuss here. There are at least two important build configurations you should know about: Debug and Release. The Build configuration that's currently selected is at the top of the editor, just below the toolbar in the default position:
Depending on which configuration you select, different compiler options are used. A Debug configuration typically includes extensive debug information in the build, as well as the ability to turn off optimizations to speed up compilation. Release builds are often optimized (either for size or for speed) and take a bit longer to build; they result in smaller or faster executables. Stepping through a file's behavior by moving through with the debugger line by line is often better in the Debug mode than the Release mode.
Customizing the font and color in Visual Studio is not just about flexibility. Due to monitor resolutions being too high or low, it may become a necessity!
Visual Studio is a highly customizable code editing tool. You might find the default fonts too small for your screen. This is easily adjustable by holding down the Ctrl key and using the mouse wheel to increase or decrease the size, but you may want to change the default value. Or, perhaps you may want to have more control, as you may want to change your code's font size and color. You may also want to completely customize the coloration of keywords and the text background colors. The Fonts and Colors dialog box, which we'll show you how to use in this section, allows you to completely customize every aspect of the code editor's font and color:
From within Visual Studio, go to
Tools
|
Options...
:
Select
Environment
|
Fonts and Colors
from the dialog that appears. It will look like what's shown in the following screenshot:
Play around with the font and font size of
Text Editor/Plain Text
. Click
OK
on the dialog, and see the results in the code-text editor:
Text Editor/Plain Text describes the font and size that's used for all code text within the regular code editor. If you change the size of the font, the size changes for any text that's entered into the coding window (for all languages, including C, C++, C#, and others).
The color (foreground and background) is completely customizable for each item. Try this for the Text Editor/Keyword setting (affects all languages), or for items specific to C++, such as Text Editor/C++ Functions. Click OK, and you will see the changed color of the item reflected in the code editor.
You may also want to configure the font size of the Output Window, under the Show settings for: option, so click on the drop-down and select Output Window, as shown in the following screenshot:
The Output Window is the little window at the bottom of the editor that displays build results and compiler errors.
The Fonts and Colors dialog simply changes the appearance of code in the text editor as well as for other windows, such as the output window. It is very useful for making your coding environment more comfortable.
Once you have customized your settings, you'll find that you may want to save your customized Fonts and Colors settings for others to use, or to put into another installation of Visual Studio, which you have on another machine. Unfortunately, by default, you won't be able to save-out your customized Fonts and Colors settings. You will need something called the Visual Studio Theme Editor extension to do so. We will explore this in the next recipe.
The
Extension – changing the color theme in Visual Studio
recipe describes how to import and export color themes
By default, you cannot save the changes you make to the font colors and background settings that you make in the Fonts and Colors dialog. To fix this issue, Visual Studio has a feature called Themes. If you go to Tools | Options | Environment | General, you can change the theme to one of the three pre-installed stock themes (Light, Blue, and Dark):
A different theme completely changes the look of Visual Studio, from the colors of the title bars to the background color of the text editor window.
You can also customize the theme of Visual Studio completely, but you'll need an extension to do so. Extensions are little programs that can be installed into Visual Studio to modify its behavior.
By default, your customized color settings cannot be saved or reloaded into another Visual Studio installation without the extension. With the extension, you will also be able to save your own color theme to share with others. You can also load the color settings made by another person or by yourself into a fresh copy of Visual Studio.
Go to
Tools
|
Extensions and Updates...
.
From the dialog that appears, choose
Online
in the panel on the left-hand side. Start typing
Theme Editor
into the search box on the right. The
Color Theme Editor for Visual Studio
option will pop up in your search results:
Click the small
Download
button in the top right-hand corner of the entry. Click through the installation dialog prompts, allowing the plugin to install. You'll then notice on the bottom of the window that it is scheduled for installation but will wait until Visual Studio is closed.
Close the window and Visual Studio, saving our project. After our program has closed, the VSIX Installer window will come up to confirm that you want to install the software. Click on the
Modify
button and it should start:
Once it has finished installing, open up Visual Studio again and open our project. One of the quickest ways to do so is from the
Recent
section on the
Start Page
:
After restarting, go to
Tools
|
Customize Colors
to open the
Color Themes
editor page:
From the
Color Themes
dialog that appears, click on the little palette-shaped icon on the upper-right corner of the theme that you want to use as your base or starting theme (I've clicked on the palette for the
Light
theme here, as you can see in the following screenshot):
A copy of the theme will appear in the
Custom Themes
section in the lower part of the
Color Themes
window. Click on
Edit Theme
to modify the theme that is the middle button that appears when you hover over the custom theme. When you are editing the theme, you can change everything from the font text color to the C++ keyword color.
The main area you are interested in is the C++ Text Editor section. To gain access to all the C++ Text Editor options, be sure to select the Show All Elements option at the top of the Theme Editor window, as shown in the following screenshot:
Note that, while most of the settings you are interested in will be under
Text Editor
|
C/C++
, some will not have the
C++
subheading. For example, the setting for the main/plain text inside the editor window (for all languages) is under
Text Editor
|
Plain Text
(without the
C++
subheading).
Select the theme to use from
Tools
|
Options
|
Environment
|
General
. Any new themes you have created will appear automatically in the drop-down menu.
Once we load the plugin, it integrates into Visual Studio quite nicely. Exporting and uploading your themes to share with others is quite easy too.
Adding a theme to your Visual Studio installs it as an extension in Tools | Extensions and Updates.... To remove a theme, simply Uninstall its extension:
Code-writing formatting with Visual Studio is a pleasure. In this recipe, we'll discuss how to control the way Visual Studio lays out the text of your code.
Code has to be formatted correctly. You and your co-programmers will be able to better understand, grok, and keep your code bug-free if it is consistently formatted. This is why Visual Studio includes a number of auto-formatting tools inside the editor.
Go to
Tools
|
Options.
Once there, go to the
Text Editor
|
C/C++
section and select it. This dialog displays a window that allows you to toggle
Automatic brace completion
:
Automatic brace completion is a feature where, when you type {, a corresponding } is automatically typed for you. This feature may irk you if you don't like the text editor inserting characters for you unexpectedly.
You generally want Auto list members on, as that displays a nice dialog with the complete names of data members listed for you as soon as you start typing. This makes it easy to remember variable names, so you don't have to memorize them:
Some more autocomplete behavior options are located under
Text Editor
|
C/C++
|
Formatting
:
I recommend using all of the options at first and then disabling them only if they interrupt your workflow.
The default autocomplete and autoformat behaviors may irk you. You need to converse with your team on how you want your code to be formatted (spaces or tab indents, size of indent, and so on), and then configure your Visual Studio settings accordingly.
There are a number of shortcut keys that will make coding and project navigation much faster and more efficient for you. In this recipe, we will describe how to use some of the common shortcut keys that will really enhance your coding speed.
To get started, you will need to have Visual Studio installed and a project opened to look at the features.
The following are some very useful keyboard shortcuts for you to try:
Click on one part of the code, then click somewhere else, at least 10 lines of code away. Now, press
Ctrl
+
-
[navigate backwards]. Navigation through different pages of source code (the last place you were at, and the place you are at now) is done by pressing
Ctrl
+
-
and
Ctrl
+
Shift
+
-
, respectively:
Note that the being mentioned is the one near the 0 key on your keyboard and will not work with the on the numpad.
Say, for example, you're editing code in one place, and you want to go back to the place you've just been (or go back to the section in the code you came from). Simply press Ctrl + -, and that will warp you back to the location in the code you were at last. To warp forward to the location you were at before, press Ctrl + -, press Ctrl + Shift + -. To warp back, the previous location should be more than 10 lines away, or in a different file. These correspond to the forward and back menu buttons in the toolbar:
Press
Ctrl
+
W
to highlight a single word.
Press and hold
Ctrl
+
Shift
+ right arrow (or left arrow) (not
Shift
+ right arrow) just to move to the right and left of the cursor, selecting entire words.
Press
Ctrl
+
C
to copy text,
Ctrl
+
X
to cut text, and
Ctrl
+
V
to paste text.
Clipboard ring
: The clipboard ring is a kind of a reference to the fact that Visual Studio maintains a stack of the last copy operations. By pressing
Ctrl
+
C
, you push the text that you are copying into an effective stack. Pressing
Ctrl
+
C
a second time on different text pushes that text into the
Clipboard Stack
. For example, in the following diagram, we pressed
Ctrl
+
C
on the word
cyclic
first, then
Ctrl
+
C
on the word
paste
afterward.
As you know, pressing Ctrl + V pastes the top item in the stack. Pressing Ctrl + Shift + Insert accesses a very long history of all the items ever copied in that session, that is, items underneath the top item in the stack. After you exhaust the list of items, the list wraps back to the top item in the stack. This is an odd feature, but you may find it useful occasionally.
Ctrl
+
M
collapses a code section:
Keyboard shortcuts allow you to speed up work in the code editor by reducing the number of mouse- reaches that you have to perform in a coding session.
The mouse is a pretty handy tool for selecting text. In this section, we'll highlight how to use the mouse in an advanced way so that you can make quick edits to your code's text.
Hold down the
Ctrl
key while clicking to select an entire word:
Hold down the
Alt
key to select a box of text (
Alt
+ left-click + drag):
You can then either cut, copy, or overwrite the box-shaped text area; in the latter case, the characters you type will be repeated in all selected rows.
Mouse-clicking alone can be tedious, but with the help of Ctrl + Alt, it becomes quite cool.
There are a number of steps to follow to install and configure UE4 properly. In this recipe, we'll walk through the correct installation and setup of the engine.
UE4 takes up quite a few GB of space, so you should have at least 20 GB or so free for the installation on the target drive. Note that every project is also at least 1 GB as well, so you will need more space on your computer (or an additional hard drive) for more projects you wish to create.
Visit
unrealengine.com
in your web browser of choice:
On the top-right corner of the screen, click on the
Download
button. You'll then be asked to create an Epic Games account. If you already have one, you can scroll down to the bottom of the screen and click the
Sign in
option.
Run the installer for the Epic Games Launcher program by double-clicking the
EpicGamesLauncherInstaller-x.x.x-xxx.msi
installer. Install it in the default location.
Once the Epic Games Launcher program is installed, open it by double-clicking its icon, which can be found on your desktop or in the Start menu:
You'll need to sign in with the same login information you created or used earlier, and then you'll arrive at the main page of the launcher:
There are a lot of available options, but we want to click on the
Unreal Engine
option on the top-left of the screen.
Browse the start page and take a look around. Eventually, you will need to install an engine. Click on the large orange
Install Engine
button on the screen, as shown in the following screenshot:
A pop-up dialog will ask you to agree to an
End Licence Agreement
. Afterwards, you'll be asked to choose an install location. Then, click the
Install
button:
The launcher will start downloading the engine. It is about 7 GB, so it may take a while. Once finished, your screen should look something like this:
After the engine has installed, the Install Engine