Learning C++ by creating games with UE4

Learning C++ by creating games with UE4Reviews
Author: William Sherif
Pub Date: 2015
ISBN: 978-1-78439-476-9
Pages: 342
Language: English
Format: PDF/EPUB
Size: 28 Mb

Download

Узнайте программирования C++ с забавной , в реальном мире приложение, которое позволяет вам создавать свои собственные игры !
Unreal Engine 4 используется для создания высшего класса, захватывающие игры от AAA студий, и учиться программировать на С ++ нуждается в некоторой серьезной мотивации.
Обучение C++ путем создания игры с UE4 начнется с основами C++: установка редактора кода, так что вы можете начать писать C++ код. Затем вы узнаете, как писать небольшие программы, автономный C++, которые покажут вам, как использовать язык С++, не перегружая вас слишком много кода в самом начале. Как мы копаться в более продвинутых C++ понятий, то вы начнете исследовать функциональность двигателя UE4 может предложить. Вы будете использовать редактор UE4 создать свой собственный мир, а затем программа в некоторых серьезно увлекательный игровой процесс. К концу этой книги, вы должны иметь хорошее представление о том, как программировать на C++.

+

Chapter 1. Coding with C++

Y ou’re a first-time programmer . Y ou have a lot to learn!

Academics often describe programming concepts in theory but like to leave implementation to someone else, preferably someone from the industry . W e don’t do that in this book—in this book, we will describe the theory behind C++ concepts and implement our own game as well.

The first thing I will recommend is that you do the exercises. Y ou cannot learn to program simply by reading. Y ou must work with the theory with the exercises.

We are going to get started by programming very simple programs in C++. I know that you want to start playing your finished game right now . However , you have to start at the beginning to get to that end (if you really want to, skip over to Chapter 12, Spell Book, or open some of the samples to get a feel for where we are going).

In this chapter , we will cover the following topics:

  • Setting up a new project (in V isual Studio and Xcode)
  • Your first C++ project
  • How to handle errors
  • What are building and compiling.

Setting up our project

Our first C++ program will be written outside of UE4. T o start with, I will provide steps for both Xcode and V isual Studio 2013, but after this chapter , I will try to talk about just the C++ code without reference to whether you’re using Microsoft Windows or Mac OS.

Creating your first C++ program

We are now going to write some C++ source code. There is a very good reason why we are calling it the source code: it is the source from which we will 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.

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

Tip

In practice, there are some differences between V isual Studio’ s flavor of C++ and Xcode’s flavor of C++, but these differences mostly come up when working with advanced C++ concepts, such as templates.

One of the main reasons why using UE4 is so helpful is that UE4 will erase a lot of the differences between Windows and Mac. The UE4 team did a lot of magic in order to get the same code to work on both Windows and Mac.

What is building and compiling?

Y ou might have heard of a computer process term called compiling. Compiling is the process of converting your C++ program into code that can run on a CPU. Building your source code means the same thing as compiling it.

See, your source code.cpp file will not actually run on a computer . It has to be compiled first for it to run.

This is the whole point of using Microsoft V isual Studio Express or Xcode. V isual Studio and Xcode are both compilers. Y ou can write C++ source code in any text-editing program — even in Notepad. But you need a compiler to run it on your machine.

Every operating system typically has one or more C++ compilers that can compile C++ code to run on that platform. On Windows, you have V isual Studio and Intel C++ Studio compiler . On Mac, there is Xcode, and on all of Windows, Mac, and Linux, there is the GNU Compiler Collection (GCC).

The same C++ code that we write (Source) can be compiled using different compilers for different operating systems, and in theory , they should produce the same result. The ability to compile the same code on different platforms is called portability . In general, portability is a good thing.

Scripting

There is another class of programming languages called scripting languages. These include languages such as PHP, Python, and ActionScript. Scripted languages are not compiled—for JavaScript, PHP , and ActionScript, there is no compilation step. Rather, they are interpreted from the source as the program is run. The good thing about scripting
languages is that they are usually platform-independent from the first go, because interpreters are very carefully designed to be platform-independent.

Summary

To sum it up, we learned how to write our first program in the C++ programming language in our integrated development environment (IDE, V isual Studio, or Xcode). This was a simple program, but you should count getting your first program to compile and run as your first victory . In the upcoming chapters, we’ll put together more complex programs and start using Unreal Engine for our games.

The preceding screenshot is of your first C++ program and the following screenshot is of its output, your first victory:

Chapter 2. V ariables and Memory

T o write your C++ game program, you will need your computer to remember a lot of things. Things such as where in the world is the player , how many hit points he has, how much ammunition he has left, where the items are in the world, what power-ups they provide, and the letters that make up the player ’ s screen name.

The computer that you have actually has a sort of electronic sketchpad inside it called memory , or RAM. Physically , computer memory is made out of silicon and it looks something similar to what is shown in the following screenshot:

RAM is short for Random Access Memory . It is called random access because you can access any part of it at any time. If you still have some CDs lying around, they are an example of non-random access. CDs are meant to be read and played back in order . I still remember jumping tracks on Michael Jackson’ s Danger ous album way back when
switching tracks on a CD took a lot of time! Hopping around and accessing different cells of RAM, however , doesn’t take much time at all. RAM is a type of fast memory access known as flash memory .

RAM is called volatile flash memory because when the computer is shut down, RAM’s contents are cleared and the old contents of RAM are lost unless they were saved to the hard disk first.

For permanent storage, you have to save your data into a hard disk. There are two main types of hard disks, platter-based Hard Disk Drives (HDDs) and Solid-state Drives (SSDs). SSDs are more modern than platter-based HDDs, since they use RAM’ s fastaccess (Flash) memory principle. Unlike RAM, however , the data on an SSD persists after
the computer is shut down. If you can get an SSD, I’d highly recommend that you use it! Platter-based drives are outdated. W e need a way to reserve a space on the RAM and read and write from it. Fortunately , C++ makes this easy .

Variables

A saved location in computer memory that we can read or write to is called a variable.

A variable is a component whose value can vary . In a computer program, you can think of a variable as a container , into which you can store some data. In C++, these data containers (variables) have types. Y ou have to use the right type of data container to save your data in your program.

If you want to save an integer , such as 1, 0, or 20, you will use an int type container. You can use float-type containers to carry around floating-point (decimal) values, such as 38.87, and you can use string variables to carry around strings of letters (think of it as a “string of pearls”, where each letter is a pearl).

You can think of your reserved spot in RAM like reserving parking space in a parking garage: once we declare our variable and get a spot for it, no one else (not even other programs running on the same machine) will be given that piece of RAM by the operating system. The RAM beside your variable might be unused or it might be used by other
programs.

Tip

The operating system exists to keep programs from stepping on each other ’s toes and accessing the same bits of computer hardware at the same time. In general, civil computer programs should not read or write to each other ’s memory . However , some types of cheat programs (for example, maphacks) secretly access your program’s memory . Programs such as PunkBuster were introduced to prevent cheating in online games.