Unreal Engine 4.x Scripting with C++ Cookbook
eBook - ePub

Unreal Engine 4.x Scripting with C++ Cookbook

Develop quality game components and solve scripting problems with the power of C++ and UE4, 2nd Edition

John P. Doran, William Sherif, Stephen Whittle

Condividi libro
  1. 708 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Unreal Engine 4.x Scripting with C++ Cookbook

Develop quality game components and solve scripting problems with the power of C++ and UE4, 2nd Edition

John P. Doran, William Sherif, Stephen Whittle

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Write efficient, reusable scripts to build custom characters, game environments, and control enemy AI

Key Features

  • Build captivating multiplayer games using Unreal Engine and C++
  • Incorporate existing C++ libraries into your game to add extra functionality such as hardware integration
  • Practical solutions for memory management, error handling, inputs, and collision for your game codebase

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

  • Create C++ classes and structs that integrate well with UE4 and the Blueprints editor
  • Discover how to work with various APIs that Unreal Engine already contains
  • Utilize advanced concepts such as events, delegates, and interfaces in your UE4 projects
  • Build user interfaces using Canvas and UMG through C++
  • Extend the Unreal Editor by creating custom windows and editors
  • Implement AI tasks and services using C++, Blackboard, and Behavior Trees
  • Write C++ code with networking in mind and replicate properties and functions

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Unreal Engine 4.x Scripting with C++ Cookbook è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Unreal Engine 4.x Scripting with C++ Cookbook di John P. Doran, William Sherif, Stephen Whittle in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Ciencia de la computación e Programación de juegos. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2019
ISBN
9781789803372

Communication Between Classes and Interfaces: Part I

The following recipes will be covered in this chapter:
  • Creating a UInterface
  • Implementing a UInterface on an object
  • Checking if a class implements a UInterface
  • Casting to a UInterface implemented in native code
  • Calling native UInterface functions from C++
  • Inheriting UInterfaces from one another
  • Overriding UInterface functions in C++
  • Implementing a simple interaction system with UInterfaces

Introduction

This chapter 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.
In your game projects, you will sometimes require a series of potentially disparate objects to share a common functionality, but it would be inappropriate to use inheritance because there is no is-a relationship between the different objects in question. Languages such as C++ tend to use multiple inheritance to solve this issue.
However, in Unreal, if you wanted functions from both parent classes to be accessible to Blueprint, you would need to make both of them UCLASS. This is a problem for two reasons. Inheriting from UClass twice in the same object would break the concept that UObject should form a neatly traversable hierarchy. It also means that there are two instances of the UClass methods on the object, and they would have to be explicitly differentiated between within the code. The Unreal codebase solves this issue by borrowing a concept from C#: that of an explicit Interface type.
The reason for using this approach, instead of composition, is that Components are only available on Actors, not on UObjects in general. Interfaces can be applied to any UObject. Furthermore, it means that we are no longer modeling an is-a relationship between the object and the component; instead, it would only be able to represent has-a relationships.

Technical requirements

This chapter requires the use of Unreal Engine 4 and uses Visual Studio 2017 as the IDE. Instructions on how to install both pieces of software and the requirements for them can be found in Chapter 1, UE4 Development Tools, of this book.

Creating a UInterface

UInterfaces are pairs of classes that work together to enable classes to exhibit polymorphic behavior among multiple class hierarchies. This recipe shows you the basic steps involved in creating a UInterface purely in code.

How to do it...

  1. From the Content Browser, go to Add New | New C++ Class. From the menu that pops up, scroll down all the way until you see the Unreal Interface selection and select it. Afterward, click on the Next button:
  1. From there, verify that the Name of the class is MyInterface and then click on the Create Class button:
  1. Add the following code to the header file:
#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MyInterface.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
GENERATED_BODY()
};

class CHAPTER_07_API IMyInterface
{
GENERATED_BODY()

// Add interface functions to this class. This is the class that
//will be inherited to implement this interface.

public:
virtual FString GetTestName();
};
  1. Implement the class with this code in the .cpp file:
#include "MyInterface.h"

// Add default functionality here for any IMyInterface functions that are not pure virtual.
FString IMyInterface::GetTestName()
{
unimplemented();
return FString();
}
  1. Compile your project to verify that the code was written without errors.

How it works...

UInterfaces are implemented as pairs of classes that are declared in the interface's header.
As always, because we are leveraging Unreal's reflection system, we need to include our generated header file. Refer to the Handling events implemented via virtual functions recipe in Chapter 5, Handling Events and Delegates, for more information.
As with classes that inherit from UObject, which uses UCLASS, we need to use the UINTERFACE macro to declare our new UInterface. Passing in the class specifier of MinimalAPI causes only the class's type information to be exported for use by other modules.
For more information on this and other class specifiers, check out: https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Reference/Classes/Specifiers.
The class is tagged as UE4COOKBOOK_API to help with exporting library symbols.
The base class for the UObject portion of the interface is UInterface.
Just like UCLASS types, we require a macro to be placed inside the body of our class so that the auto-generated code is inserted into it. That macro is GENERATED_BODY() for UInterfaces. The macro must be placed at the very start of the class body.
The second class is also tagged as UE4COOKBOOK_API, and is named in a specific way.
Note that the UInterface derived class and the standard class have the same name but a different prefix. The ...

Indice dei contenuti