100% found this document useful (3 votes)
55 views

Get Learning C# programming with Unity 3D Alex Okita free all chapters

Okita

Uploaded by

cottitostaoe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
55 views

Get Learning C# programming with Unity 3D Alex Okita free all chapters

Okita

Uploaded by

cottitostaoe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Learning C# programming with Unity 3D Alex Okita

https://textbookfull.com/product/learning-c-programming-
with-unity-3d-alex-okita/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

C# Game Programming Cookbook for Unity 3D 2nd Edition Jeff


W. Murray

https://textbookfull.com/product/c-game-programming-cookbook-for-
unity-3d-2nd-edition-jeff-w-murray/

textboxfull.com

Learning C 7 By Developing Games with Unity 2017 Learn C


Programming by building fun and interactive games with
Unity Third Edition Micael Dagraca
https://textbookfull.com/product/learning-c-7-by-developing-games-
with-unity-2017-learn-c-programming-by-building-fun-and-interactive-
games-with-unity-third-edition-micael-dagraca/
textboxfull.com

Learning C by developing games with Unity 2020 An


enjoyable and intuitive approach to getting started with C
programming and Unity Fifth Edition Harrison Ferrone
https://textbookfull.com/product/learning-c-by-developing-games-with-
unity-2020-an-enjoyable-and-intuitive-approach-to-getting-started-
with-c-programming-and-unity-fifth-edition-harrison-ferrone/
textboxfull.com

Beginning Game AI with Unity: Programming Artificial


Intelligence with C# Sebastiano M. Cossu

https://textbookfull.com/product/beginning-game-ai-with-unity-
programming-artificial-intelligence-with-c-sebastiano-m-cossu/

textboxfull.com
Developing 2D Games with Unity: Independent Game
Programming with C# 1st Edition Jared Halpern

https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern/

textboxfull.com

Developing 2D Games with Unity: Independent Game


Programming with C# 1st Edition Jared Halpern

https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern-2/

textboxfull.com

Game Programming in C Creating 3D Games Creating 3D Games


Sanjay Madhav

https://textbookfull.com/product/game-programming-in-c-
creating-3d-games-creating-3d-games-sanjay-madhav/

textboxfull.com

Getting Started with Unity 2018 A Beginner s Guide to 2D


and 3D Game Development with Unity 3rd Edition Edward
Lavieri
https://textbookfull.com/product/getting-started-with-
unity-2018-a-beginner-s-guide-to-2d-and-3d-game-development-with-
unity-3rd-edition-edward-lavieri/
textboxfull.com

Deep Reinforcement Learning in Unity: With Unity ML


Toolkit 1st Edition Abhilash Majumder

https://textbookfull.com/product/deep-reinforcement-learning-in-unity-
with-unity-ml-toolkit-1st-edition-abhilash-majumder/

textboxfull.com
Learning C# Programming
with Unity 3D
Learning C# Programming
with Unity 3D
Second Edition

Alex Okita
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742

© 2020 by Taylor & Francis Group, LLC


CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed on acid-free paper

International Standard Book Number-13: 978-1-138-33681-0 (paperback)


978-1-138-33682-7 (hardback)

This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made
to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all
materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all
material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been
obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future
reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized
in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying,
microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.
copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400.
CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been
granted a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.

Library of Congress Cataloging‑in‑Publication Data

Names: Okita, Alex, author.


Title: Learning C# programming with Unity 3D / Alex Okita.
Description: Second edition. | Boca Raton, FL : CRC Press/Taylor & Francis
Group, 2019.
Identifiers: LCCN 2019010020 | ISBN 9781138336810 (pbk. : acid-free paper) |
ISBN 9781138336827 (hardback : acid-free paper)
Subjects: LCSH: Computer games--Programming. | C# (Computer program language)
| Unity (Electronic resource) | Three-dimensional display systems.
Classification: LCC QA76.76.C672 O43 2019 | DDC 794.8/1526--dc23
LC record available at https://lccn.loc.gov/2019010020

Visit the Taylor & Francis Web site at


http://www.taylorandfrancis.com

and the CRC Press Web site at


http://www.crcpress.com
Contents

1. Introduction: What This Book Is About ........................................................................................ 1


1.1 Why Read a Book: Why This Book May or May Not Be for You ......................................... 1
1.1.1 Do I Need to Know Math? ........................................................................................ 2
1.1.2 Programming as a Form of Expression .................................................................... 2
1.1.3 Games as a Stage with Lights ................................................................................... 2
1.2 Personal Information .............................................................................................................. 2
1.3 A Brief History of Computer Programming: How Programming Came to Be ..................... 3
1.3.1 Mechanical Computers ............................................................................................. 3
1.3.2 Logic ......................................................................................................................... 3
1.3.3 Computer Science ..................................................................................................... 3
1.3.4 The Origin of Software ............................................................................................. 4
1.3.5 Modern Computer Language .................................................................................... 4
1.3.6 The Future of Computer Languages ......................................................................... 4
1.4 C#: A Flexible Programming Language ................................................................................ 4
1.4.1 C# Paradigm ............................................................................................................. 5
1.4.2 Unity 3D: A Brief History of Game Engines ........................................................... 5
1.4.3 Why Use Unity 3D to Learn? ................................................................................... 6
1.4.4 How Does Unity 3D Use C#? ................................................................................... 6
1.4.4.1 How to Tell Unity 3D What to Do............................................................ 7
1.5 What Is Programming? ........................................................................................................... 7
1.5.1 What Does C# Look Like? ....................................................................................... 8
1.5.2 Learning to Copy and Paste ...................................................................................... 9
1.5.3 Iteration ..................................................................................................................... 9
1.6 Compiling: Turning Words into Computer Instruction .......................................................... 9
1.7 What We’ve Learned ............................................................................................................ 10
1.8 Leveling Up: The Path Ahead .............................................................................................. 10

2. Before You Begin .............................................................................................................................11


2.1 What Will Be Covered in This Chapter ................................................................................11
2.2 Unity 3D Installation ............................................................................................................ 13
2.2.1 Visual Studio Installation ....................................................................................... 15
2.2.2 Unity Version Numbering ........................................................................................16
2.3 Git SCM Installation ..............................................................................................................17
2.3.1 Git-SCM.com Git Software (Optional) ....................................................................17
2.3.2 GitHub.com Sign Up! (Also Optional) ....................................................................18
2.3.3 Git: The Basics ........................................................................................................ 20
2.3.4 Your First Repo ....................................................................................................... 22
2.3.5 Before Moving On .................................................................................................. 25
2.4 Unity General Overview ....................................................................................................... 26
2.4.1 The Main Window .................................................................................................. 27
2.4.1.1 Toolbar .................................................................................................... 27
2.4.1.2 Scene, Game View, and Asset Store....................................................... 28
2.4.1.3 Hierarchy Panel ...................................................................................... 28

v
vi Contents

2.4.1.4 Inspector Panel ....................................................................................... 29


2.4.1.5 Project and Console Panel ...................................................................... 29
2.4.1.6 Assets Directory ......................................................................................31
2.4.1.7 Project Settings Directory ...................................................................... 32
2.4.1.8 Packages Directory ................................................................................. 32
2.4.2 Git Ignore ................................................................................................................ 32
2.4.3 What We’ve Learned .............................................................................................. 32
2.5 Sample Code ......................................................................................................................... 33
2.5.1 Code Fragments ...................................................................................................... 33
2.5.2 Are Errors Bad? ...................................................................................................... 34
2.5.2.1 Compile-Time and Run-Time Errors...................................................... 34
2.5.3 How to Follow Along .............................................................................................. 35
2.5.4 Comments: A First Look ........................................................................................ 36
2.5.5 What We’ve Learned .............................................................................................. 37
2.6 Working with C#: Game Engines ......................................................................................... 37
2.6.1 Creating and Assigning a New C# File: Now It’s Your Turn ................................. 40
2.6.2 Naming Your New File ........................................................................................... 42
2.6.3 Using Your New File............................................................................................... 43
2.6.4 Additional Unity 3D Tools ...................................................................................... 48
2.6.5 Working in a Live Scene ......................................................................................... 50
2.6.6 Saving a Scene ........................................................................................................ 50
2.6.7 Opening a Scene ..................................................................................................... 50
2.6.8 What We’ve Learned ...............................................................................................51
2.7 Leveling Up: The Journey Has Just Begun............................................................................51

3. First Steps: Just Getting Started .................................................................................................. 53


3.1 What Will Be Covered in This Chapter ............................................................................... 53
3.2 Review................................................................................................................................... 54
3.2.1 Submitting Changes to Git ...................................................................................... 54
3.3 Let’s Build a Game—Part 1 ................................................................................................. 56
3.3.1 Movement Experiments .......................................................................................... 57
3.3.2 Reading Programmer Documentation .................................................................... 58
3.4 Tokens ....................................................................................................................................61
3.4.1 Writing C#............................................................................................................... 62
3.4.2 Comments a First Look........................................................................................... 63
3.4.3 Separator Tokens ..................................................................................................... 64
3.4.4 Operator Tokens ...................................................................................................... 65
3.4.5 Literals .................................................................................................................... 66
3.4.6 Transitive and Non-Transitive Operations .............................................................. 67
3.4.7 Putting It All Together ............................................................................................ 67
3.4.8 What We’ve Learned .............................................................................................. 69
3.5 Statements and Expressions .................................................................................................. 69
3.5.1 Expressions ............................................................................................................. 70
3.5.2 How Unity 3D Executes Your Code ....................................................................... 70
3.5.3 Thinking in Algorithms .......................................................................................... 70
3.5.3.1 Wash, Rinse, Repeat ............................................................................... 73
3.5.4 What We’ve Learned .............................................................................................. 73
3.6 Keywords .............................................................................................................................. 73
3.6.1 The Class Keyword ................................................................................................. 74
3.6.2 What We’ve Learned .............................................................................................. 76
3.7 White Space .......................................................................................................................... 76
3.7.1 Code Style ............................................................................................................... 78
3.7.2 What We’ve Learned .............................................................................................. 79
Contents vii

3.8 Code Blocks ........................................................................................................................ 79


3.8.1 What We’ve Learned .......................................................................................... 81
3.9 Classes: A First Look.......................................................................................................... 81
3.9.1 Objects ................................................................................................................ 82
3.9.2 What We’ve Learned .......................................................................................... 86
3.10 Variables ............................................................................................................................. 86
3.10.1 Identifiers ............................................................................................................ 87
3.10.2 Data ..................................................................................................................... 88
3.10.3 Variable Manipulation ........................................................................................ 89
3.10.3.1 Declaration Placement Examples ..................................................... 90
3.10.4 Dynamic Initialization ........................................................................................ 90
3.10.5 What We’ve Learned .......................................................................................... 91
3.11 Variable Names ................................................................................................................... 91
3.11.1 Useful Variables in Unity 3D .............................................................................. 94
3.11.2 Variable Assignment ........................................................................................... 96
3.11.3 Putting It Together: Using the Variables............................................................. 98
3.11.4 What We’ve Learned ........................................................................................ 100
3.12 Types: A First Look .......................................................................................................... 100
3.12.1 Value and Reference Types ............................................................................... 100
3.12.2 What We’ve Learned .........................................................................................101
3.13 Strong Typing.................................................................................................................... 102
3.13.1 Dynamic Typing ............................................................................................... 102
3.13.2 What We’ve Learned ........................................................................................ 103
3.14 Type Casting, Numbers .................................................................................................... 103
3.14.1 Explicit versus Implicit Casting ........................................................................ 104
3.14.1.1 A Basic Example ............................................................................. 105
3.14.2 What We’ve Learned ........................................................................................ 107
3.15 Comments ......................................................................................................................... 108
3.15.1 Line Numbers.....................................................................................................111
3.15.2 Code Folding ......................................................................................................111
3.15.3 Summary Comments .........................................................................................113
3.15.4 Navigating in Code ............................................................................................114
3.15.5 What We’ve Learned .........................................................................................115
3.16 Leveling Up: Moving on to Basics ....................................................................................115
3.16.1 Git Revision History ..........................................................................................115

4. Basics: The Building Blocks of Code ...........................................................................................117


4.1 What Will Be Covered in This Chapter ............................................................................117
4.2 Review ...............................................................................................................................117
4.3 Building a Game—Part 1 ..................................................................................................118
4.3.1 Mouse Point .......................................................................................................119
4.3.2 Move to Point .................................................................................................... 120
4.3.3 What We’ve Learned .........................................................................................121
4.4 Class Construction .............................................................................................................121
4.4.1 Class Declaration .............................................................................................. 123
4.4.1.1 A Basic Example ............................................................................. 123
4.4.1.2 Value and Reference Types ............................................................. 124
4.4.2 Adding Data Fields ........................................................................................... 125
4.4.3 Access Modifiers and the Dot Operator ........................................................... 125
4.4.4 Class Scope ....................................................................................................... 127
4.4.5 Class Members .................................................................................................. 128
4.4.6 What We’ve Learned ........................................................................................ 128
viii Contents

