logo资料库

Learning C++ by Creating Games with Unreal Engine 4.pdf

第1页 / 共342页
第2页 / 共342页
第3页 / 共342页
第4页 / 共342页
第5页 / 共342页
第6页 / 共342页
第7页 / 共342页
第8页 / 共342页
资料共342页,剩余部分请下载后查看
Cover
Copyright
Credits
About the Author
About the Reviewers
www.PacktPub.com
Table of Contents
Preface
Chapter 1: Coding with C++
Setting up our project
Using Microsoft Visual C++ on Windows
Using XCode on a Mac
Creating your first C++ program
Semicolons
Handling errors
Warnings
What is building and compiling?
Scripting
Exercise – ASCII art
Summary
Chapter 2: Variables and Memory
Variables
Declaring variables – touching the silicon
Reading and writing to your reserved spot in memory
Numbers are everything
More on variables
Math in C++
Exercises
Generalized variable syntax
Primitive types
Object types
Exercise – Player
Pointers
What can pointers do?
Address of operator &
The Null pointers
cin
printf()
Exercise
Solution
Summary
Chapter 3: If, Else, and Switch
Branching
Controlling the flow of your program
The == operator
Coding if statements
Coding else statements
Testing for inequalities using other comparison operators (>, >=, <, <=, and !=)
Using logical operators
The Not (!) operator
Exercises
Solution
The And (&&) operator
The Or (||) operator
Our first example with Unreal Engine
Exercise
Solution
Branching code in more than two ways
The else if statement
Exercise
Solution
The switch statement
Switch versus if
Exercise
Solution
Summary
Chapter 4: Looping
The while loop
Infinite loops
Exercises
Solutions
The do/while loop
The for loop
Exercises
Solutions
Looping with Unreal Engine
Summary
Chapter 5: Functions and Macros
Functions
An example of a library function – sqrt()
Writing our own functions
A sample program trace
Exercise
Solution
Functions with arguments
Functions that return values
Exercises
Solutions
Variables, revisited
Global variables
Local variables
The scope of a variable
Static local variables
Const variables
Function prototypes
.h and .cpp files
prototypes.h contains
funcs.cpp contains
main.cpp contains
Extern variables
Macros
Advice – try to use const variables where possible
Macros with arguments
Advice – use inline functions instead of macros with arguments
Summary
Chapter 6: Objects, Classes, and Inheritance
struct objects
Member functions
The this keyword
Strings are objects?
Invoking a member function
Exercises
Solutions
Privates and encapsulation
Some people like it public
class versus struct
Getters and setters
Getters
Setters
But what's the point of get/set operations?
Constructors and destructors
Class inheritance
Derived classes
Syntax of inheritance
What does inheritance do?
is-a relationship
protected variables
Virtual functions
Purely virtual functions (and abstract classes)
Multiple inheritance
private inheritance
Putting your classes into headers
.h and .cpp
Exercise
Summary
Chapter 7: Dynamic Memory Allocation
Dynamic memory allocation
The delete keyword
Memory leaks
Regular arrays
The array syntax
Exercise
Solutions
C++ style dynamic size arrays (new[] and delete[])
Dynamic C-style arrays
Summary
Chapter 8: Actors and Pawns
Actors versus pawns
Creating a world to put your actors in
The UE4 editor
Editor controls
Play mode controls
Adding objects to the scene
Starting from scratch
Adding light sources
Collision volumes
Adding collision detection for the objects editor
Adding an actor to the scene
Creating a player entity
Inheriting from UE4 GameFramework classes
Associating a model with the Avatar class
Loading the mesh
Creating a blueprint from our C++ class
Writing C++ code that controls the game's character
Making the player an instance of the Avatar class
Setting up controller inputs
Exercise
Solution
Yaw and pitch
Creating non-player character entities
Displaying a quote from each NPC dialog box
Displaying messages on the HUD
Using TArray
Exercise
Solution
Triggering an event when it is near an NPC
Make the NPC display something to the HUD when something is nearby
Exercises
Solutions
Summary
Chapter 9: Templates and Commonly Used Containers
Debugging the output in UE4
UE4's TArray
An example that uses TArray
Iterating a TArray
Finding whether an element is in the TArray
TSet
Iterating a TSet
Intersecting TSet
Unioning TSet
Finding TSet
TMap
A list of items for the player's inventory
Iterating a TMap
C++ STL versions of commonly used containers
C++ STL set
Finding an element in a
Exercise
Solution
C++ STL map
Finding an element in a
Exercise
Solution
Summary
Chapter 10: Inventory System and Pickup Items
Declaring the backpack
Forward declaration
Importing assets
Attaching an action mapping to a key
Base class PickupItem
The root component
Getting the avatar
Getting the player controller
Getting the HUD
Drawing the player inventory
Using HUD::DrawTexture()
Exercise
Detecting inventory item clicks
Dragging elements
Exercises
Summary
Chapter 11: Monsters
Landscape
Sculpting the landscape
Monsters
Basic monster intelligence
Moving the monster – steering behavior
The discrete nature of monster motion
Monster SightSphere
Monster attacks on the player
Melee attacks
Defining a melee weapon
Sockets
Creating a skeletal mesh socket in the monster's hand
Attaching the sword to the model
Code to equip the player with a sword
Triggering the attack animation
Projectile or ranged attacks
Bullet physics
Adding bullets to the monster class
Player knockback
Summary
Chapter 12: Spell Book
The particle systems
Changing particle properties
Settings for the blizzard spell
Spell class actor
Blueprinting our spells
Picking up spells
Creating blueprints for PickupItems that cast spells
Attaching right mouse click to cast spell
Writing the avatar's CastSpell function
Instantiating the spell – GetWorld()->SpawnActor()
if(spell)
spell->SetCaster(this)
Writing AMyHUD::MouseRightClicked()
Activating right mouse button clicks
Creating other spells
The fire spell
Exercises
Summary
Index
Learning C++ by Creating Games with UE4 Learn C++ programming with a fun, real-world application that allows you to create your own games! William Sherif BIRMINGHAM - MUMBAI
Learning C++ by Creating Games with UE4 Copyright © 2015 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 author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be 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. First published: February 2015 Production reference: 1180215 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78439-657-2 www.packtpub.com
Credits Author William Sherif Reviewers Brandon Mann Matt Sutherlin Alan Wolfe Project Coordinator Rashi Khivansara Proofreaders Martin Diver Lawrence A. Herman Paul Hindle Commissioning Editor Edward Bowkett Indexer Hemangini Bari Acquisition Editor Subho Gupta Graphics Sheetal Aute Content Development Editor Production Coordinator Anand Singh Melwyn D'sa Technical Editor Saurabh Malhotra Cover Work Melwyn D'sa Copy Editors Dipti Kapadia Deepa Nambiar
About the Author William Sherif is a C++ programmer with more than 8 years' programming experience. He has a wide range of experience in the programming world, from game programming to web programming. He has also worked as a university course instructor (sessional) for 7 years. He has released several apps on to 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. I'd like to thank my family, for their support when I was writing this book; Mostafa and Fatima, for their hospitality; as well as Ryan and Reda, for letting me write.
About the Reviewers Brandon Mann is a well-rounded game developer with over 7 years of professional game-development experience. He has worked on a wide range of titles, from Indie Games to AAA titles, and at companies such as Warner Bros., Midway, and 343 Industries. He has worked on several titles, including Blacklight, Tango Down, Gotham City Impostors, and Battle Nations. Matt Sutherlin has been working in the games industry for over a decade now, where he's held job titles ranging from QA and scripter to engine programmer, and technical artist. Most recently, he has been heavily focusing on graphics technology, working on engine renderers, art pipelines, and shaders for AAA titles, such as Heroes of the Storm and Halo 5: Guardians. I would like to thank my beautiful wife, Megan, for years of support and understanding and Matthew Phillips for giving me my debut in the industry.
Alan Wolfe is a self-taught game and engine programmer who has worked at companies such as inXile Entertainment, Midway, Warner Bros., and Blizzard Entertainment. He has worked on titles including Line Rider 2, Unbound, Gotham City Impostors, Battle Nations, Insanely Twisted Shadow Planet, and StarCraft II: Heart of the Swarm. Alan is currently a senior engine programmer at Blizzard Entertainment, where he works on StarCraft II and Heroes of the Storm. I'd like to thank Packt Publishing and the author for allowing me to contribute to this book and to help budding game programmers learn the same way I did. If you want to succeed as a game programmer, practice implementing everything you learn, hang out with like- minded individuals, who want to achieve the same things you do, and never stop learning new things.
www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. 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.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.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. https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
分享到:
收藏