Tải bản đầy đủ (.pdf) (466 trang)

UDK game programming with unrealscript

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.44 MB, 466 trang )


Unreal Development Kit Game
Programming with UnrealScript

Create games beyond your imagination with the Unreal
Development Kit

Rachel Cordone

BIRMINGHAM - MUMBAI

Download from Wow! eBook <www.wowebook.com>

Beginner's Guide


Unreal Development Kit Game Programming with
UnrealScript
Beginner's Guide

Copyright © 2011 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: December 2011

Production Reference: 1081211

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-192-5
www.packtpub.com

Cover Image by Tom Mooney ()


Credits
Author
Rachel Cordone
Reviewers

Project Coordinator
Kushal Bhardwaj
Proofreaders

Edward Davies

Mario Cecere


Dave Voyles

Chris Smith

Acquisition Editor
Wilson D'Souza
Development Editor
Meeta Rajani

Indexer
Hemangini Bari
Graphics
Manu Joseph

Technical Editors

Production Coordinator

Pramila Balan

Arvindkumar Gupta

Kavita Iyer
Llewellyn Rozario

Cover Work
Arvindkumar Gupta


About the Author

Rachel Cordone is a designer and self-taught UnrealScript programmer, who has been
working with the Unreal Engine since 1999. She has worked for various game and simulation
companies since 2003, including Pipeworks Software and Parsons Brinkerhoff, and has
started up her own game company, Stubborn Horse Studios, to make independent games
with the Unreal Development Kit. Stubborn Horse's first project, Prometheus, won several
awards in Epic Games' Make Something Unreal Contest.
I would like to thank my crazy goat for his love and support while writing
this book!


About the Reviewers
Edward Davies is in the final year of his Game Art and Animation degree at the University
of Glamorgan. He has a strong interest in game design, particularly the Unreal Development
Kit, concept art, and 3D modeling and texturing. More of Edward's work may be seen at
www.kungfoowiz.deviantart.com/gallery.

Dave Voyles is a Managing Editor and Podcast Producer for Armless Octopus. He covers
Xbox LIVE Indie Game, Xbox Live Arcade, and Playstation Network news, reviews, and
developer interviews. He holds a BS in Communication Studies from SUNY Oneonta,
and is currently attending the New York Institute of Technology to work on his MBA in
Management of Information Systems. His additional work within the gaming community
includes working as the Coordinator of the Indie Games Summer Uprising, which looks to
promote the most outstanding titles on the Xbox LIVE Indie Games platform. Dave is also an
Unreal Script programmer for two titles that will be released on PC and iOS, at the end of
2011. Most notably, he is the founder of the New York City-based UDK meetup group, where
he works with other developers to collaborate on endeavors in a physical environment,
as well as provide tutorials. You can find more of his work by visiting his sites
or .



www.PacktPub.com
Support files, eBooks, discount offers, and more
You might want to visit www.PacktPub.com for support files and downloads related to
your book.
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
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.



Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can access, read, and search across 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 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 nine entirely free books. Simply use your login credentials for
immediate access.


Table of Contents
Preface1
Chapter 1: Project Setup and Test Environments
7
System requirements
Minimum requirements:
Time for action – Installing the UDK
Directory overview
Binaries
Development
Engine
UDKGame
Using external programs to code
ConTEXT
Time for action – Installing ConTEXT
Time for action – Configuring ConTEXT
UnCodeX
Time for action – Installing UnCodeX
Time for action – Configuring UnCodeX
nFringe
WOTgreal
Setting up a project
Time for action – Setting up AwesomeGame
Compiling and testing

Time for action – Compiling and testing AwesomeActor
A quick note about comments
Summary

Chapter 2: Storing and Manipulating Data
Variables and arrays
Booleans

8
8
8
10
10
11
11
11
12
12
13
14
19
19
20
23
23
23
24
30
30
37

38

39
39
40


Table of Contents

Time for action – Using booleans
Integers and floats
Time for action – Using integers
Time for action – Using floats
Strings
Time for action – Using strings
Enums
Time for action – Using enums
Arrays
Time for action – Using arrays
Dynamic arrays
Time for action – Using dynamic arrays
Structs
Time for action – Using structs
Vectors
Time for action – Using vectors
Rotators
Time for action – Using rotators
Variable properties
Default properties
Time for action – Using the default properties block