4.5 Directives .......................................................................................................................... 129


4.5.1 Libraries.......................................................................................................... 130
4.5.2 Ambiguous NameSpaces .................................................................................133
4.5.3 What We’ve Learned ...................................................................................... 134
4.6 Functions........................................................................................................................... 134
4.6.1 What Are Functions? .......................................................................................135
4.6.2 Unity 3D Entry Points .................................................................................... 136
4.6.3 Writing a Function .......................................................................................... 137
4.6.4 More on White Space and Tabs ...................................................................... 139
4.6.5 What We’ve Learned ...................................................................................... 140
4.7 Order of Operation: What Is Calculated and When ......................................................... 140
4.7.1 Evaluating Numbers ........................................................................................141
4.7.1.1 Math ...............................................................................................141
4.7.1.2 Operator Evaluation...................................................................... 144
4.7.2 What We’ve Learned .......................................................................................145
4.8 Scope: A First Look .......................................................................................................... 146
4.8.1 Class Scope ..................................................................................................... 146
4.8.2 Function Scope ................................................................................................149
4.8.3 Blank Scope .....................................................................................................151
4.8.4 What We’ve Learned .......................................................................................152
4.9 This ....................................................................................................................................152
4.9.1 A Basic Example .............................................................................................152
4.9.2 When This Is Necessary..................................................................................153
4.9.3 What We’ve Learned ...................................................................................... 154
4.10 Building a Game—Part 2 ..................................................................................................155
4.10.1 Additional Components ...................................................................................155
4.10.2 The Projectile ................................................................................................. 158
4.10.3 The Target ....................................................................................................... 160
4.10.4 What We’ve Learned .......................................................................................161
4.11 Logic and Operators ..........................................................................................................162
4.11.1 Booleans ..........................................................................................................162
4.11.2 Equality Operators ..........................................................................................163
4.11.3 Logical Not! .....................................................................................................165
4.11.4 Greater and Less than Operators .................................................................... 166
4.11.5 Logical And and Or Operators ........................................................................168
4.11.6 If and Branching ..............................................................................................169
4.11.7 What We’ve Learned .......................................................................................173
4.12 Loops .................................................................................................................................173
4.12.1 Unary Operators ..............................................................................................173
4.12.2 While ...............................................................................................................174
4.12.3 For ....................................................................................................................175
4.12.4 Do-While .........................................................................................................177
4.12.5 Postfix and Prefix Notation..............................................................................178
4.12.6 Using Loops .....................................................................................................179
4.12.7 Loops within Loops.........................................................................................182
4.12.8 Runaway Loops ...............................................................................................183
4.12.9 Breakpoints: A First Look ...............................................................................185
4.12.10 What We’ve Learned .......................................................................................186
4.13 ScopeAgain ........................................................................................................................187
4.13.1 Visibility or Accessibility ................................................................................187
4.13.1.1 A Basic Example ...........................................................................187
4.13.2 Global Scope....................................................................................................188
4.13.3 What We’ve Learned ...................................................................................... 190
Contents ix

4.14 Warnings versus Errors .................................................................................................... 190


4.14.1 Warnings ........................................................................................................... 190
4.14.2 Errors .................................................................................................................191
4.14.3 Understanding the Debugger ............................................................................ 192
4.14.4 What We’ve Learned ........................................................................................ 194
4.15 Leveling Up: Fundamentals .............................................................................................. 194
4.15.1 Style Guides ...................................................................................................... 194

5. Fundamentals: Building a Foundation ...................................................................................... 195


5.1 What Will Be Covered in This Chapter ........................................................................... 195
5.2 Review .............................................................................................................................. 196
5.2.1 Modular Code ................................................................................................... 199
5.3 Inheritance: A First Look ................................................................................................. 202
5.3.1 Class Members .................................................................................................. 204
5.3.1.1 Inheriting Members .......................................................................... 205
5.3.1.2 Is-A and Has-A ................................................................................. 206
5.3.2 Instancing .......................................................................................................... 206
5.3.3 Parent Child ...................................................................................................... 208
5.3.4 Object ................................................................................................................ 209
5.3.4.1 A Type Is Not an Object ....................................................................212
5.3.5 != null ................................................................................................................213
5.3.6 What We’ve Learned .........................................................................................214
5.4 Instancing...........................................................................................................................214
5.4.1 Class Initialization .............................................................................................215
5.4.2 New ....................................................................................................................215
5.4.3 Constructors .......................................................................................................216
5.4.4 What We’ve Learned .........................................................................................219
5.5 Static ................................................................................................................................. 220
5.5.1 A Basic Example ............................................................................................... 220
5.5.2 Static Variables ................................................................................................. 222
5.5.2.1 A Basic Example............................................................................... 222
5.5.3 Static Functions ................................................................................................. 224
5.5.4 Putting It All Together ...................................................................................... 225
5.5.5 What We’ve Learned ........................................................................................ 228
5.6 Building A Game—Part 3 ................................................................................................ 228
5.6.1 The Player ......................................................................................................... 228
5.6.2 The Mover ......................................................................................................... 229
5.6.3 The Shooter ....................................................................................................... 230
5.6.4 Physics Settings................................................................................................. 232
5.6.5 What We’ve Learned ........................................................................................ 233
5.7 Jump Statements and Return ............................................................................................ 233
5.7.1 Return................................................................................................................ 234
5.7.1.1 A Basic Example............................................................................... 235
5.7.2 Returning Objects ............................................................................................. 235
5.7.3 A Class Is a Type .............................................................................................. 236
5.7.4 Tuples ................................................................................................................ 238
5.7.5 Null Is Not Void ................................................................................................ 240
5.7.6 What We’ve Learned ........................................................................................ 240
5.8 Arrays: A First Look......................................................................................................... 241
5.8.1 Fixed-Sized Arrays ........................................................................................... 241
5.8.1.1 A Basic Example............................................................................... 241
5.8.2 Foreach .............................................................................................................. 246
5.8.2.1 A Basic Example............................................................................... 246
x Contents

5.8.3 Dynamic Initialization ...................................................................................... 246


5.8.4 Using the While Loop with Arrays................................................................... 247
5.8.4.1 Setting Array Values ....................................................................... 247
5.8.4.2 Getting Array Values ...................................................................... 248
5.8.5 What We’ve Learned ........................................................................................ 248
5.9 Jump Statements: Break and Continue ............................................................................. 248
5.9.1 A Basic Example ............................................................................................... 249
5.9.1.1 Continue .......................................................................................... 249
5.9.2 ZombieData ....................................................................................................... 249
5.9.3 Foreach Again ....................................................................................................251
5.9.4 What We’ve Learned ........................................................................................ 252
5.10 Multidimensional Arrays .................................................................................................. 252
5.10.1 Columns and Rows ........................................................................................... 253
5.10.1.1 A Basic Example ............................................................................. 254
5.10.2 A Puzzle Board ................................................................................................. 255
5.10.3 Camera and Screen Setup ................................................................................. 257
5.10.4 Checking Range ................................................................................................ 259
5.10.5 What We’ve Learned .........................................................................................261
5.11 Array List .......................................................................................................................... 262
5.11.1 A Basic Example ............................................................................................... 263
5.11.2 ArrayList.Contains() ............................................................................. 265
5.11.3 Remove.............................................................................................................. 266
5.11.4 Sort and Reverse ............................................................................................... 267
5.11.5 What We’ve Learned ........................................................................................ 268
5.12 Strings ............................................................................................................................... 268
5.12.1 Declaring a String ............................................................................................. 268
5.12.1.1 A Basic Example ............................................................................. 269
5.12.2 Escape Sequences ............................................................................................. 270
5.12.3 Verbatim Strings: @.......................................................................................... 271
5.12.4 What We’ve Learned ........................................................................................ 272
5.13 Combining What We’ve Learned ..................................................................................... 272
5.13.1 Timers ............................................................................................................... 273
5.13.2 What We’ve Learned ........................................................................................ 278
5.14 Leveling Up: On to the Cool Stuff.................................................................................... 278

6. Intermediate: The Tricks of the Trade ...................................................................................... 281


6.1 What Will Be Covered in This Chapter ........................................................................... 281
6.2 Review .............................................................................................................................. 281
6.2.1 Class Members .................................................................................................. 282
6.2.1.1 A Basic Example ............................................................................. 282
6.2.1.2 Thinking Like a Programmer ......................................................... 283
6.2.2 Return................................................................................................................ 286
6.2.2.1 A Basic Example ............................................................................. 286
6.2.3 Arguments aka “Args” (Not Related to Pirates) ............................................... 287
6.2.3.1 The Basic Example ......................................................................... 287
6.2.3.2 Multiple Args .................................................................................. 288
6.2.3.3 Using Args ...................................................................................... 290
6.2.4 Assignment Operators....................................................................................... 292
6.2.4.1 A Basic Example ............................................................................. 292
6.2.5 What We’ve Learned ........................................................................................ 294
Contents xi

6.3 Building a Game—Part 4 ................................................................................................. 294


6.3.1 ChaseCamera .................................................................................................... 294
6.3.2 Tile Floor Generator ......................................................................................... 295
6.3.3 What We’ve Learned ........................................................................................ 297
6.4 Class Constructors ............................................................................................................ 298
6.4.1 A Basic Example ............................................................................................... 299
6.4.2 What We’ve Learned ........................................................................................ 301
6.5 Arrays Revisited ............................................................................................................... 302
6.5.1 Using Arrays in Unity 3D ................................................................................. 302
6.5.1.1 Starting with 0 .................................................................................. 304
6.5.1.2 Mathf................................................................................................. 306
6.5.1.3 Time .................................................................................................. 307
6.5.2 Instancing with AddComponent(); .............................................................. 307
6.5.3 Type Casting Unity 3D Objects ........................................................................ 309
6.5.4 What We’ve Learned .........................................................................................311
6.6 Enums ................................................................................................................................311
6.6.1 Using Enums ......................................................................................................312
6.6.2 Combining What We’ve Learned ......................................................................315
6.6.3 What We’ve Learned .........................................................................................317
6.7 Switch ................................................................................................................................317
6.7.1 A Basic Example ................................................................................................318
6.7.2 Default ................................................................................................................319
6.7.3 More On Cases .................................................................................................. 322
6.7.4 Fall Through ..................................................................................................... 326
6.7.5 goto Case ........................................................................................................... 327
6.7.6 Finite State Machine ......................................................................................... 328
6.7.7 What We’ve Learned ........................................................................................ 330
6.8 Structs ............................................................................................................................... 330
6.8.1 Building a Struct ................................................................................................331
6.8.2 Struct versus Class .............................................................................................332
6.8.3 Without Structs ................................................................................................. 334
6.8.4 Handling Structs ................................................................................................335
6.8.5 Accessing Structs .............................................................................................. 336
6.8.6 Global Access ................................................................................................... 337
6.8.7 What We’ve Learned ........................................................................................ 337
6.9 Class Data ......................................................................................................................... 338
6.9.1 Character Base Class ........................................................................................ 339
6.9.2 Const ................................................................................................................. 340
6.9.3 Readonly ............................................................................................................341
6.9.4 What We’ve Learned ........................................................................................ 342
6.10 Namespaces ...................................................................................................................... 342
6.10.1 A Basic Example ............................................................................................... 342
6.10.2 Directives in Namespaces ................................................................................. 344
6.10.3 Ambiguous References ..................................................................................... 345
6.10.4 Alias Directives................................................................................................. 345
6.10.5 What We’ve Learned ........................................................................................ 346
6.11 Functions Again ................................................................................................................ 346
6.11.1 Parameter Lists ................................................................................................. 346
6.11.2 Side Effects ....................................................................................................... 347
6.11.3 Multiple Arguments .......................................................................................... 348
xii Contents

6.11.4 Useful Parameters ............................................................................................. 348


