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

Partager le livre
  1. 708 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et 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

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Unreal Engine 4.x Scripting with C++ Cookbook est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Unreal Engine 4.x Scripting with C++ Cookbook par John P. Doran, William Sherif, Stephen Whittle en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Ciencia de la computaciĂłn et ProgramaciĂłn de juegos. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
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 ...

Table des matiĂšres