Editable variables
Time for action – Editable variables
Config variables
Time for action – Creating config variables
Common operators
Standard arithmetic
Time for action – Math!
Modulo
Time for action – Using modulo
Comparisons
Time for action – Comparisons
Logical operators
Time for action – Using logical operators
Concatenation
Time for action – Concatenation
Variable functions
Ints
Floats
Strings
Vectors
Rotators

40
43
43
45
46
46
47
48

50
51
53
53
56
57
60
60
63
64
67
67
67
70
70
71
71
73
73
73
75
75
76
76
79
80
81
82
83
83

83
84
84
84

[ ii ]


Table of Contents

Flow control
If else
Time for action – Using if/else
For
Time for action – Using the for statement
While
Time for action – Something
Do until
Switch
Time for action – Using switches
Return
Goto
Summary

85
85
86
87
87
88

88
90
90
90
92
92
93

Chapter 3: Understanding the Class Tree

95

What is a class?
Time for action – All classes are created equally
Inheritance
Time for action – Examining inheritance
Time for action – Making a custom weapon
Time for action – Experiments with inheritance
Function overriding
Time for action – Creating a custom GameInfo and PlayerController
Time for action – Experiments with function overriding
Actors as variables
Time for action – Experiments with Actors as variables
Casting
Time for action – Casting Actor variables
Time for action – A practical example of casting for our game
Summary

Chapter 4: Making Custom Classes


96
96
98
99
100
102
104
104
106
116
116
124
124
129
134

135

Creating a class
Awesome Game quicky design document
Class breakdown
Time for action – Creating the weapon branch
Class modifiers
Placeable
Notplaceable
Abstract
Time for action – Using abstract
Native
[ iii ]


135
136
137
137
147
147
147
148
148
150


Table of Contents

Config
Hidecategories
Time for action – Hidecategories
Hidedropdown
Actors versus objects
Common UnrealScript classes
The GameInfo
Time for action – Expanding AwesomeGame
Time for action – SHOOT NOW!
Time for action – Customizing the Pawn class
The Controller
Time for action – Expanding the Controller
Time for action – No, my left!
The Pawn
Time for action – Detecting collisions to give our Pawn damage
Time for action – Making the TestEnemies move

The HUD
Time for action – Using the HUD
Summary

Chapter 5: Using Functions

151
151
152
153
153
154
154
154
157
160
162
163
169
172
172
174
178
178
182

183

What's your function?
Creating and calling functions

Time for action – Writing a function
Time for action – Calling custom functions
Time for action – What's your malfunction?
Local versus instance variables
Local Variables
Time for action – Using local variables
Actors as local variables
Time for action – Using Actors as local variables
Time for action – Modifying the projectile
Function parameters and modifiers
Function parameters
Time for action – Using function parameters
Time for action – Out parameters
Return values
Time for action – I'd like to return this please
Function modifiers
Native
Const, NoExport, latent, and iterator
Event

184
187
188
191
194
196
196
197
202
202

204
205
206
206
210
212
212
216
216
216
216

[ iv ]


Table of Contents
Simulated, server, client, reliable, and unreliable
Singular
Exec

Time for action – Filthy cheater

217
217
218

218

Static


219

Time for action – Using static functions
The super
Using timers
Time for action – Just five more minutes mom
Putting it all together
Time for action – Expanding Awesome Game
Summary

220
221
221
222
224
224
232

Chapter 6: Using States to Control Behavior

233

It's a state of mind
Creating a state
Time for action – Writing a state
Switching between states
Time for action – Switching states
Function overriding in states
Red state, blue state, no state, new state?
Time for action – Multiple personalities

Non-state functions
Time for action – Calling non-state functions
Time for action – Non-state functions from inside a state
State changes and detection
BeginState
Time for action – BeginState
EndState
Time for action – EndState
State detection
Time for action – Using state detection functions
Subclassing states
Time for action – Subclassing the Seeking state
Keywords, labels, and latent functions
Keywords
Time for action – Using ignores
Labels and latent functions
Time for action – Do we really need to give labels to everything?
Like a boss
The cleanup job
[v]

233
234
234
239
240
243
243
243
248

248
250
252
252
252
254
254
256
257
259
259
260
260
260
261
261
263
263


Table of Contents