6.11.4.1 The Rule of Three ........................................................................... 349
6.11.5 Foreach versus For ............................................................................................ 350
6.11.6 What We’ve Learned .........................................................................................351
6.12 Unity 3D Execution Order .................................................................................................351
6.12.1 A Basic Example ................................................................................................353
6.12.2 Component Execution Order ............................................................................ 356
6.12.3 What We’ve Learned ........................................................................................ 358
6.13 Inheritance Again ............................................................................................................. 358
6.13.1 Function Overrides ........................................................................................... 359
6.13.1.1 A Basic Example ............................................................................. 359
6.13.2 Class Inheritance............................................................................................... 362
6.13.2.1 Sharing Common Attributes ........................................................... 362
6.13.3 Object ................................................................................................................ 364
6.13.4 What We’ve Learned ........................................................................................ 367
6.14 Type Casting Again .......................................................................................................... 368
6.14.1 (<Type>) versus “as” ......................................................................................... 368
6.14.2 User-Defined Type Conversion ......................................................................... 370
6.14.3 Implicit versus Explicit Type Conversion ......................................................... 372
6.14.4 What We’ve Learned ........................................................................................ 373
6.15 Working with Vectors ........................................................................................................374
6.15.1 Vectors Are Objects ...........................................................................................374
6.15.2 Stepping through MonsterGenerator .................................................................375
6.15.3 Gizmos .............................................................................................................. 377
6.15.3.1 A Basic Example ............................................................................. 377
6.15.3.2 DrawRay.......................................................................................... 379
6.15.4 Using Gizmos.................................................................................................... 380
6.15.5 What We’ve Learned .........................................................................................381
6.16 goto Labels .........................................................................................................................381
6.16.1 Skipping Down ................................................................................................. 383
6.16.2 Zombie State Machine ...................................................................................... 383
6.16.3 This as a Reference to Yourself ........................................................................ 386
6.16.4 Just for Zombies ................................................................................................ 386
6.16.5 What We’ve Learned ........................................................................................ 388
6.17 More on Arrays ................................................................................................................. 389
6.17.1 Length and Count.............................................................................................. 389
6.17.1.1 A Basic Example ............................................................................. 389
6.17.2 Foreach: A Reminder ........................................................................................ 390
6.17.3 Discovery .......................................................................................................... 393
6.17.4 Putting It Together ............................................................................................ 394
6.17.4.1 Breakpoint, Observing Inheritance ................................................ 395
6.17.5 What We’ve Learned ........................................................................................ 399
6.18 Out Parameter ................................................................................................................... 399
6.18.1 A Basic Example ............................................................................................... 401
6.18.2 Simple Sort (Bubble Sort) ................................................................................. 403
6.18.3 Simple Sort Proof .............................................................................................. 405
6.18.4 What We’ve Learned ........................................................................................ 406
6.19 Ref Parameter ................................................................................................................... 407
6.19.1 A Basic Example ............................................................................................... 407
6.19.2 Code Portability Side Effects............................................................................ 408
6.19.3 What We’ve Learned .........................................................................................410
Contents xiii

6.20 Type Casting Numbers ......................................................................................................411


6.20.1 Number Types ....................................................................................................413
6.20.2 Integers ...............................................................................................................413
6.20.2.1 Signed Numbers ...............................................................................414
6.20.3 Floating Point .....................................................................................................414
6.20.4 What We’ve Learned .........................................................................................415
6.21 Types and Operators ..........................................................................................................415
6.21.1 GetType() .......................................................................................................416
6.21.2 More Type Casting .............................................................................................417
6.21.3 Type Aliasing .................................................................................................... 420
6.21.4 Boxing and Unboxing ....................................................................................... 420
6.22 Operator Overloading ....................................................................................................... 422
6.22.1 A Basic Example ............................................................................................... 423
6.22.2 Overloading* ..................................................................................................... 424
6.22.3 Overloading < and > ......................................................................................... 426
6.22.4 What We’ve Learned ........................................................................................ 427
6.23 Controlling Inheritance .................................................................................................... 427
6.23.1 Sealed ................................................................................................................ 428
6.23.1.1 A Basic Example ............................................................................. 428
6.23.2 Extension Functions .......................................................................................... 429
6.23.3 Abstract ............................................................................................................. 430
6.23.3.1 A Basic Example ............................................................................. 430
6.23.4 Abstract: Abstract ..............................................................................................432
6.23.5 Putting This to Use ........................................................................................... 434
6.23.6 What We’ve Learned .........................................................................................435
6.24 Building A Game—Part 5 ................................................................................................ 436
6.24.1 Generating Rooms ............................................................................................ 436
6.24.2 What We’ve Learned ........................................................................................ 439
6.25 Leveling Up ...................................................................................................................... 440

7. Advanced ....................................................................................................................................... 441


7.1 What Will Be Covered in This Chapter ........................................................................... 441
7.2 Review .............................................................................................................................. 442
7.2.1 Moving Forward ............................................................................................... 442
7.3 Visual Studio .................................................................................................................... 442
7.3.1 The Debugger.................................................................................................... 443
7.3.2 Mono History .................................................................................................... 443
7.4 Function Overloading ....................................................................................................... 444
7.4.1 A Closer Look at Functions .............................................................................. 444
7.4.1.1 A Basic Example ............................................................................. 445
7.4.2 Function Signature ............................................................................................ 446
7.4.3 Different Signatures .......................................................................................... 447
7.4.4 Putting It Together ............................................................................................ 448
7.4.5 Not Quite Recursion.......................................................................................... 449
7.4.6 DrawWords........................................................................................................ 450
7.4.7 What We’ve Learned .........................................................................................453
7.5 Accessors (or Properties) ...................................................................................................453
7.5.1 Value ................................................................................................................. 454
7.5.1.1 A Basic Example ..............................................................................455
7.5.2 Set Event.............................................................................................................455
7.5.3 Read-Only Accessor ......................................................................................... 457
7.5.4 Simplification .................................................................................................... 458
7.5.5 What We’ve Learned ........................................................................................ 459
xiv Contents

7.6 Base Classes: Another Look ............................................................................................. 459


7.6.1 Generalization—Base Classes .......................................................................... 459
7.6.2 Specialization .................................................................................................... 461
7.6.3 Base ................................................................................................................... 461
7.6.4 Partial ................................................................................................................ 463
7.6.5 Protected, Private, and Public ........................................................................... 466
7.6.6 What We’ve Learned ........................................................................................ 467
7.7 Optional Parameters ......................................................................................................... 467
7.7.1 Using Optionals................................................................................................. 468
7.7.1.1 A Basic Example ............................................................................. 469
7.7.2 Optional Arguments ......................................................................................... 470
7.7.3 Named Parameters .............................................................................................471
7.7.3.1 A Basic Example ............................................................................. 472
7.7.4 Combining What We’ve Learned ..................................................................... 473
7.7.5 What We’ve Learned .........................................................................................474
7.8 Delegate Functions ............................................................................................................474
7.8.1 Delegates ............................................................................................................474
7.8.1.1 A Basic Example ..............................................................................474
7.8.2 Delegate Signatures............................................................................................476
7.8.3 Stacking Delegates .............................................................................................476
7.8.4 Using Delegates................................................................................................. 477
7.8.5 Updating Delegates ............................................................................................478
7.8.6 What We’ve Learned ........................................................................................ 480
7.9 Interface ............................................................................................................................ 480
7.9.1 Early Planning .................................................................................................. 480
7.9.1.1 A Basic Example ............................................................................. 481
7.9.1.2 Using Accessors .............................................................................. 483
7.9.2 Multiple Interfaces ............................................................................................ 486
7.9.3 IComparer ......................................................................................................... 488
7.9.4 Using IComparer ............................................................................................... 490
7.9.5 What We’ve Learned .........................................................................................491
7.10 Class Constructors Revisited .............................................................................................491
7.10.1 A Basic Example ............................................................................................... 492
7.10.2 When to Create a New Class ............................................................................ 495
7.10.3 What We’ve Learned ........................................................................................ 498
7.11 Preprocessor Directives .................................................................................................... 498
7.11.1 A Basic Example ............................................................................................... 499
7.11.2 UNITY_EDITOR ............................................................................................. 500
7.11.3 Mobile Development ......................................................................................... 502
7.11.4 Warning............................................................................................................. 504
7.11.5 Organizing ........................................................................................................ 505
7.11.6 What We’ve Learned ........................................................................................ 506
7.12 Try Catch Finally .............................................................................................................. 506
7.12.1 A Basic Example ............................................................................................... 506
7.12.2 Exception Messages .......................................................................................... 508
7.12.3 Custom Exceptions ............................................................................................510
7.12.4 Finally ................................................................................................................512
7.12.5 Try-Catch and Finally in Use.............................................................................513
7.12.6 What We’ve Learned .........................................................................................513
7.13 IEnumerator .......................................................................................................................514
7.13.1 Enumeration .......................................................................................................515
7.13.1.1 A Basic Example ..............................................................................515
7.13.1.2 What Doesn’t Work ..........................................................................517
Contents xv

7.13.2 Implementing IEnumerator ................................................................................517


7.13.3 What We’ve Learned .........................................................................................521
7.14 Generics .............................................................................................................................521
7.14.1 Generic Functions ............................................................................................. 522
7.14.1.1 A Basic Example ............................................................................. 522
7.14.1.2 Why T? ............................................................................................ 523
7.14.2 Making Use of Generic Functions .................................................................... 523
7.14.3 Generic Types ................................................................................................... 526
7.14.4 Var ..................................................................................................................... 527
7.14.5 Multiple Generic Values ................................................................................... 529
7.14.6 Anonymous Objects .......................................................................................... 530
7.14.7 What We’ve Learned .........................................................................................533
7.15 Events .................................................................................................................................533
7.15.1 A Basic Example ............................................................................................... 534
7.15.2 A Proper Event .................................................................................................. 536
7.15.3 EventArgs ...........................................................................................................537
7.15.4 Generic EventArgs .............................................................................................539
7.15.5 What We’ve Learned ........................................................................................ 540
7.16 Extension Functions .......................................................................................................... 540
7.16.1 Extensions ..........................................................................................................541
7.16.2 A Basic Example ................................................................................................541
7.16.3 Overloading with Extensions ............................................................................ 543
7.16.4 Magic Mirror .................................................................................................... 544
7.16.5 What We’ve Learned ........................................................................................ 546
7.17 Destructors ........................................................................................................................ 546
7.17.1 A Basic Example ............................................................................................... 547
7.17.2 Clearing Events or Delegates ............................................................................ 549
7.17.3 OnDestroy ......................................................................................................... 550
7.17.4 The Ugly Truth...................................................................................................552
7.17.5 What We’ve Learned ........................................................................................ 554
7.18 Concurrency or Co-Routines .............................................................................................555
7.18.1 Yield ...................................................................................................................555
7.18.1.1 A Basic Example ..............................................................................557
7.18.2 Setting Up Timers ..............................................................................................558
7.18.3 Arrays of Delegates ............................................................................................561
7.18.4 Stopping a Co-Routine ...................................................................................... 562
7.18.5 What We’ve Learned ........................................................................................ 565
7.19 Dictionary, Stacks, and Queues ........................................................................................ 565
7.19.1 A Basic Example ............................................................................................... 565
7.19.2 ContainsKey ...................................................................................................... 567
7.19.3 Stacks ................................................................................................................ 568
7.19.3.1 A Basic Example ............................................................................. 568
7.19.4 Movement Stack ................................................................................................ 569
7.19.5 Queues................................................................................................................571
7.19.6 What We’ve Learned ........................................................................................ 573
7.20 Lambda Expressions ......................................................................................................... 573
7.20.1 Anonymous Expressions ....................................................................................574
7.20.1.1 A Basic Example—Action Func and Expression ............................575
7.20.2 Lambda Expressions ......................................................................................... 577
7.20.3 A Queue of Lambdas ........................................................................................ 577
7.20.4 What We’ve Learned ........................................................................................ 578
7.21 Leveling Up ...................................................................................................................... 578
xvi Contents

8. Extended........................................................................................................................................ 579
8.1 What We’ll Be Covering in This Chapter ........................................................................ 579
8.2 Review .............................................................................................................................. 579
8.3 Readability .........................................................................................................................581
8.3.1 ?: Notation ..........................................................................................................581
8.3.2 If ........................................................................................................................ 582
8.3.3 Smell ................................................................................................................. 583
8.3.3.1 Comments ......................................................................................... 583
8.3.3.2 One Responsibility Rule ................................................................... 583
8.3.3.3 Duplicated Code ............................................................................... 584
8.3.3.4 Naming ............................................................................................. 584
8.3.4 What We’ve Learned ........................................................................................ 584
8.4 Git Revisited ..................................................................................................................... 584
8.4.1 Git Branch and Merge ....................................................................................... 585
8.4.2 Merge Conflicts ................................................................................................. 588
8.4.3 What We’ve Learned .........................................................................................591
8.5 Recursion ...........................................................................................................................591
8.5.1 A Basic Example ............................................................................................... 593
8.5.2 Understanding Recursion .................................................................................. 593
8.5.3 In Practice ......................................................................................................... 594
8.5.4 What We’ve Learned ........................................................................................ 601
8.6 LINQ ................................................................................................................................. 601
8.6.1 Lambdas and Arrays ......................................................................................... 601
8.6.1.1 A Basic Example .............................................................................. 601
8.6.2 Var ..................................................................................................................... 602
8.6.3 LINQ From ....................................................................................................... 603
8.6.4 Strange Behaviors in LINQ .............................................................................. 604
8.6.5 LINQ on Lists ................................................................................................... 605
8.6.6 Monsters Linq ................................................................................................... 608
8.6.7 What We’ve Learned .........................................................................................611
8.7 Reflection ...........................................................................................................................613
8.7.1 A Basic Example ................................................................................................615
8.7.2 Reflection MethodInfo .......................................................................................615
8.7.3 What We’ve Learned .........................................................................................618
8.8 Dynamic............................................................................................................................ 620
8.8.1 ExpandoObject ...................................................................................................621
8.8.2 Expando Reader ................................................................................................ 622
8.8.3 What We’ve Learned ........................................................................................ 623
8.9 Bitwise Operators ............................................................................................................. 624
8.9.1 Big Endian and Little Endian ........................................................................... 624
8.9.2 Signed or Unsigned ........................................................................................... 625
8.9.3 Bitwise Or | ....................................................................................................... 627
8.9.4 Enums and Numbers ......................................................................................... 627
8.9.5 Bitwise And & .................................................................................................. 628
8.9.6 Bitwise Exclusive Or ^ (xor) ............................................................................. 629
8.9.7 Setting Bitwise Flags ........................................................................................ 630
8.9.8 Bitwise Shortcuts | = and ^ = .............................................................................631
8.9.9 Bits in Numbers ................................................................................................ 632
8.9.10 Bit Shifting >> and << ..................................................................................... 632
8.9.11 What We’ve Learned ........................................................................................ 634
8.10 Attributes .......................................................................................................................... 635
8.10.1 A Basic Example ............................................................................................... 635
8.10.2 Custom Attributes ............................................................................................. 639
Contents xvii

8.10.3 Custom Attributes for Default Values............................................................... 639


8.10.4 Multiple Attributes ............................................................................................ 641
8.10.5 Putting Attributes to Work ................................................................................ 642
8.10.6 Attribute Flags .................................................................................................. 644
8.10.7 What We’ve Learned ........................................................................................ 645
8.11 Bitwise Math ..................................................................................................................... 646
8.11.1 Two’s Complement Unary Operator ~ .............................................................. 647
8.11.2 Bitwise Addition and Subtraction ..................................................................... 648
8.11.3 Bitwise Multiplication ....................................................................................... 650
8.11.4 Bitwise Tricks ....................................................................................................651
8.11.5 What We’ve Learned ........................................................................................ 652
8.12 Architectures and Organization ....................................................................................... 653
8.12.1 Planning Structure: Namespaces and Directories ............................................ 653
8.12.2 Divide and Conquer .......................................................................................... 654
8.12.3 Refactoring ........................................................................................................ 655
8.12.4 What We’ve Learned ........................................................................................ 656
8.13 Design Patterns ................................................................................................................. 656
8.13.1 Creational Design Patterns ............................................................................... 657
8.13.1.1 Singleton Pattern ............................................................................. 657
8.13.2 Structural Design Patterns ................................................................................ 657
8.13.3 Behavioral Design Patterns............................................................................... 658
8.13.4 What We’ve Learned ........................................................................................ 658
8.14 Continuing on Your Own.................................................................................................. 658

9. Stuff We Couldn’t Cover ..............................................................................................................661


9.1 The Extern Keyword..........................................................................................................661

10. Good Luck..................................................................................................................................... 663

Index ...................................................................................................................................................... 665


1
Introduction: What This Book Is About

This book was written as an answer for anyone to pick up a modern programming language and be pro-
ductive. You will be able to start a simple game in Unity 3D from scratch. By the end of this book, you
will have the basic skills to eventually become a capable unity game programmer, or at least know what
is involved with how to read and write some code.
You should have general computer skills before you get started. Come prepared; you’ll need a modern
Windows or OSX computer capable of running modern software with an internet connection. After this
book, you should be armed with the knowledge required to feel confident in learning more.
Each chapter has example code organized by chapter and section. We’ll try to make a fun project start-
ing with the basic functions of a typical game and we’ll see how the basic game can be expanded upon
and we’ll learn a bit about what’s involved with a larger project.

1.1 Why Read a Book: Why This Book May or May Not Be for You
You could go online and find videos and tutorials to learn; however, there is a disadvantage when it
comes to learning things in order and in one place. Most internet video or tutorial websites may gloss
over or dwell on a subject. You could skip ahead, but then what are you paying for?
Online content is often brief and doesn’t go into much depth on any given topic. It is incomplete or still
a work in progress. You’ll often find yourself waiting weeks for another video or tutorial to come out.
Just so you know, you should find the act of learning exciting. If not, then you’ll have a hard time con-
tinuing through to the end of this book. To learn any new skill, a lot of patience is required.
I remember asking an expert programmer how I’d learn to program. He told me to write a compiler.
At that time, it seemed rather unfair, but now I understand why he said that. It is like telling someone
who wants to learn how to drive Formula 1 cars to go compete in a race. In both cases, the “learning”
part was left out of the process.
It is very difficult to tell someone who wants to learn to write code where to begin. However, it all
really does start with your preparedness to learn. Your motivation must extend beyond the content of
this book.
You may also have some preconceived notions about what programming is. I can’t change that, but
you should be willing to change your thoughts on the topic if you make discoveries contrary to your
knowledge. Keep an open mind.
As a game developer, you should find the act of making a game, or simply learning how to make a
game, is just as fun as playing a game. If your primary goal is only to make that game that you’ve had in
your head for years, then you might have a problem. You may find this book a tedious chore if learning
C# is just something in the way of making your game.
Computer artists often believe that programming is a technical subject that is incompatible with
art. I find the contrary to be true. Programming is an art, much as literature and design are an art.
Programming just has a lot of obscure rules that need to be understood for anything to work.
No, programming is not too hard to learn. Writing a massive multiplayer online role-playing game
is quite hard. Learning how to write a simple behavior isn’t hard; like drawing, you start off with the
basics drawing spheres and cubes. After plenty of practice, you’ll be able to create a real work of art.
This applies to writing code, that is, you start off with basic calculations and then move on to the logic
that drives a complex game.

1
2 Learning C# Programming with Unity 3D

1.1.1 Do I Need to Know Math?


With complex rules in mind, does programming require the knowledge of complex mathematics? Unless
you are writing mathematical software, only a bit of geometry is required. Most of the examples here
use only a tiny bit of math to accomplish their purpose. Mathematics and programming do overlap quite
a lot in their methodology.
Math taught in schools provides a “best” solution. Programming results tend to behave a bit like a
math proof, but the proof isn’t just another bit of math. Rather, the proof of your code means that your
zombies chase after humans. A considerable amount of math required has been done for you by the com-
puter. It is just up to you to know what math to use, and then plug in the right variables.

1.1.2 Programming as a Form of Expression


There is a deeper connection between words and meaning in programming. This connection is more
mechanical, but flexible at the same time. In relation to a programming language, programmers like to
call this “expressiveness.”
When you write words in literature, you infer most if not all of the meaning. When programming
inference isn’t implied, then you’re free to define your own meanings for words and invent words freely.
One common merging of art and code appears within a video game. Anytime characters can react to
each other and react to user interaction conveys a greater experience. An artist, an animator, or an envi-
ronment designer should have some ability to convey a richer experience to the audience. A character
artist can show a monster to his or her audience; but to really experience the creature, the monster should
glare, grunt, and attack in reaction to the audience.

1.1.3 Games as a Stage with Lights


To do this, we’re going to use Unity 3D, a game engine. If you were to write a play, you would not have to
build the auditorium for your performance. Building lights and generating electricity to run them would
distract you from writing your play. Playwrights don’t often build lights and auditoriums from scratch,
and there are plenty of venues you could use as your stage, which is where game engines come in.
Game engines such as Unity 3D, Unreal Engine, Crytek, and even Game Maker, or Godot have a lot
of the basic game and math-related functions built for you. They are ready-built stages that allow you
to focus on making your game. The necessary code for rendering and physics have been done for you.

1.2 Personal Information


I started in the 1990s cleaning up sprites for Atari. Back then the job qualifications were basically having
Photoshop and a Wacom tablet. Oh, how times have changed!
I’ve worked for many different game studios, in both 2D and 3D, as an artist. I was exposed to many dif-
ferent software packages including Strata 3D on the Mac and Alias (PowerAnimator) on the SGI (Silicon
Graphics International), among many others. I loved learning new 3D modeling, rendering, and animation
applications. During my work as an artist, I used Maya, a popular 3D modeling package. While using
Maya, I was introduced to the Maya Embedded Language (MEL) script. Today I use Modo with Python.
After working as a character rigger and technical artist, I learned many programming languages
including MAXScript, MEL, and Python, which helped me to write tools for my fellow artists to use for
speeding up production.
I eventually moved into the job of programmer as my primary job function. I wrote prototypes for
Microsoft in Unreal using UnrealScript, a programming language much like JavaScript. This game studio
was run by John Gaeta and Peter Oberdorfer, who are award-winning movie special effects directors.
I began my career path as an artist, then became a technical artist, and finally a programmer.
This seems to be a common trend among a small variety of 3D artists like myself. I know quite a few
folks who have followed the same career path.
Introduction: What This Book Is About 3

During my transition from artist to programmer, I found that there was very little by way of help, docu-
mentation, web pages, or similar to complete the transition from an artist to a programmer. Many people
assumed that you were either a programmer to begin with, or not interested in making that transition.
After a long discussion, with my then girlfriend—now wife, about what a variable is, I decided it was
time to write a book.

1.3 A Brief History of Computer Programming:


How Programming Came to Be
Basic insight into the inner workings of your computer may help understand why and how some text gets
converted into computer instruction. The words that make up C#, or any other programming language
get converted into what is called byte code.
Skilled programmers tend to know more about the inner workings of their computer. Like any trade
or learned skill, the more you know about how your tools work, the better understanding of the entire
process you’ll have.
To understand why programming tools work as they now do stems from knowing how they used to
work. To understand how the current tools came about means knowing a bit of computer history and how
computers used to work.

1.3.1 Mechanical Computers


The first programmable computer is arguably the Babbage machine built by Charles Babbage in the
1820s. Made of tens of thousands of moving parts and weighing several tons, it was a gigantic mechani-
cal calculator that could display a 31-digit number. It was called the Difference Engine. in 1824 Babbage
won a gold medal from the British Astronomical Society, by using his gigantic calculator, for correcting
a set of tables used to chart the movement of planets through the sky.
In 1833, a countess named Augusta Ada King of Lovelace, commonly known as Ada Lovelace, met
Babbage at a party where she was introduced to the Difference Engine. Several years later, it became
evident that she understood the machine better than Charles himself. Despite being a housewife to the
Earl of Lovelace, she wrote several documents on the operation of the Difference Engine as well as its
upgraded version, the Analytical Engine. She is often considered to be the first computer programmer
for her work with the Difference Engine and its documentation.

1.3.2 Logic
In 1848, George Boole gave us Boolean logic. It would take nearly a century between the Difference
Engine and the first general programmable computer to make its appearance. Thanks to George, today
our computers count in binary (1s and 0s), and our software thinks in terms of true or false.
In 1887, Dorr Eugene Felt built a computing machine with buttons; thanks to him, our computers have
keyboards. In the 1890s, a tabulating machine used paper with holes punched in it representing 1s and 0s
to record the US census. Back then it saved US$2 million and 6 years of work.
There was a moment in time where a trinary computer existed. In 1840, Thomas Fowler created a compet-
ing mechanical computer which worked with −1, 0, and +1. Even as late as 1958 an electronic version was
created in the Soviet Union, but its usefulness lost out to binary which was easier to build and used less power.
In the 1900s, between Nicola Tesla and Alexander Graham Bell, modern computers were imminent
with the invention of the transistor. In 1936, Konrad Zuse made the Z1 computer, another mechanical
computer like that of Babbage, but this time it used a tape with holes punched in it like the tabulating
machine. He’d later move on to make the Z3 in 1941.

1.3.3 Computer Science


In the 1940s, Alan Turing, a British computer scientist, designed and built an encrypted message decoder
called the Bombe that used electromechanical switches for helping the Allied forces during World War II.
4 Learning C# Programming with Unity 3D

Between the 1930s and the 1950s, Turing informed the computer scientists that computers can, in theory,
solve anything calculable, calling this concept Turing completeness. All of these components were lead-
ing toward our modern computer.
In the mid-1940s, John Von Neumann demonstrated, with the help of his theory, that the computer
can be built using simple components. In this way, the software that controls the hardware can add the
complex behavior. Thanks to Tesla and Bell, the Z3 was made completely of electronic parts. It included
the first use of logic while doing calculations, making it the first complete Turing-complete computer.
In 1954, Gordon Teal at Texas Instruments introduced the silicon-based transistor, a key component for
building solid-state electronics that are used today in every computer.

1.3.4 The Origin of Software