Time for action – Reverting our code
Attack of Schellenberg
Time for action – Creating the abstract base class
Time for action – Creating and spawning the boss
Time for action – I like you, I kill you last
Time for action – Rage mode activate
Summary


Chapter 7: Working with Kismet

263
264
264
271
275
279
282

283

Overview of Kismet
A simple introduction
Time for action – Using Kismet
Building complexity
Time for action – A more complex Kismet sequence
Time for action – Bug fixing time!
Kismet actions
Creating Kismet actions
Time for action – Creating Kismet actions
Using variables in Kismet actions
Time for action – Using variables in Kismet
Kismet handler functions
Time for action – Using handler functions
Time for action – Differentiating Kismet inputs
Kismet conditions
Time for action – What condition my condition was in
Kismet events
Time for action – The cleanup job

Creating and triggering a Kismet event
Time for action – Our first Kismet event
Giving the event some meaning
Time for action – Moving functionality into Kismet
Further expanding our Kismet
Time for action – Setting the wave size
Supported events
Time for action – Using SupportedEvents
Time for action – Creating a custom SupportedEvent
Latent actions
Time for action – Creating a latent action
Summary

Chapter 8: Creating Multiplayer Games
The server-client relationship

283
284
284
288
288
291
295
295
295
299
299
303
303
306

308
309
311
311
312
312
313
313
316
316
321
322
322
325
325
329

331
331

[ vi ]


Table of Contents

One state to bind us all
Testing network code
Setting up the server
Time for action – The server batch file
Setting up the client

Time for action – The client batch file
Fixing Awesome Game
Time for action – Unbreaking the player
Time for action – Unbreaking the game
The GameReplicationInfo class
Time for action – Making the GameReplicationInfo
Fixing enemy fleeing
Time for action – RUN AWAY!
Time for action – Bossing around
Replicating function calls
Replication function modifiers
Reliable versus Unreliable
Client functions

332
333
333
333
334
334
335
336
337
341
341
346
346
349
351
351

351
351

Time for action – Setting up for the client function
Time for action – Using the client function
Server functions

352
354
355

Time for action – Using a server function
Simulated functions

356
357

Time for action – Setting up the map
Time for action – Using simulated functions
Time for action – COMBO BREAKER!
Role and authority
Role and RemoteRole
Time for action – Examining Role and RemoteRole
Time for action – Respect my authority!
NetMode
Time for action – Checking the level's NetMode.
Replicating variables
The replication block
Time for action – Replicating a variable
Replication variables

ReplicatedEvent
Time for action – Using ReplicatedEvent
Summary

[ vii ]

358
359
360
362
362
363
364
365
365
366
366
367
368
369
370
372


Compiler errors
Time for action – Preparing for brokenness
Time for action – A new script package
Time for action – Breaking the class itself
Time for action – Breaking some more code
Time for action – Misleading errors

Time for action – Captain obvious to the rescue!
Time for action – Setting up a twofer
Time for action – Mal-function
Time for action – Taking care of other function errors.
Time for action – Actor variable errors
Time for action – Other variable errors
Debugging
Accessed none
Time for action – Dealing with Accessed None
Time for action – Fixing an Accessed None
Time for action – Accessed None in function parameters
Using the log
Time for action – Setting up a scenario
Time for action – Debugging using the log
Optimization
The profiler
Time for action – Using the profiler
Clock / UnClock
Time for action – Using Clock and UnClock
Best practices
Summary

Chapter 10: Odds and Ends
Using Components
Creating Components
Time for action – Adding a Component to an Actor
Time for action – Component compiler error
Interacting with Components
Time for action – Components as variables
A practical example

Time for action – Creating a toggleable flashlight
DLLBind
Time for action – Using DLLBind

373
374
374
374
376
377
380
381
382
384
386
387
390
392
392
392
394
396
397
397
400
404
404
404
409
409

410
412

413
413
414
414
419
420
421
424
424
426
426

Download from Wow! eBook <www.wowebook.com>

Chapter 9: Debugging and Optimization


Table of Contents

Final Thoughts
Other Resources
Summary

428
428
429


Appendix: Pop Quiz Answers

431

Chapter 1, Project Setup and Test Environments
Chapter 2, Storing and Manipulating Data
Chapter 3, Understanding the Class Tree
Chapter 4, Making Custom Classes
Chapter 5, Using Functions
Chapter 6, Using States to Control Behavior
Chapter 7, Working with Kismet
Chapter 8, Creating Multiplayer Games
Chapter 9, Debugging and Optimization
Chapter 10, Odds and Ends