The Z3 was programmed using Plankalkül, the first high-level programming language, invented by Zuse
shortly after he finished building the machine. In 1952, Grace Hopper created the first compiler software
that could translate human-readable words into machine operations. For the first time, the programmer
didn’t need to know how the transistors operated in the hardware to build software for the computer to
run. This opened computer programming to a whole new audience.
Between the 1950s and 1970s, computers and computer languages were growing in terms of complex-
ity and capability. In 1958, John McCarthy invented LISP (for LISt Processor) at the Massachusetts
Institute of Technology (MIT). Object-oriented programming appeared in Simula 67 in 1967. Imperative
and procedural programming made its appearance in Pascal in 1970.

1.3.5 Modern Computer Language


Bell Labs, started by Bell in the 1920s, hired Dennis Ritchie in 1967. In 1974, Ritchie published the
C programming language that has become the most popular computer programming language. In 1983,
Bjarne Stroustrup introduced C++ as “C with Classes.”
In 2001, Microsoft released C#, a combination of several programming language paradigms taking the
concepts from C, C++, LISP, Haskell, and others, and incorporating the ideas and concepts into a single
language. Today, C# is in its seventh major revision, whereas C++ is in its seventeenth revision. C# is
an evolving language having new features and concepts. Each revision makes the language more flexible
and most important, more expressive.

1.3.6 The Future of Computer Languages


The International Business Machines Corporation (IBM), a US company founded in the 1920s, is cur-
rently engineering new technology using particle physics discovered nearly a century ago. Modern spin-
tronics derived from the work of German physicists in the late 1920s.
Without particle physics and physicists such as Friedrich Hermann Hund, we wouldn’t understand
quantum tunneling, and the solid-state storage or universal serial bus (USB) thumb drive wouldn’t exist.
After the discovery and confirmation of the Higgs boson, you might ask “Why bother?” This question
might not get a good answer for another hundred years, but it’ll be worth it.
C# is not the last computer language; many others have been introduced just recently. Google has intro-
duced Kotlin specifically for developing apps for Android and Apple has introduced Swift for their iOS
platform. Microsoft, not to be outdone, introduced Q# for the future of quantum computing.
Using Q# for game development isn’t likely, at least for now. Programming languages have advantages
and disadvantages in different disciplines. The purpose of programming languages is to allow human
readable text to be converted into instructions that run on a computer’s CPU.

1.4 C#: A Flexible Programming Language


There are plenty of programming languages and each language was created to fulfill different roles.
Modern programming languages in use today are often described by their paradigms. A paradigm can
be explained as a sort of writing style; each style follows a different set of rules.
Introduction: What This Book Is About 5

1.4.1 C# Paradigm
In poetry, a haiku follows a 5-7-5 syllable tempo. A sonnet follows an iambic pentameter with 10 syl-
lables per line and rhymes in a specific pattern. Not all prose needs to end in rhyme like haiku, but many
do. Rhyming and tempo are paradigms used in poetry.
Modern programming paradigms include object oriented, functional, imperative, and logic program-
ming. Some programming languages use one or more of these paradigms together. For instance, F# is an
imperative, object oriented, and functional language. However, Haskell is purely functional.
C# (pronounced “see sharp”) is a combination of many different paradigms. As the name infers, C#
is a C-style language and primarily expressed using the object-oriented paradigm. How C# expresses its
different paradigms, and how you choose to write with it, is greatly up to you. Due to this flexibility, C#
offers a variety of uses reaching far beyond game development.
You may have heard of C++, another multiparadigm language. It is often used by programmers
who need detailed control over memory management and many other aspects of their final product.
This attention to detail and design is required to produce a refined final product.
C# is an imperative language, that is to say, it carries out operations in order. It is also object oriented,
so each object is allowed to do things on its own. Finally, it is also memory managed, aka garbage col-
lected, so your computer’s memory is organized for you. However, it does provide the flexibility to man-
age memory if you require that level of control. Just as well, C++ has recently added garbage collection
features to match C#.

1.4.2 Unity 3D: A Brief History of Game Engines


A game engine is basically software that provides many of the basic functions that are commonly used
to build a video game. Most game engines allow for rendering either 2D or 3D shapes by importing files
generated in a third-party software such as Blender, or 3D Studio Max or Maya, if you can afford them.
Another function often found in a game engine is the ability to write software for the game engine to run.
Unity 3D allows you to write software to run your game in C#.
3D game engines have a relatively short history in general computing. Their evolution has been hot
and fast. Starting around the 1990s, the 486 processor finally made its way into the general populace as
a processor fast enough to allow for everyone to play 3D games. Or at least pseudo-3D, 2D images drawn
in the shape of three-dimensional geometry. The technology for true 3D polygons would take a few more
years to make it onto the average person’s desktop.
Pseudo-3D was used to describe skewing and scaling flat sprites or 2D images to appear as though they
were in 3D space. True vertex transformation wouldn’t appear in popular video game engines on the PC
with games such as Quake 1 until 1995. This was greatly with the help of faster and enhanced graphics
processors. The modding community took Quake 1 and created many new additions and full conversions.
When powerful graphics processing units (GPUs) were introduced, it opened the floodgates for many
more 3D games to make their appearance. At first, these games functioned only on million-dollar com-
puters, but thanks to 3dfx, nVidia, Maxtrox, and ATi in the late 1990s, the GPU quickly took over being
a requirement for any 3D game. At that time, id Software, run by John Carmack, quickly came to the
forefront of the 3D gaming engine technologies that introduced many concepts such as spline surfaces,
complex shadows, and lighting with Quake 2.
Around 1998, Epic Games’ Unreal Engine and Valve Software’s Source Engine took off with the
launch of Unreal Tournament and Half-Life. More modding and conversions exploded onto the scene.
Many successful game modifications including Counter-Strike would go on to be one of the most played
online multiplayer games Valve Software would publish for years to come.
Before the availability of dedicated GPU and cheap RAM, computers didn’t have enough free resources
to support a game engine with editing tools. Often the game itself required so much of the computer that
there was little room left for any extras, such as particle editors or just-in-time compiling.
The modding community influenced business, making Unreal 2 and Source more modder friendly.
Garry’s Mod and Unreal Editor changed how game engines were made by adding tons of user-friendly
tools. Many users bought games specifically to “mod,” or modify, and create new games.
6 Learning C# Programming with Unity 3D

It’s no surprise that at this time many game engines would show up on the scene to cater to the mod-
ding community and the independent game developer. The large game engines found themselves top
heavy by focusing on large publishers. Companies such as RenderWare and Gamebryo found themselves
poorly positioned to compete against newcomers who focused on the independent developers such as
Unity 3D.
With the independent game developer in mind, many game engines focused on affordability and cross-
platform development. Tools such as Game Maker, Unreal Engine, and of course Unity 3D allowed
for many more people to learn and build games without the multimillion-dollar investment formerly
required for game production.

1.4.3 Why Use Unity 3D to Learn?


Making a game is more fun than making accounting software.
Having fun learning is more important as getting high test scores. After a test, you pretty much forget
the anguish of studying all night and what it was for. So there is no reason to make learning menial work
because studying should not be boring.
Once learning becomes entertainment, the lessons become something you want to remember. As a fun
experience, you remember the experience longer. I’ve found that learning how to write game artificial
intelligence (AI) and behavior is the best way to retain programming experience.
Programming lessons are often taught using stiff diagrams and simple math problems with little or no
immediate or obvious applicable use. How often have you actually needed to know where trains would
meet leaving two different cities at two different speeds? Experience shows that readers lose interest in
any example involving grade-point averages or a train leaving from New York.
Unity 3D offers examples by blowing things up. Calculus and vector examples are usually explained
using grids and lines. These concepts are lost by abstract pictures that lack meaning or application.
In this case, we’ll use math to solve for where a bad guy needs to throw a grenade in order to hit a mov-
ing target.
Finally, we’ll end up with both the knowledge that will help build a game and the skills needed to write
software outside of Unity 3D. With any luck, we’ll also have fun while learning. Some of these examples
may even help with your math classes.
Unity 3D also provides us with a complete set of tools we can use in our code quickly and easily. These
tools are a part of the Unity 3D application programming interface (API). The Unity 3D API is filled
with useful things such as commonly used routines, data structures, and classes. These parts of the API
are often called a library. We’ll learn what all that means in the remaining chapters.

1.4.4 How Does Unity 3D Use C#?


Not that long ago, Unity 3D supported three programming languages. First there was Boo, which
was given an accompaniment with JavaScript followed by C#. As of the latest version of Unity 3D,
C# is considered the primary, or first-class programming language. Both JavaScript and Boo have
been phased out.
In general, C# is gaining more support across many open source projects and has a growing user base.
The Dot Net Foundation which maintains .NET Core, is an open source project supported on multiple
platforms.
You might be surprised to learn that programming languages have version numbers. With each new
version of a language comes new features. As of the writing of this book, Unity 2018 and newer sup-
ports. NET 4.7 which is C# version 7.0. What does that mean? It’s not so simple, actually. Microsoft
created .NET (dot net): a framework or a standardized environment in which code can accomplish
common tasks with fewer commands. Using software without a framework often means reinventing
code that’s been written many times. The framework is also intended to be easily extended. Unity 3D
includes many game-related additions to help make building games easier.
Introduction: What This Book Is About 7

The progression looks something like this table:

January 2002 C# 1.0 .NET 1.0


April 2003 C# 1.2 .NET 1.1
November 2005 C# 2.0 .NET 2.0 Unity 3D 4.1
November 2007 C# 3.0 .NET 3.5
April 2010 C# 4.0 .NET 4.0 Unity 3D 5.6
August 2012 C# 5.0 .NET 4.5
July 2015 C# 6.0 .NET 4.6 Unity 3D 2017.1
March 2017 C# 7.0 .NET 4.7 Unity 3D 2018.1
August 2017 C# 7.1 .NET 4.7.1

As new versions of .NET were introduced, the language that went along with it was also upgraded,
though with different version numbers. Confusing? Yes. Unfortunately, to add to the confusion there’s
also three versions of .NET that include .NET Standard, .NET Framework, and .NET Core. The differ-
ences between these have no effect on what we’ll be learning in this book, so I’ll leave that research up to
you to do on your own. For now, we’re not so concerned with the version of either so long as the features
we learn about the C# language work within the context of Unity.
C# is used by a large community of general application programmers, not just game engineers. Unity
3D is also a cross-platform game engine, and a developer working on either OSX or Windows can both
share and use files for Unity 3D. C# is the same on both Mac and PC as well as Android and iOS for
mobile, so there is no concern over what operating system you’re going to use.

1.4.4.1 How to Tell Unity 3D What to Do


Unity 3D and Microsoft Visual Studio are going to be our integrated development environment (IDE).
IDEs come in many different forms. Many professional Windows programmers use Visual Studio, and
Apple developers usually use Xcode in OSX. Microsoft has also made Visual Studio available for OSX.
An IDE provides many tools that make programming easier by highlighting the lines of code and auto-
matically completing many commonly repeated tasks.
When it comes to learning about the nuts and bolts of programming, you’re basically learning about
how your computer thinks, or in more general terms, how your computer does what it is told. Somewhere
along the way you’ll realize how unintelligent your computer is and how smart good programmers are.
Programming is basically writing detailed instructions on what to do and when. In short, get data then
do something with the data. A big part of programming is figuring out where to get information; we’ll
call this data management. Then you use logic to determine what to do with the data once you have it.
Programming logic is organized into algorithms found in statements organized into functions.
Unity 3D provides tools to move characters and objects with your code. These assets all live in the
project directory as separate files or entities that you can control through code. Unity 3D provides scenes
to give structure to your assets—3D models particle systems for explosions, fire, and other special
effects—and tools to create and edit all these things. It is largely up to you to learn these different sys-
tems on your own, but this book covers the code part of things.
Unity doesn’t provide a set of 3D modeling or 2D image editing tools. For this you’ll want to find
something like Blender 3D which is free or use another 3D modeling and texturing tools set. You’ll also
want to look into Gimp or Dogwaffle to paint 2D images to import into Unity.

1.5 What Is Programming?


It’s all about writing code.
Programming is a process in which we organize data and use logic to do something with the data.
The data is everything a computer can store; they can range from numbers to zombie characters in a
video game. You do this by writing text into many files called source code. Source code written into text
files replaces punch cards used by the computing machines half a century ago.
8 Learning C# Programming with Unity 3D

When data is combined with logic and then written into a single file, it’s called a class. Classes are also
data and, as such, can be managed with more logic. Classes are used to create objects in the computer’s
memory and can be duplicated to have a life of their own.
Classes are used to build objects. Each piece of data within the class becomes a part of that object.
Different chunks of data inside of a class are called class members. Class members can also be chunks
of logic called functions or methods. If that is not clear right now, don’t worry we’ll go into detail on that
in Chapter 2.
Objects created from a class are called instances. In a game with a horde of zombies, each zombie is
duplicated or instanced from a zombie class. Each zombie has unique values for each attribute or data
element in the class. This means hit points and positions are unique for each duplicate zombie object.
Similar to families, objects can inherit properties from one another. The child, sometimes called a
subclass, inherits attributes from its parent. For instance, the child created from a zombie may inherit
the parent’s hunger for brains. To be useful, the child zombie can also add new objects and change the
objects it inherited from its parent class. As a result, now the child zombie might have tentacles that the
parent didn’t have.
Objects talk to each other through events and messages. Shooting at zombies can create an event, or
in programmer terms, it “raises” an event. The bullet impact event tells the zombie class to then take
necessary steps when hit by a bullet. Events command the class to take actions on its data, which is where
functions come in.
Functions, also known as methods, are sections of logic that act on data. They allow your class to cre-
ate additional events and talk to yet more objects. As the player presses the trigger and moves the joystick
around, yet more events can be raised and messages can be sent. Events and messages allow the player
to interact with your world; logic events and objects together build your game.

1.5.1 What Does C# Look Like?


A game in Unity 3D is made up of many different C# files. Each file can be referred to as a “class” and
ends with the .cs file extension (which stands for C Sharp). This isn’t a class like those that are taught. It is
more akin to biology in which classes are categories that define different groups of organisms.
Introduction: What This Book Is About 9

A basic game might have several different classes, each one named according to its purpose. Each one of
these classes contains the text that is your actual C# code. Just so you know, by default Windows will hide
the extensions of your files. As a result, if classes appear as simply Weapon or Monster and there is no exten-
sion.cs, then you should go into the Tools menu, select Folder Options/View, and enable file extensions.
As an exercise, we’ll be building a simple game involving monsters and shooting. We’ll start with a
basic skeleton with simple object classes and simple movement behaviors. From this we’ll build on some
basic events when the player presses keys, moves the mouse, and shoots at targets.

1.5.2 Learning to Copy and Paste


A good artist creates, a great artist steals.
Code examples exist for you to copy and paste into your project. The content for this book and all down-
loadable content are no different. You’ll have to understand what the code does and fit it to your needs.
Every programmer copies code habitually. Whether it is from code written in a previous project, or
code found on the internet. Programmers copy code to learn. Of course, it’s still important to know how
to write new code from scratch; most programming classes in school will prefer that you do this for all
your assignments. With practice, being able to recognize what to copy and where to paste is something
you’ll only know from writing code to begin with.
Programming is a constant learning process. It is a language to command computers. Anytime you
learn a new language, there will be plenty of words which you’ll have to look up. Add to this the fact that
every programmer gets to make up new words, and you’ve got a language that always needs a dictionary.
This is one reason why programmers are usually fast typists. This is also cause for programmers to be
picky about what keyboard they prefer.
In most cases, the projects in this book will be in some state where you can read text that is already
in place. Most of the projects are in a more complete state where you’ll be able to run them and see an
intended result.
As a programmer, I’ve gotten used to searching the internet for example code. Once I’ve discovered
something that looks useful, I copy that code and paste it into a simple test case. After I’ve wrapped my
head around what the code is doing, I rewrite it in a form that is more suited for my specific case.
Even if the code involves some fun trick, I’ll learn from that code. As I learn new tricks, I grow as a
programmer. The only way to learn new tricks is finding the necessity to solve a new problem for which
I haven’t already figured out a solution. Finding solutions to problems is a fundamental part of being a
programmer.

1.5.3 Iteration
Programming is an iterative process. It’s irregular to write an entire class or function without test-
ing it many times. The general flow is to write a part of a function, test it, add a bit more, test again,
and repeat until the function accomplishes its goal. Even then, many functions often work together in
sequence. Often, you’ll write a function, verify that it’s working, write another function, and verify some
more. Then you’ll have them work with one another and test again, all the while you’ll be changing and
re-arranging your code to fix mistakes.
Most of the time you’ll start off with a bunch of placeholders until you get around to finishing your
code. As you’re adding features and finishing code, you’ll have chunks of work that aren’t finished.
This is a part of game development and a part of learning as well.

1.6 Compiling: Turning Words into Computer Instruction


To test and use your code, it is time to compile. Compiling is the process of taking all of your source
files and building bytecode. Unity 3D uses .NET, as it is a compiler to generate the bytecode, but Unity
3D does this automatically. Mono is an open-source compiler that runs on many different processors and
operating systems.
10 Learning C# Programming with Unity 3D

The combination of the central processing unit (CPU) and operating system is often called a platform.
Each platform requires a unique native machine code to execute or run. Building code for each platform
is called a target. Unity 3D converts the bytecode into a native machine code and can target Mac, PC,
Android, and iOS.
Native machine code is the set of instructions that directly talk to the CPU and operating system (think
of the holes punched into a card and fed to a machine). Unity 3D is a simple way to generate the complex
set of instructions for your computer to run. Code that talks directly to the hardware is referred to as
“low-level” programming.
There are layers of software between you and the computer’s hardware. When writing C# for games
using Unity 3D, your code is compiled by .NET. Unity 3D then takes the bytecode from .NET and com-
piles a target platform into a native machine code.
Both Unity 3D and .NET are the layers underneath your code and the computer’s hardware, putting
you on a higher level. This is often referred to as a computer layer abstraction. That is why C# is usually
considered a high-level programming language.
Programming at a lower level requires much more knowledge of memory management and a wide
variety of other APIs that might include graphic cards, physics, sound, and everything else that runs a
game. Writing for a layer involves an in-depth knowledge of both the layers below and above the one
you’re writing for.
The computer hardware, such as CPU, graphics card, memory, and storage, live on the lowest level.
Above them is the basic input/output system (BIOS) and software that starts the hardware when you
press the Power button. Above that is your computer’s operating system and drivers that talk to the hard-
ware. Finally, Unity 3D lives above the operating system and your code lives above Unity 3D.
That is why we’re going to use Unity 3D to write games and not start from scratch in raw C++.
Otherwise, we’ll have to spend a few years learning about physics, rendering, and assembly language or
the instruction set that your CPU uses to operate.

1.7 What We’ve Learned


We should have an idea of what C# is, and what is required of you to learn it. If you haven’t done so
already, download and install the required software.
Unity 3D is free; there are no fees or royalties that need to be paid to the Unity 3D developers so long
as you’re not building any commercial product. Git is also free to use and you’ll need that to follow the
chapters on Git.
You’re even allowed to deploy your projects on anything Unity can target. Until you’re ready for a
commercial launch, the free license is all you’ll need for learning and sharing your game with your peers
on your computer.
Optionally, you may also want to setup an account on the Unity 3D site if you want to download and
use any of the assets from their Asset Store. A Visual Studio Community account and a GitHub account
may also come in useful later.

1.8 Leveling Up: The Path Ahead


The computer systems in place today have evolved from machines with moving parts. Telling these
machines what to do involved setting knobs and switches. Today, we use text organized into statements
and logic.
It is difficult to imagine a time when computers were so slow that simple calculations involved waiting
for clattering switches to finish moving around to produce an answer. Today, computers complete many
billions of calculations in a single second. All too often, we take for granted that computers rely on the
same concepts and systems engineered nearly a century ago.
Understanding how computers work puts into context how we write software. It is possible to write
software without this understanding, but it is harder to appreciate how it works.
Discovering Diverse Content Through
Random Scribd Documents
seemed never so repulsive, never so awful, although it had been my
lot to hold many a human skull in my hands before, in the medical
classes. The skeleton in uniform with shining buttons caused me to
shudder. “This is war,” I reflected, “and here is its symbol.”
The sun is burning and scorching me as before. My hands and face
have been smarting for some time. I drank up the remaining water.
The thirst tortured me so intensely that in trying to take a single
swallow I gulped down all. Fool that I was not to have called to the
Cossacks when they were so near! Even if they had been Turks, it
would have been better than this. They would have tortured me an
hour or two; but now there’s no saying how long I am to lie here and
suffer. My dear, dearest mother! If you only knew! You would tear
your gray hair out, you would knock your head against the wall, you
would curse the day of my birth, you would curse the world which
invented war and its sorrows.
It is well that you and Masha will not hear of my sufferings. Farewell,
Mother; farewell, my sweetheart, my love! But how sad and bitter I
feel! And there is something gnawing at the heart....
Again I am thinking of that little white dog! The porter did not pity it,
but knocked its head against the wall and threw it into a garbage
heap. And still it was alive; and suffered a whole day. I am more
unfortunate, because I have suffered already three days. To-morrow
will be the fourth day, then the fifth, the sixth.... Death, where art
thou? Come, come! Take me!
But death does not come. And I am lying in the blaze of this terrible
sun; and there is not a drop of water to refreshen my inflamed throat;
the corpse, too, is making me ill. Myriads of vermin are feeding in it.
How they swarm! When he is eaten, and there is nothing left except
the bones and the uniform, then it will be my turn. I too shall share
the same fate.
The day passes, and the night passes. No change. Again morning.
No change. Another day will pass....
The bushes are stirring and rustling, as if they were talking among
themselves. “You will die, you will die, you will die!” they whisper.
“You will not see, you will not see, you will not see!” answer the
bushes on the other side.
“No, you will not see them here!” I hear a loud voice quite near.
I tremble and at once come to myself. I look up, to find the good blue
eyes of our corporal Yakovlev looking at me.
“Spades!” he cries out. “There are two more of them here—and one
of them is theirs!”
“There is no need for spades, no need to bury me; I’m alive!” I wish
to cry out; but only a feeble groan issues from my parched lips.
“Lord! But he is alive! Barin[1] Ivanov! Children, come this way! Our
Barin is alive! And bring the doctor, quick!”
Presently I feel the pleasant contact in my mouth of water, whiskey,
and of something else. Then everything disappears.
The stretcher sways with a measured motion. This motion is
soothing. Now I recall myself, now everything lapses from my
memory. The bandaged wounds no longer hurt. An inexpressible
feeling of comfort has diffused itself through my entire body....
“Hal-t! L-lo-wer! Fresh hands to the stretchers! Now get hold—lift—
march!”
The command is issued by Peter Ivanich, our sanitary officer, a tall,
lean, and very kindly man. He is so tall that as I turn my eyes in his
direction I can see his head, his peculiar long beard, and his
shoulders, although the stretcher is being carried on the shoulders of
four big men.
“Peter Ivanich!” I whisper.
“What is it, dear fellow?”
Peter Ivanich leans toward me.
“Peter Ivanich, what did the doctor tell you? Will I die soon?”
“What are you saying, Ivanov? Of course you will live. Your bones
are whole. What a lucky fellow you are! Your bones are all right, and
so are your arteries! But tell me, how did you manage to pull through
these three and a half days? What did you eat?”
“Nothing.”
“And had you anything to drink?”
“I took the Turk’s flask. Peter Ivanich, I cannot speak now. Later....”
“Well, God be with you, dear fellow, and have your nap.”
Again sleep, forgetfulness....
When I awake again, I am in the division hospital, surrounded by
nurses and doctors. At my feet stands a man whom I recognize as a
celebrated St. Petersburg professor. His hands are blood-stained.
He is attending to me, and presently he turns to speak to me:
“Well, the Lord has been good to you, young man. You will remain
alive. We’ve deprived you of one leg; but that is a mere trifle. Can
you talk?”
Yes, I can talk, and I am telling him all that I have written here.
FOOTNOTES:
[1] A term of deference usually employed by peasants and
servants in addressing their master, or in speaking of him.
CHEKHOV, RECORDER OF LOST
ILLUSIONS

The history—that is, the philosophical history—of a national literature


is sure to reveal the close relation subsisting between the significant
social movements of that nation and its literature. Those who think
lightly of fiction as a force in a people’s life fail to recognize that in
the large it is something more than a mirror of the times, since
worthy fiction must be an expression—and that the most vivid
possible—of the ideals, the faiths, the scepticisms, the struggles, the
foibles, the prejudices, the occupations light and serious, and,
chiefly, the social ferment, of the era it represents, because out of,
and not merely during, that era its fiction was born.
While really no more applicable to Russia than to any other nation,
this representative quality of literature is more startlingly apparent in
Slavic literature than in any other. During the period just preceding
1880, the “back to the people” movement was at its height. Tolstoi’s
life among his peasants inspired many to imitation—but that is a
story by itself. Enough to note here that the movement broke down
of its own weight, as all social movements must which think to fill old
skins with new wine. And Anton Pavlovich Chekhov came to a full
though depressing inheritance of the stunned discouragement
characteristic of the early eighties. In common with his entire school,
Chekhov’s philosophy embraced three paramount tenets: The
“system” in Russia is productive of evil, and evil only; there is no
present hope of better things; but for the future, such hope as may
gestate unborn can come to birth only by the Russian people’s
facing the full truth honestly and fearlessly.
Here is a social philosophy which is something more than
pessimism, for while it believes that things must be worse before
they can be better, it neither denies nor predicts the coming of that
meliorated day. The true basis of Russian realism is thus seen to
differ from the French: French realism is sensational and of the
senses; that of Russia is intellectual and largely for a patriotic
purpose.