Index

431
431
432
432
432
432
433
433
433
433

435


[ ix ]



Preface
Welcome to Unreal Development Kit Game Programming with UnrealScript! This book
teaches you how to program using the UnrealScript language so you can create your own
game projects using the UDK. Instead of using dry, hypothetical code you will use the topics
learned in each chapter to build an actual working game. By the end of the book, you will be
comfortable enough with the language to start working on projects of your own.

What this book covers

Chapter 1, Project Setup and Test Environments, guides you through the installation and
setup of the Unreal Development Kit as well as ConTEXT and UnCodeX, two programs that
we will use to write our code. We also examine the directory structure of the UDK and take a
look at the configuration files.
Chapter 2, Storing and Manipulating Data, covers the different types of variables we can
use in the UDK as well as the flow control statements we can use to react to our changing
environment.
Chapter 3, Understanding the Class Tree, examines the class tree so we can understand
the relationship between objects in the world. We learn about inheritance and function
overriding to customize our object's behavior.
Chapter 4, Making Custom Classes, focuses on the creation of classes of our own. The core
classes for any UDK project are discussed, and we create our own versions of them for our
game. We change how the camera works, what the rules of the game are, and how the
player is controlled.
Chapter 5, Using Functions, covers the use of functions to expand our game. Here we learn
how to pass information from one object to another, and how to manipulate that data
and return it to the original object. Custom functions are created for our objects to create

functionality that didn't exist in the original UDK classes.


Preface

Chapter 6, Using States to Control Behavior, covers states and how they can be used to
organize and control complicated behavior such as enemy classes for our game. Creating
states, changing states, and working with functions within states are discussed.
Chapter 7, Working with Kismet, discusses the use of Kismet in UDK games as well as the
creation of custom actions and events. These are used to demonstrate the power of Kismet
to tailor the gameplay to each individual level's needs.
Chapter 8, Creating Multiplayer Games, covers running a server and a client on a single
machine for multiplayer testing. We also cover the fundamentals of networking code and
how to design your game with multiplayer in mind.
Chapter 9, Debugging and Optimization, discusses common errors encountered when
compiling and running UnrealScript as well as solutions to these problems. Different ways of
optimizing code to make your game run faster are also discussed.
Chapter 10, Odds and Ends, covers the use of Components to customize the visual look of
objects in our game. The use of DLLBind to communicate with programs outside of the UDK
is also discussed.

What you need for this book

A computer capable of running the UDK is required for this book, see
for the
minimum requirements. ConTEXT and UnCodeX are included in the files with the book.
See for downloading code files.

Who this book is for


This book is for people who are new to the Unreal Development Kit and who wish to
create their own game projects using UnrealScript. The information here is also useful
to programmers having experience in another language and who wish to expand their
knowledge by learning UnrealScript.

Conventions

In this book, you will find several headings appearing frequently.
To give clear instructions of how to complete a procedure or task, we use:

[2]


Preface

Time for action – heading
1.

Action 1

2.

Action 2

3.

Action 3

Instructions often need some extra explanation so that they make sense, so they are
followed with:


What just happened?
This heading explains the working of tasks or instructions that you have just completed.
You will also find some other learning aids in the book, including:

Pop quiz – heading
These are short multiple choice questions intended to help you test your own understanding.

Have a go hero – heading
These set practical challenges and give you ideas for experimenting with what you
have learned.
You will also find a number of styles of text that distinguish between different kinds of
information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "We can play the example game that comes
with it to get an idea of what the UDK can do by going into C:\UDK\UDK-AwesomeGame\
Binaries\Win32 (or Win64 if we have a 64-bit operating system) and running UDK.exe"
A block of code is set as follows:
Class AwesomeActor extends Actor
placeable;
defaultproperties
{
Begin Object Class=SpriteComponent Name=Sprite
Sprite=Texture2D'EditorResources.S_NavP'
End Object
Components.Add(Sprite)
}

[3]



Preface

When we wish to draw your attention to a particular part of a code block, the relevant lines
or items are set in bold:
Begin Object Class=SpriteComponent Name=Sprite
Sprite=Texture2D'EditorResources.S_NavP'
HiddenGame=True
End Object
Components.Add(Sprite)