Chekhov was a south-Russian, born January 17, 1860, in Taganrog,


a seaport on an arm of the Black Sea, near the mouth of the river
Don. His father was a serf, whose ambition and ability led him early
to buy his freedom and provide for the education of his four children.
Anton passed through the local college and was graduated from the
school of medicine at Moscow, but more than his year as a hospital
interne, and a volunteer service during an epidemic of cholera, he
did not practise.
His medical education, however, set the tone for Chekhov’s literary
work, for he became a great pathologist of character, dealing chiefly
with those sick of mind and heart whom we are wont to think of as
unnormal. Early afflicted with the tubercular trouble which he
combated in vain, and which carried him off July 2, 1904, in
Badenweiler, Germany, at forty-four, he disclosed in his work, as
Professor Phelps has pointed out, the double character of the
observing physician and the sick patient. To the observer and in the
observed, in such a dual rôle, trivialities would assume a larger
interest than to the typical healthy man writing of complacent themes
in a rosy land. And so they did to Chekhov—as will more and more
appear.
While yet the youth was in the University (1879) he began to write
“fugitive sketches” for the minor metropolitan newspapers, and
eventually for the better-known Novoe Vremya and the St.
Petersburg Gazette. A humor keen, if somewhat coarse,
characterized these productions, which were often only a few
hundred words in length. This light satirical tone prevailed until after
the appearance in 1887 of his first book. Perhaps the critical
disapproval it aroused made him see that one who could write so
well might be better employed than in merely making people laugh,
as one reviewer expressed it. At all events, his later work was more
serious, though always a subtle, intellectual humor might be found—
for it often lurked—in his most sober fictional and dramatic writings.
Chekhov was so modest, so retiring, so diffident even, that he came
to his own by dint of sheer merit. When in the later years of his short
life he married Olga Knipper, the blonde beauty of the Théâtre Libre,
they took a villa at Yalta, on the Black Sea, for the husband’s
enfeebled health demanded a milder climate than that of the
metropolis. At Yalta, for a time, dwelt also Tolstoi and Gorki, and
there Chekhov learned to know his brother writers. With that sincere
big-heartedness which is happily characteristic of each of the
Russian littérateurs chosen for inclusion in this series, both did much
to bring his work to the attention of the public to which they were
themselves looking.
With Tolstoi’s convictions Chekhov had little in common, so he did
not seek him out. But the elder artist went to the younger, and a firm
friendship ensued. That the enthusiastic prophesies of both Tolstoi
and Gorki were not fully realized was doubtless due to the untimely
ending of a career so full of promise and of real literary achievement.

Naturally, Chekhov’s attitude toward life was something more


personal than was his conscious philosophy. The lost illusions of the
Russian people—I speak now of the Russia of the late eighties and
early nineties—were perfectly reflected in our author’s work. Of one
of his characters he writes:
The Student remembered that when he left the house his mother sat in the
hall, barefooted, and cleaned the samovar; and his father lay upon the stove
and coughed; and because it was Good Friday nothing was being cooked at
home, though he was tortured with desire to eat. And now, shivering with cold,
the Student reflected that just the same icy wind blew in the reign of Rurik, in
the reign of Ivan the Terrible, and in the reign of Peter the Great; and that there
was just the same gnawing hunger and poverty, just the same dilapidated
thatched roofs, just the same ignorance, the same boredom with life, the same
desert around, the same darkness within, the same sense of oppression—that
all these terrors were and are and will be, and that, though a thousand years
roll by, life can never be any better.

Could anything be more pitiful—and more hopeless! And yet it was


not the pity of it that Chekhov was picturing. It was the fatalism, the
mockery, the uselessness of struggle, the satire of even complaining,
that seemed to him to demand a voice. All contemporary life was
gray. To him it was a silly thing to seek to idealize it. The only course
was to view things as they are—the venom, the scurrility, the
disenchantment, the heart-break, the hunger, the chill of soul and
body, were real; then why delude self by renaming them, for alter
them one could not! Why struggle when inertia accomplished just as
much—that is to say, nothing! Why dream when the visions brought
one no nearer light than did waking! Again and again his characters
set out cheered by hopes and warmed by illusions, but one by one
they return, hardened, dulled, disenchanted. But even this
experience is not worth fretting about. The gaunt, wild-eyed men, the
flat, empty-breasted women, are products of the Russian system, so
why should they aspire to the unattainable? Let them be indifferent,
for that is the surest anæsthetic.
But in all this one feels the terrible arraignment of the god-of-things-
as-they-are, and no blame for the individual. Chekhov doubtless
pitied men, but he excoriated Russian society. If he laughed at
misery, it was that misery might not crush out the very life. If he
preached indifferentism, it was that the Juggernaut of society should
not pulverize those over whom its wheels must surely pass.
In the banalities of life and its useless beatings against the bars
Chekhov was quick to see effective literary material. If life was
colorless, it still called for a master of grays and neutral tints to lay
them effectively upon the canvas—and such a painter was Chekhov.
Dealing with trivial things, and dealing with them in a manner
sometimes bitterly laughing, again at times with fierce cynicism, but
sometimes too with the gentle sadness of an accepted despair, the
man became a sincere realist—an accurate delineator of “the
unprofitable life.” He could picture, in “The Steppe,” that most
monotonous of all landscapes with an idealized charm of variety
which enchanted the reader, but his obligation to human nature was
to paint it remorselessly with truth. Unhappily, his pathological mind
saw little but the contemptible, the trivial, the stupid, and the mean.
The nobler elements he did not omit, but he never asserted or even
intimated their final triumph. He could strip the shreds of pretension
and illusion from the soul of man as ruthlessly as a fiend would
denude the body of his helpless victim. For old age to be despicable,
or for youth to be polluted, was all the justification needed to picture
them just so upon his canvas.
“Ward No. 6”—a pitiless tragedy disclosing the ultimate break-down
of all that is noble in body, mind, and spirit—is probably Chekhov’s
greatest story. It takes its title from the lunatic asylum in a “squalid,
remote, and stagnant country town.... A pandemonium of brutality,
corruption, and neglect.” The patients suffer unspeakable abuses
from the attendants, chiefly from the porter, Nikita, whose brutal fists
beat all protesting patients into insensibility.
The old doctor used to sell the hospital stores to enrich himself, but
Ragin, the new physician, was a man of honesty, heart, and ability.
The abuses of the place he detests, and the sufferings of the
inmates make his gorge rise and his heart burn. But, as with most of
Chekhov’s good men, his will is inert, and at last he condones and
falls into indifference toward the horrors of the place.
One day he discovers an unusual intelligence in Gromof, one of the
long-time inmates, and comes to take a great interest in him. For
hours at a time he gives up his occupations and listens to Gromof’s
wisdom. The nurses, at this, think Ragin insane, and by a trick shut
him up in the very room whose terrible condition at first so inspired
him with horror. “I am glad! You drank other men’s blood; now they
will drink yours!” screams Gromof in a rage of madness.
After a short confinement, Ragin joins the other inmates in a revolt,
but Nikita uses his huge fists, and the next day Ragin is dead.
I recite this at some length because no shorter story could so fully
present the hopeless philosophy of its author. It is a powerful,
impressionistic picture of Russia—at its worst, let us hope.
Chekhov made several excursions into the drama, but he was not
given to plot, and all his efforts were subtle and intellectual, so that it
requires a company of brilliant actors to present his plays. The most
important are “The Cherry Orchard,” “The Seagull,” “The Bear,” and
“The Gray Stocking.”

In the short-story our author excelled, but here too his tendency was
not toward plot. The objectivist in fiction tends toward the
impressionistic sketch, and Chekhov was a master in sensing a
mood outside of himself and relentlessly reproducing the impression.
Of “Darling,” Tolstoi has said that the author intended to laugh at
Darling, sneer at her self-sacrifice; but in spite of his plan he had
created a character of beauty.
Olenka Plemyannikof, the daughter of a retired “college assessor,”
cannot live unless she is loving some one. She loves her father, her
mother, her relatives, and when at school she had fallen in love with
the French-master. Observing her rosy cheeks and kind expression,
and the naïve smile playing on her face when she is pleased, every
one feels attracted to her, and frequently women stop in the midst of
a conversation and grasp her hand, exclaiming, “You darling!”
Koukin, manager and proprietor of the Tivoli pleasure gardens,
occupies the wing in the Plemyannikofs’ house. Troubles connected
with rainy evenings, when his audiences are small, touch Olenka’s
kind heart, and she stays awake at night until he comes home, so
that she may smile encouragement through her window. At length
they marry, and their life runs smoothly, Olenka helping her husband
in many ways. Her radiant face alone draws people, and she tells
them that the theatre is the greatest thing in the world. “What a
wonderful man you are!” she says adoringly to her husband. But
when on a business trip to Moscow Koukin dies; and Olenka feels
then that the end of the world has come for her.
Three months after, returning from church one day, she meets Vassili
Andreyich Pastovalof, manager of a timber merchant’s yard, and he
tells her that she should bear submissively the fate which God willed.
His grave voice stays in her memory—and shortly afterward they are
married. They live happily, and now it seems to Olenka that she has
been in the timber trade all her life. She echoes her husband’s
opinions—whatever he thinks, she thinks, wherever he wants to go,
or not to go, she does the same. When her friends suggest
recreation, her reply is, “I and Vassichka have no time to frequent
theatres. We are business people, with no time for trifles. Besides,
what good is there in theatres?”
Thus they live harmoniously for six years. But one cold morning,
after drinking some hot tea, Pastovalof steps into the yard without his
hat and catches a chill. Four months later Olenka is again a widow.
Not till six months after her husband’s death does she remove her
weeds and open the house shutters, so great is her grief. Then it is
rumored that she takes tea with a regimental veterinary surgeon,
Smirnin, who occupies one of the wings of her house. He is
separated from his wife, but contributes to his son’s support. Olenka
becomes absorbed in this new interest, for she cannot live without
lavishing her affection on some one. Their happiness is interrupted
by Smirnin’s being called away with his regiment; and now the
woman is once more desolate.
The years pass and Olenka is entirely without fixed opinions, has
nothing to speak about, so she grows old-looking and dormant. She
has nothing to reflect. But one night Smirnin comes back. He has
retired from the army, is reunited with his wife, and wants to settle
down in the town. Olenka offers him her house free to live in, saying
that the wing is quite enough for her; so the man and the woman and
their child come to Olenka’s house. And in the little boy she finds an
object to love, even taking him into her own rooms, where they play
and study together. Then Olenka develops opinions on education,
and grows young again.

In his earlier days Chekhov espoused satirical comedy. In “A Work of


Art—The Story of a Gift” we have one of these typical nonsense
stories.
A young man, Alexander Smirnoff, enters the office of Dr. Koshelkoff,
his physician, and, with many expressions of profoundest gratitude,
presents him with an exquisite bronze candelabrum. The youth is the
only son of his mother, and out of the stock left by his father—for
they are carrying on his business in antiques—they have reserved
this treasure, which they now give to the physician because his care
had saved the young man’s life. Smirnoff’s one regret is that he does
not possess the mate, so as to give the doctor the pair.
The medical man is embarrassed. The piece is lovely, but—
improper. The two dancing female figures are quite too
unconventional for the doctor’s office—he has a wife, a family, a
mother-in-law, and lady patients! No, he cannot accept the gift. But
after many hurt protests on the part of the donor, the physician keeps
it anyhow.
No sooner is the young man gone than the doctor remembers a gay
bachelor lawyer to whom he owes many favors, and hurries off to
give him the beautiful but immodest bronze. The lawyer cannot
express his admiration—and regret. His patrons would be horrified, it
would injure his reputation. No, he cannot keep it.
The physician in turn is deeply wounded, so to save his friend’s
feelings the lawyer consents to keep it; and the doctor hurries off
chuckling in glee.
Immediately the lawyer presents the statuette to an actor. The
theatrical star is delighted, and soon his room is besieged by men
who want to see the savory work of art. But presently the actor sees
that he cannot receive lady visitors in the presence of such a
statuette.
“Sell it,” suggests a friend, and at once he despatches the offending
candelabrum to Madame Smirnoff, the old woman who deals in
antiques.
Two days later Dr. Koshelkoff sat peacefully in his study—when suddenly the
door of his room flew open, and Alexander Smirnoff burst upon his sight. His
face beamed with joy, he fairly shone, and his whole body breathed
inexpressible content.
In his hands he held an object wrapped in a newspaper.
“Doctor,” he began breathlessly, “imagine my joy! What good fortune! Luckily
for you, my mother has succeeded in obtaining a companion piece to your
candelabrum. You now have the pair complete. Mother is so happy. I am her
only son, you know. You saved my life.”
Trembling with joy and with excess of gratitude, young Smirnoff placed the
candelabrum before the doctor. The physician opened his mouth, attempted to
say something, but the power of speech failed him—and he said nothing.

Again in a different vein is “The Safety Match.”


Lieutenant Klausoff, a retired officer of the Horse Guards, who has
separated from his wife, Olga, on account of his own dissipations
and her shrewish temper, is reported as missing by Psyekoff, the
lieutenant’s agent. The examining magistrate, Chubikoff, and
Dukovski, his ambitious assistant, learn that Klausoff has not been
seen for a week, and when they break open his room all signs
suggest a murder. Young Dukovski, who is a disciple of induction as
a means of arriving at the facts of crime, discovers in the room one
boot, a burned safety-match, marks of teeth on a pillow, signs of
struggle about the bed, an unfastened window, footprints beneath it,
the mark of a knee on the window-sill, and some threads of blue
cloth caught in a burdock bush near-by. All these lead him to
conclude that the murderers, one of whom wore blue trousers,
climbed in the window, sprang upon Klausoff while he was taking off
his boots, smothered him with a pillow, and dragged him away. The
second boot is at length found near-by, and the investigators now
seek for the criminals. The shrewd Dukovski, who is continually
laughed at by his superior Chubikoff, infers that two of the murderers
are the valet and Psyekoff, the agent, because it developed that first
the valet and then Psyekoff had loved the same woman, whom their
master had finally won. Besides, Psyekoff wears blue trousers.
Jealousy must have been the cause, for the victim’s watch and
money still lay upon his table. When confronted with these facts and
a reconstruction of the deed, neither can make effective denial.
A third conspirator is found in the victim’s sister, who is a religious
enthusiast and intensely indignant that her rakish brother should be
living apart from his wife, Olga.
At last Dukovski succeeds in tracing the purchase of a box of safety
matches to Olga, whereupon he concludes that she also is
implicated. He and Chubikoff confront her with the circumstantial
evidences which indicate that she and her accomplices have
dragged off the body of her husband. Astounded, she breaks down,
and leads the officers into an adjoining room, where the body of
Klausoff is lying on a couch—asleep! The wife, who still loves her
tipsy lord, has dragged him away and holds him in durance so that
she may live with him whether he will or not.

Master of an alert, firm style, and skilled not only in penetration but in
effective expression, Chekhov has a place in Russian literature
which is less difficult to designate than is usual in the case of one
only a few years dead. Certainly his themes are neither big nor vital
enough, nor yet sufficiently human, to accord him position beside the
philosophical Tolstoi, the titanic Turgenev, and the iron-hearted
Dostoevski (a greater novelist than short-story writer). Rather do his
workmanship, power of characterization, and subtle, sardonic humor
point to a solitary niche close to the grim and morbid Andreev. His
appeal—always intellectual—to his own people is tremendous, and
in Germany his vogue is still important. It seems safe to say that
among Russian fictionists he stands in the first rank of the second
company.
To represent Chekhov’s work, I have chosen “In Exile,” which follows
complete in a new translation, because, while it exhibits all his
mature characteristics, it is less unpleasant on the one hand and on
the other less trivial than many of his other short-stories. But of its
qualities the reader may now judge.
IN EXILE
By Anton Chekhov
Old Simon, nicknamed Wiseacre, and a young Tartar, whom no one
knew by name, sat on the river-bank before a bonfire. The other
three ferrymen were in the hut. Simon, a man of sixty, gaunt and
toothless, but broad of shoulder and still hale in appearance, was
drunk. He had meant to go to sleep long ago, but there was a flask in
his pocket, and he feared that his comrades in the hut might ask him
to pass it around. The Tartar felt ill and tired; shivering in his rags, he
was recounting what a comfortable home he had had in his native
province, and what a handsome, clever wife he had left there. He
was hardly more than twenty-five years old, but now, before the
blaze of the bonfire, his pale, melancholy face seemed to be that of a
mere lad.
“It’s no paradise here, to be sure,” Wiseacre agreed with him. “You
can see for yourself: water, bare banks, and everywhere clay—
nothing more.... Holy Week has passed, there’s ice on the river, and
only this morning it snowed.”
“It’s miserable! Miserable!” said the Tartar, as he glanced round him
in terror.
Some ten paces away flowed the dark, cold river. It seemed to
grumble as it noised its way past the corroded clay bank and rapidly
bore itself onward somewhere towards the distant sea. At the very
edge of the bank there rose the dark, massive form of a barge, the
kind called karbass by the ferrymen. Looking in the distance towards
the opposite bank, one could see numerous fires, flaring and
retreating, and resembling so many leaping serpents. It was the
burning of last year’s grass. And beyond the fires, again darkness.
The sound of floating ice beating against the barge could be heard. It
was damp, cold....
The Tartar glanced up towards the sky. There were just as many
stars here as at home, and the same surrounding darkness; yet
there was something lacking. Somehow, at home, in the Simbirsk
province, there were no such stars and no such sky.
“It’s miserable! Miserable!” he repeated.
“You’ll get used to it!” said Wiseacre, and laughed. “You are still in
your teens, and silly. Your mother’s milk hasn’t as yet dried upon
your lips. Of course it seems to your foolish mind that there is no one
more miserable than you; but the time will come when you yourself
will say, ‘May God grant every one such a life!’ Now, look at me. In
another week the water will be normal again; I shall take charge of
the ferry-boat; you will go jaunting through Siberia, while I shall
remain here and resume making my way from bank to bank. I’ve
been doing it twenty-two years, night and day. The pike and the
salmon under the water; I above it. And thank God for that! I want
nothing. May God grant every one such a life!”
The Tartar threw more brushwood into the fire, and, moving closer to
it, said:
“I have an ailing father. When he dies, my mother and my wife will
join me here. They have promised.”
“What do you want with a mother and a wife?” asked Wiseacre.
“You’ll repent it, brother. It’s the devil that’s putting you up to it, curse
his soul! Don’t listen to him, the accursed one! Don’t give in to him.
When he gets your mind on women, just spite him; tell him, ‘I don’t
want them!’ When he talks freedom to you, get stubborn; tell him, ‘I
don’t want it! I want nothing—neither father, nor mother, nor wife, nor
freedom, nor house, nor anything! I want nothing, confound their
souls!’”
Wiseacre took another gulp from his flask, and continued:
“Now, look at me, brother. I am not a simple muzhik, but a sexton’s
son, in fact; and when I lived in freedom in Kursk I wore a frock-coat;
but now I’ve gotten so that I could sleep naked on the ground and
eat grass. And God grant every one such a life! I want nothing, and I
fear no one. I’m on good terms with myself, and I cannot imagine
any one richer and freer than I. When I was banished from Russia, I
insisted from the very first day: ‘I want nothing!’ The devil he talks to
me of wife, and of home, and of freedom; and I back at him: ‘I want
nothing!’ I insisted on mine, and, as you see, I live well, and do not
complain. Give way to the devil but an inch, and you are lost. There’s
no deliverance, you sink into the bog over your very head, and
there’s no getting out. Not alone your brother, the stupid muzhik; but
nobles and educated men are lost. Some fifteen years ago they sent
here one of that gentry. He didn’t share some property with his
brothers, tampered somehow with a will. They say he comes from
the dukes or the barons—or perhaps he is only an official—how
should one know? Well, this gentleman arrived here, and the first
thing he did was to buy himself a house and some land. ‘I intend,’ he
said, ‘to live by the sweat of my brow, because,’ he said, ‘I am no
longer a gentleman, but a convict.’ ‘Well,’ said I to myself, ‘may God
help him, he means well!’ He was at that time a fussy, bustling young
man; did his own mowing and now and then caught fish, and rode
sixty versts a day on horseback. That was his one misfortune. From
the very first year he made trips to Girino, to the post-office there.
Times were and he would be on my ferry-boat, sighing: ‘Ah, Simon,
it’s rather a long time since they have sent me money from home!’
‘There’s no need,’ I’d go on telling him, ‘of money, Vassili Sergeyich.
What good is it? Throw it aside,’ I argued with him. ‘All that’s gone
by; forget it as if it never were; as if you had only dreamt it; and begin
life anew. Don’t listen,’ I said to him, ‘to the devil. It’ll lead to no good;
it’ll only draw a noose around your neck. Now it’s money you want,
and later it’ll be another thing—there’s no end to it. If it’s happiness
you seek, first of all desire nothing. Yes.... If,’ I said to him, ‘fate has
treated you and me badly, there’s no begging charity of her, no
falling at her feet; rather should one treat her with scorn and laugh at
her—then she too will laugh.’ So I spoke to him.... Two years later I
ferried him over to this side—and he all overjoyed and laughing. ‘I
am going,’ he said, ‘to Girino to meet my wife. She has taken pity on
me,’ he said, ‘and is coming out here. She’s a fine woman, good-
hearted.’ He almost choked from happiness. The next day he
brought his wife. She was young, handsome, in a pretty hat; and in
her arms a girl baby; and all sorts of baggage with her. As to Vassili
Sergeyich, he fussed around her, couldn’t stop feasting his eyes on
her or stop raving about her. ‘Yes, brother Simon, even in Siberia
people live.’ ‘All right,’ I said to myself. ‘Don’t be too sure of that.’
And from that time on, mark it, he began to make weekly visits to
Girino: to see if any money had come from Russia. He needed no
end of money. ‘She,’ he said, ‘is sacrificing her youth and beauty in
Siberia for my sake, and is sharing with me my bitter lot; and
therefore,’ he said, ‘I should give her every possible pleasure.’ ... To
make it cheerful for her, he started up an acquaintance with the
officials and with all sorts of trashy people. Well, all this company
had to be furnished food and drink; then a piano had to be had, and
a shaggy little dog for the sofa—the deuce take it!... In a word,
luxury, extravagance! She did not live long with him. How could she?
Here she saw only mud, water, cold, no vegetables or fruits, and all
around her uneducated people, full of drink, and without manners—
and she a spoiled lady from St. Petersburg.... Naturally, she grew
sick of it. And the husband too was no longer what he had been, but
a convict.
“It was one Assumption Eve, three years later, that I remember some
one shouting from the opposite bank. I crossed over, and whom
should I see but the lady herself, all wrapped up—and with her a
young gentleman, one of the officials. A troika!... I ferried them over
to this side; the troika was ready; ah, but you should have seen them
fly! Hardly the wink of an eye and there was not a trace of them.
“And in the morning Vassili Sergeyich came running here. ‘Simon,
has my wife passed this way with a gentleman in spectacles?’ ‘Yes,
she did pass this way,’ I said to him. ‘Go and seek the wind in the
fields!’ He gave chase to them, but returned in five days. When I
ferried him across to the other side, he threw himself down in the
bottom of the boat, and began to beat his head against the planks
and to whine. ‘What else had you to expect?’ I said to him. I laughed
and reminded him: ‘Even in Siberia people live!’ But he only beat his
head the harder.... Then he began to hanker after freedom. He heard
his wife was in Russia, and of course he wanted to go there and to
take her away from her lover. Almost every day he would go to the
post-office or to the government offices. He presented petition after
petition, begging for pardon and for permission to return home. He
told me he had spent a couple of hundred rubles on telegrams alone.
He sold his land, while he mortgaged his house to Jews. He grew
gray and bent; his face yellow—a consumptive, in fact. Speaking to
you, he would always go: khe-khe-khe ... and his eyes full of tears.
For eight years he kept on handing in those petitions, but after that
he had come to life and grown jolly again. You see, he had thought
of another luxury. His daughter had grown up. And he feasted his
eyes on her and didn’t get enough of it. She really was an attractive
girl—pretty, black-browed, and rather spirited in manner. Every
Sunday he’d take her with him to Girino to church. They’d stand
hand in hand on the ferry, and he not taking his eyes from her. ‘Yes,
Simon,’ he would say, ‘even in Siberia people live. Even in Siberia
there is happiness. Just look what a daughter I’ve got! You can’t find
another like her if you seek a thousand miles around!’ The girl, as I
said, was really a beauty.... But I thought to myself: ‘Just wait....
She’s a young girl; the blood tingles, and one wants to live, and what
sort of life is to be had here?’ And, comrade, to make the story short,
she really began to ail.... She got to coughing, and coughing, to
pining away; and now she is very sick, can hardly stand on her legs.
Consumption! There’s your Siberian happiness for you—the deuce
take it!—that’s how even in Siberia people live.... Now he’s begun to
chase after doctors, and to bring them back home with him. Let him
but hear there’s a doctor or a healer within two hundred or three
hundred versts, and off he goes after him. It’s terrible to think how
much money he has spent on doctors. I’d rather drink up the
money.... She’ll die, any way. She’ll die, there’s no gainsaying that,
and then he’ll be lost altogether. He’ll hang himself from sorrow, or
he’ll escape to Russia—and then you know what will happen. He’ll
be caught, sentenced to hard labor; he’ll taste the knout....”
“That is well,” murmured the Tartar, trembling with cold.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like