New terms and important words are shown in bold. Words that you see on the screen, in
menus or dialog boxes for example, appear in the text like this: "Let's click on Return to
Desktop for now."
Warnings or important notes appear in a box like this.

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—
what you liked or may have disliked. Reader feedback is important for us to develop titles
that you really get the most out of.
To send us general feedback, simply send an e-mail to , and
mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the
SUGGEST A TITLE form on www.packtpub.com or e-mail
If there is a topic that you have expertise in and you are interested in either writing or
contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support


Now that you are the proud owner of a Packt book, we have a number of things to help you
to get the most from your purchase.

[4]


Preface

Downloading the example code
You can download the example code files for all Packt books you have purchased from your
account at . If you purchased this book elsewhere, you can
visit and register to have the files e-mailed directly
to you.

Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do
happen. If you find a mistake in one of our books—maybe a mistake in the text or the
code—we would be grateful if you would report this to us. By doing so, you can save other
readers from frustration and help us improve subsequent versions of this book. If you
find any errata, please report them by visiting />selecting your book, clicking on the errata submission form link, and entering the details
of your errata. Once your errata are verified, your submission will be accepted and the
errata will be uploaded on our website, or added to any list of existing errata, under the
Errata section of that title. Any existing errata can be viewed by selecting your title from
/>
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt,
we take the protection of our copyright and licenses very seriously. If you come across any
illegal copies of our works, in any form, on the Internet, please provide us with the location
address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions
You can contact us at if you are having a problem with any
aspect of the book, and we will do our best to address it.

[5]



1

Project Setup and Test Environments
Introducing the Unreal Development Kit (UDK)
Epic Games' Unreal Development Kit is a powerful tool, but like any complicated
piece of software it can be overwhelming at first. This book will guide you
through the structure of the UDK and the basic principles of UnrealScript, and
by the end you will have the skills you need to start making your own games.
Let's get started!

It's a great time to get into the UDK and UnrealScript. With the release of the UDK, Epic
Games has opened up a great way for indie developers to make high quality games without
high quality budgets. With hard work and dedication even a small team of people can make
a great game in their spare time, and with digital distribution platforms such as Steam, it's
become much easier to self-publish and build a community of fans, and being able to sell
your game doesn't hurt either.
With constant updates to the UDK, Epic provides the latest features of the Unreal Engine for
free if you're just looking at game development as a hobby. If you're aiming to start up your
own development studio, the licensing terms for the UDK fit even in the smallest of budgets.

If you're looking for AAA quality, the UDK is where you'll find it.
Being an UnrealScript programmer is the most important job in a UDK project. Even without
artists you can build prototypes and demonstrate your gameplay using UnrealScript and
placeholder artwork using the assets included with the UDK. As they say, actions speak
louder than words, and having a fun game which people can play will help attract the art
talent to give your project the visual quality you need.
So with that, let's take a look at the Unreal Development Kit!


Project Setup and Test Environments

Before we start cooking let's set the table. There are a few things we need to do before we
write our first line of code.
In this chapter we will:
‹‹

Install the UDK and take a look at its directory structure.

‹‹

Learn what external programs we can use to code UnrealScript.

‹‹

Set up our first project.

‹‹

Compile and test a Hello World program.


Let's see what our computer needs to run the UDK.

System requirements
It doesn't take a top of the line computer to work with the UDK, but like any software
there are system requirements that we need to meet. According to the UDK website they
are as follows:

Minimum requirements:
‹‹

Windows XP SP2 or Windows Vista

‹‹

2.0+ GHz processor

‹‹

2 GB system RAM

‹‹

SM3-compatible video card

‹‹

3 GB free hard drive space

Make sure the computer we're working on meets these requirements, and then we can
install the UDK!


Time for action – Installing the UDK
Epic's official UDK website is the best place to stay up to date with the latest UDK releases
and features, so we'll be heading there for the download.

1.

Go to and get the latest release.

2.

Run the installer. It will ask us to accept the license agreement and where we want
to install the UDK. By default, it will use the UDK version for the installation, but to
help keep things organized it's better to use a project name. This helps if you have
more than one project using the same UDK version. In this book we will be calling
our project Awesome Game, so let's change the installation directory.
[8]


×