Download Complete C# Game Programming Cookbook for Unity 3D 2nd Edition Jeff W. Murray PDF for All Chapters

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

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

com

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/

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.

Learning C# programming with Unity 3D Alex Okita

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

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

Unity Game Development: Programming C# in Unity Engine , a


guide book for beginners - 1nd edition - 2020 Kathleen
Peterson
https://textbookfull.com/product/unity-game-development-programming-c-
in-unity-engine-a-guide-book-for-beginners-1nd-edition-2020-kathleen-
peterson/
textboxfull.com

Unity Game Development Cookbook Essentials for Every Game


1 / converted Edition Paris Buttfield-Addison

https://textbookfull.com/product/unity-game-development-cookbook-
essentials-for-every-game-1-converted-edition-paris-buttfield-addison/

textboxfull.com
3D Game Programming for Kids Create Interactive Worlds
with JavaScript 2nd Edition Chris Strom

https://textbookfull.com/product/3d-game-programming-for-kids-create-
interactive-worlds-with-javascript-2nd-edition-chris-strom/

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

3D Game Programming for Kids Create Interactive Worlds


with JavaScript 1st Edition Chris Strom

https://textbookfull.com/product/3d-game-programming-for-kids-create-
interactive-worlds-with-javascript-1st-edition-chris-strom/

textboxfull.com
C# Game Programming
Cookbook for Unity 3D
C# Game Programming
Cookbook for Unity 3D
Second Edition

Jeff W. Murray
Second Edition published 2021
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
and by CRC Press
2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN
First Edition published by CRC Press 2014
CRC Press is an imprint of Taylor & Francis Group, LLC
© 2021 Taylor & Francis Group, LLC
The right of Jeff W. Murray to be identified as author of this work has been asserted by
him/ in accordance with sections 77 and 78 of the Copyright, Designs and Patents Act
1988.
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 copy-
right 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, access www.
copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please
contact [email protected]
Trademark notice: Product or corporate names may be trademarks or registered trade-
marks and are used only for identification and explanation without intent to infringe.
ISBN: 978-0-367-32170-3 (hbk)
ISBN: 978-0-367-32164-2 (pbk)
ISBN: 978-0-429-31713-2 (ebk)
Typeset in Minion
by SPi Global, India
This book is dedicated to my
amazing wife, Tori, and to
my boys, Ethan and William.
Boys, be nice to the cat and
the cat will be nice to you!
Contents

Acknowledgments xiii
Introduction xv
Prerequisites xvii

1. Making Games in a Modular Way 1


1.1 Important Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Manager and Controller Scripts . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1.1 Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1.2 Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1.1.1.3 Communication between Classes . . . . . . . . . . . 2
1.1.1.4 Public Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1.5 Private Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2 The Singleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2.1 Using the Singleton in Unity . . . . . . . . . . . . . . . . 3
1.1.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
1.1.4 Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.5 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.6 Finite State Machines (FSM) . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Where to Now? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

vii
2. Making a 2D Infinite Runner Game 9
2.1 Anatomy of a Unity Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2 Open Unity, Then the Example Project for This Chapter . . . . . . . . . . 10
2.2.1 A Few Notes on the Example Project . . . . . . . . . . . . . . . . . .12
2.2.2 Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.3 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Open the Main Game Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 The Game Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.5 Making Platforms to Run On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.6 Building the Player, RunMan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.6.1 Adding the RunMan Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.6.2 Adding Physics and Collisions to RunMan . . . . . . . . . . . . 23
2.6.3 Player Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6.4 RunMan Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.6.5 Animating RunMan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.6.6 Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.7 The Game Loop and the Game Manager . . . . . . . . . . . . . . . . . . . . . . . 28
2.7.1 Game States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.7.2 The Main Loop of RunMan_GameManager.cs . . . . . . . . . 29
2.7.3 Setting Up the RunMan_Game Manager
Component in the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.8 The Demise of RunMan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.9 Adding the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.9.1 Main Menu Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

3. Building the Core Game Framework 49


3.1 Controllers and Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.1 Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.2 Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.2 Core Framework Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.2.1 BaseGameManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.2.2 ExtendedCustomMonoBehaviour . . . . . . . . . . . . . . . . . . . . 56
3.2.3 BaseCameraController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4. Player Structure 61
4.1 A Player Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.2 Dealing with Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.3 User Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.3.1 The UserData Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.3.2 The BaseUserManager Class . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.4 The BasePlayerStatsController Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.5 Managing Players . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

5. Recipes: Common Components 75


5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.2 AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

viii Contents
5.3 Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.3.1 Third-Person Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.3.2 Top-Down Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.4 Game Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.4.1 GlobalRaceManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.4.2 RaceController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.5 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.5.1 Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.6 Level Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.7 ScriptableObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.7.1 ProfileScriptableObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.8 Spawning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.8.1 A Spawner Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.8.2 Trigger Spawning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
5.8.3 Timed Spawning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.9 User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.9.1 CanvasManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.9.2 ScreenandAudioFader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.9.3 MenuWithProfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.10 Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.10.1 AlignToGround . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.10.2 AutomaticDestroyObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
5.10.3 AutoSpinObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
5.10.4 FaceCamera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.10.5 LookAtCamera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.10.6 PretendFriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.10.7 TimerClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.10.7.1 Modifying the Timer to Update
Automatically����������������������������������������������������100
5.10.8 WaypointsController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.11 Weapons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

6. Player Movement Controllers 101


6.1 Humanoid Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6.2 Wheeled Vehicle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.3 Wheel Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.4 2D Platform Character Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

7. Weapon Systems 123


7.1 Building the Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
7.1.1 The BaseWeaponController Class . . . . . . . . . . . . . . . . . . . 124
7.1.2 BaseWeaponScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
7.1.3 Switching between Weapons . . . . . . . . . . . . . . . . . . . . . . . . 133

8. Waypoints Manager 137


8.1 Waypoint System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

Contents ix
9. Recipe: Sound and Audio 145
9.1 Audio Mixers and Mixing Audio in Unity . . . . . . . . . . . . . . . . . . . . . 145
9.1.1 Mixing Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9.1.2 Exposing Mixer Properties to Modify Them with Scripts . . 146
9.1.3 Audio Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
9.2 The BaseSoundManager Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
9.3 Adding Sound to Weapons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

10. AI Manager 155


10.1 AI States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
10.2 The BaseAIController Class – Base AI Control . . . . . . . . . . . . . . . . . 156
10.3 Enemy Stats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
10.4 The AIBotController Class – Making an Enemy Bot Controller . . 171
10.5 The AISteeringController Class – An AI Steering Controller . . . . . 177
10.6 Adding Weapon Control to the AI Controller . . . . . . . . . . . . . . . . . . 181
10.6.1 Attack States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
10.6.2 Base Enemy Weapon Controller . . . . . . . . . . . . . . . . . . . . .181

11. Menus and User Interface 187


11.1 Canvas Manager Class/Canvas Management . . . . . . . . . . . . . . . . . . . 187
11.2 The Main Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
11.2.1 Designing the Main Menu . . . . . . . . . . . . . . . . . . . . . . . . . .194
11.2.2 The BaseMainMenuManager Script . . . . . . . . . . . . . . . . . . 195
11.2.3 Combining BaseMainMenuManager to Use the
ProfileManager Script for Saving and Loading Audio
Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
11.3 In-Game User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

12. Saving and Loading Profiles 201


12.1 The Save Profile ScriptableObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
12.2 The BaseProfileManager Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

13. Loading Level Scenes 211


13.1 A Script for Loading Levels: LevelLoader.cs . . . . . . . . . . . . . . . . . . . . 212
13.2 Multiple Level Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
13.3 Building a Multi-Scene Level Loader in Unity . . . . . . . . . . . . . . . . . . 216

14. Racing Game 219


14.1 Ingredients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
14.2 Main Menu Scene Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
14.3 Level Select Scene Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
14.4 The Core Scene Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
14.4.1 Game Manager – The RaceGameManager Class . . . . . . . 224
14.4.2 Race Control – The GlobalRaceManager Class . . . . . . . . 238
14.4.3 Race Control – The RaceController Class . . . . . . . . . . . . . 245
14.4.4 Race Control – RacePlayerController Class . . . . . . . . . . . 253

x Contents
14.4.5 Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
14.4.5.1 Vehicle Audio����������������������������������������������������259
14.4.5.2 Incidental Sounds����������������������������������������������259
14.4.6 User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

15. Blaster Game Example 263


15.1 Ingredients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
15.2 Main Menu Scene Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
15.3 The Core Scene Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
15.3.1 The BlasterGameManager Class . . . . . . . . . . . . . . . . . . . . . 265
15.3.2 The BlasterPlayer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
15.3.3 Enemy Bots and the BlasterEnemyBot Class . . . . . . . . . . 277
15.3.4 Weapons and Projectiles . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
15.3.5 User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
15.4 Level Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
15.4.1 Spawning Enemies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Index 283

Contents xi
Acknowledgments

Thanks to all of my family, friends and everyone who has helped me with my
books, games and projects over the years: Brian Robbins, Chris Hanney,
Technicat, Molegato, NoiseCrime, Quantum Sheep, Mickael Laszlo, Juanita
Leatham, CoolPowers, Dave Brake, Sigurdur Gunnarsson, Kevin Godoy R.,
Lister, Wim Wouters, Edy (VehiclePhysics), IdeaFella, Liam Twose, RafaEnd,
Lynxie, Alistair Murphy, McFunkypants, Dani Moss, Malcolm Evans, Vinh,
Scott Wilson, Jon Keon, Dunky, Huck Z, Paradise Decay, Kryten, Silverware
Games, Pete Patterson, Dr. Not Oculus VR, Say Mistage, Andy Hatch, Lydia, RT
Warner, GermanRifter VR, Sylvain Demers, Kenneth Seward Jr, Dancer (Space
Dad), Steve Fulton, Paul Bettner, Derek Scottishgeeks, Jonny Gorden, Edward
Atkin, Ottawa Pete, Sam W., Dylan Stout, Shane McCafferty, Will Goldstone,
Sanborn VR, Gary Riches, J. Dakota Powell, Mayonnaise Boy, Stephen Parkes,
Ram Kanda, Alex Bethke, Itzik Goldman, Joachim Ante, Robert Scoble, Tony
Walsh, Andreas ‘BOLL’ Aronsson, Cat, Darrel Plant, Mike Baker, Rimsy, Cassie,
Christopher Brown, Phil Nolan, Pixel Hat Studio, Marcus Valles, Trev, Karyl,
Tami Quiring, Nadeem Rasool, Dwayne Dibley, Liz and Pete Smyth, Isaac and
Aiden, David Helgason, VR Martin, James Gamble, Vasanth Mohan, Simona
Ioffe, Alexander Kondratskiy, Tim and Paul, The Oliver Twins, Jeevan Aurol,
Rick King, Aldis Sipolins, Ric Lumb, Craig Taylor, Rob Hewson, Dani Moss,
Jayenkai (JNK), Matthew Kirubakaran, Elliot Mitchell, Ethan and William,
Pablo Rojo, Paul Bettner, AdrellaDev, Gordon Little, Ryan Evans, Sasha Boersma,
Matt Browning at Perfect Prototype, Hermit, Dirty Rectangles and the whole
Ottawa game dev community.

xiii
I would also like to sincerely thank Anya Hastwell, Thivya Vasudevan and
the team at SPI Global, the whole team at Routledge/CRC Press/AK Peters,
including Randi Cohen, Jessica Vega and Rick Adams, for making this book a
reality.
Thank you for buying this book and for wanting to do something as cool as
to make games. I wish I could tell you how awesome it feels to know that someone
else is reading this right now. I cannot wait to see your games and I sincerely hope
this book helps you in your game making adventures. Have fun making games!

xiv Acknowledgments
Introduction

The overall goal of this book is to provide a library of C# code with which to
jumpstart your projects and to help you with the overall structure of your games.
Many development cookbooks focus on only providing snippets of code, but,
here, we take a different approach. What you are holding in your hands right now
(or on your screen) is a cookbook for game development that has a highly flexible
core framework designed to speed up development of just about any type of Unity
project.
You might think of the framework as a base soup and the scripting compo-
nents as ingredients. We can mix and match script components and we can share
the same core scripts in many of them. The framework takes care of the essentials
and we add a little extra code to pull it all together the way we want it to work.
The framework is optional, however – you can use a lot the components indi-
vidually. If you intend on using the components in this book for your own games,
the framework could either serve as a base to build your games on or simply as a
tutorial test bed for you to rip apart and see how it all works. Perhaps you can
develop a better framework or maybe you already have a solid framework in
place. If you do find a way to develop your own framework, I say do it. The key to
game development is to do what works for you and your game projects – what-
ever it takes to cross the finish line.
I hope it helps you to make your games and tell your stories. I also hope you
remember to try to have fun doing it!

xv
Prerequisites

You can get up and running with the required software for the grand total of zero
dollars. Everything you need can be downloaded free of charge with no catches.
All you need is:

Unity Personal or Unity Pro version

C# programming knowledge.

This is not a book about learning how to program. You will need to know
some C# and there are several other books out there for that purpose, even if I
have tried to make the examples as simple as possible!

What this book doesn’t cover


This is not a book about learning to program from scratch. We assume that the
reader has some experience of the C# programming language. Know that I am a
self-taught programmer and I understand there may be better or alternative ways
to do things. Techniques and concepts offered in this book are meant to provide
foundation and ideation, not to be the final word on any subject.

xvii
1 Making Games in a
Modular Way

1.1  Important Programming Concepts


There are some things you will need to know to utilize this book fully. For a lot of
programmers, these concepts may be something you were taught in school. As a
self-taught coder who did not find out about a lot of these things until long after
I had been struggling on without them, I see this section as a compilation of
‘things I wish I’d known earlier’!

1.1.1 Manager and Controller Scripts


Sometimes, even I get confused about the difference between a manager and a
controller script. There is no established protocol for this, and it varies depending
on who you ask. Like some readers of the first edition of this book, I find it a little
confusing and I will try to clear that up.

1.1.1.1 Managers
Managers deal with overall management, in a similar way to how a Manager
would work in a workplace situation.

1.1.1.2 Controllers
Controllers deal with systems that the managers need to do their jobs. For exam-
ple, in the racing game example game for this book, we have race controller
scripts and a global race manager script. The race controller scripts are attached
to the players and track their positions on the track, waypoints, and other rele-
vant player-specific race information. The global race manager script talks to all

1
the race controller scripts attached to the players to determine who is winning
and when the race starts or finishes.

1.1.1.3 Communication between Classes


An important part of our modular structure is how scripts are going to commu-
nicate with each other. It is inevitable that we will need to access our scripts from
different areas of the game code, so you will need to know how to do that in Unity.

1. Direct referencing scripts via variables set in the editor by the Inspector
window.

The easiest way to have script Components talk to each other (that is, scripts
attached to GameObjects in the Scene as Components) is to have direct refer-
ences, in the form of public variables within your code. They can then be popu-
lated in the Inspector window of the Unity editor with a direct link to another
Component on another GameObject.

2. GameObject referencing using SendMessage.

SendMessage sends a message to a GameObject or Transform and calls a


function within any of its attached script Components. It only works one way, so
it is only useful when we do not need any kind of return result. For example:
someGameObject.SendMessage(“DoSomething”);

Above, this would call the function DoSomething() on any of the script
Components attached to the GameObject referenced by someGameObject.

3. Static variables.

The static variable type makes a variable accessible to other scripts without a
direct reference to the GameObject it is attached to. This is particularly useful
behavior where several different scripts may want to manipulate a variable to do
things like adding points to a score or set the number of lives of a player, and so on.
An example declaration of a static variable might be:
public static GameManager aManager;

You can have private or public static variables:

1.1.1.4 Public Static
A public static variable exists everywhere and may be accessed by any other
script.
For example, imagine a player script which needs to tell the Game Manager
to increase the current score by one:

1. In our GameManager.cs Game Manager script, we set up a variable,


public static typed:

public static gameScore;

2. In any other script, we can now access this static variable and alter the
score as needed:

GameController.gameScore++;

2 1.     Making Games in a Modular Way


1.1.1.5 Private Static
A private static variable is accessible only to any other instances of the same type
of class. Other types of class will not be able to access it.
As an example, imagine all players need to have a unique number when they
are first spawned into the game. The player class declares a variable named
uniqueNum like this:
Private static int uniqueNum;

When a player script is first created, it uses the value of uniqueNum for itself
and increases uniqueNum by one:
myUniqueNum = uniqueNum;
uniqueNum++;

The value of uniqueNum will be shared across all player scripts. The next
player to be spawned will run the same start up function, getting uniqueNum
again – only this time it has been increased by one, thanks to the player spawned
earlier. This player again gets its own unique number and increases the static
variable ready for the next one.

1.1.2 The Singleton
A singleton is a commonly used method for allowing access to an instance of a
class, accessible to all other scripts. This pattern is ideal for code that needs to
communicate with the entire game, such as a Game Manager.

1.1.2.1 Using the Singleton in Unity


The most straightforward way to achieve singleton-like behavior is to use a static
variable and, when Unity calls the Awake() function, set the instance variable to
become a reference to the script instance it represents. The keyword ‘this’ refers
to the instance of the script it is a part of.
For example:
public class MyScript : Monobehaviour
{
private static MySingleton instance;

void Awake()

{
if (instance != null)
Destroy(this);

instance = this;
}
}

The singleton instance of our script can be accessed anywhere, by practically


any script, with the following syntax:
MySingleton.Instance.SomeFunctionInTheSingleton();

1.1.3 Inheritance
Inheritance is a complex concept, which calls for some explanation here because
of its key role within the scripts provided by this book. Have a read through this
section but don’t worry if you don’t pick up inheritance right away. Once we get
to the programming it will probably become clearer.

1.1 Important Programming Concepts 3


The bottom line is that inheritance is used in programming to describe a method
of providing template scripts that may be overridden, or added to, by other scripts. As
a metaphor, imagine a car. All cars have four wheels and an engine. The types of
wheels may vary from car to car, as will the engine, so when we say ‘this is a car’ and
try to describe how our car behaves; we may also describe the engine and wheels.
These relationships may be shown in a hierarchical order:

Car–

–Wheels

–Engine

Now try to picture this as a C# script:

Car class.

Wheels function

Engine function

If we were building a game with lots of cars in it, having to rewrite the car class for
each type of car would be silly. A far more efficient method might be to write a base
class and populate it with virtual functions. When we need to create a car, rather
than use this base class, we build a new class, which inherits the base class. Because
our new class is inherited, it is optional whether we choose to override wheels or
engine functions to make them behave in ways specific to our new class. That is, we
can build ‘default’ functions into the base class and if we only need to use a default
behavior for an engine, our new class doesn’t need to override the engine function.
A base class might look something like this:
public class BaseCar : MonoBehaviour {

public virtual void Engine () {


Debug.Log(“Vroom”);
}

public virtual void Wheels () {


Debug.Log(“Four wheels”);
}
}

There are two key things to notice in the above script. One is the class declara-
tion itself and the fact that this class derives from MonoBehaviour. MonoBehaviour
is itself a class – the Unity documentation describes it as “the base class every
script derives from” – this MonoBehaviour class contains many engine-specific
functions and methods such as Start(), Update(), FixedUpdate(), and more. If our
script didn’t derive from MonoBehaviour it would not inherit those functions and
the engine wouldn’t automatically call functions like Update() for us to be able to
work with. Another point to note is that MonoBehaviour is a class that is built into
the engine and not something we can access to edit or change.
The second point to note is that our functions are both declared as virtual
functions. Both are public, both are virtual. Making virtual functions means that
the behavior in our base class may be overridden by any scripts that derive from it.
The behavior we define in this base class could be thought of as its default behav-
ior. We will cover overriding in full a little further on in this section.

4 1.     Making Games in a Modular Way


Let’s take a look at what this script actually does: if we were to call the
Engine() function of our BaseCar class, it would write to the console “Vroom.” If
we called Wheels, the console would read “Four wheels.”
Now that we have our BaseCar class, we can use this as a template and make
new versions of it like this:
public class OctoCar : BaseCar {
public override void Wheels () {
Debug.Log(“Eight wheels”);
}
}

The first thing you may notice is that the OctoCar class derives from BaseCar
rather than MonoBehaviour. This means that OctoCar inherits functions and meth-
ods belonging to our BaseCar script. As the functions described by BaseCar were
virtual, they may be overridden. For OctoCar, we override Wheels with the line:
public override void Wheels () {

Let’s take a look at what this script actually does: In this case, if we were to
call the Engine() function on OctoCar, it would do the same as the BaseCar class;
it would write “Vroom” to the console. It would do this because we have inherited
the function but have not overridden it, which means we keep that default behav-
ior. In OctoCar, however, we have overridden the Wheels() function. The BaseCar
behavior of Wheels would print “Four wheels” to the console but if we call
Wheels() on OctoCar, the overridden behavior will write “Eight wheels,” instead.
Inheritance plays a huge part in how our core game framework is structured.
The idea is that we have basic object types and specific elaborated versions of
these objects inheriting the base methods, properties, and functions. By building
our games in this manner, the communication between the different game com-
ponents (such as game control scripts, weapon scripts, projectile controllers, etc.)
becomes universal without having to write out the same function declarations
over and over again for different variations of script. For the core framework, our
main goal is to make it as flexible and extensible as possible and this would be a
much more difficult if we were unable to use inheritance.

1.1.4 Coroutines
Unity lets you run a function known as a coroutine, outside of the regular built
in functions like Update, FixedUpdate, LateUpdate, and so on. A coroutine is
self-contained code that will just go off and do its own thing once you have told
it to run. As it is running on its own, you can do some cool stuff like pause it for
a set amount of time and then have it start again, and coroutines are ideally suited
to time-based actions like fade effects or animations.
Here is an example of a coroutine (this example code comes from the Unity
engine documentation):
IEnumerator Fade()
{
for (float ft = 1f; ft >= 0; ft -= 0.1f)
{
Color c = renderer.material.color;
c.a = ft;
renderer.material.color = c;
yield return null;
}
}

1.1 Important Programming Concepts 5


This code loops the value of ft from 1 to 0 and sets the alpha value of a color
on a material to that value. This will fade out whatever renderer this script is
attached to, fading more as the alpha channel gets closer to 0. Notice the strange
line:

yield return null;

Do not be alarmed by this odd code above! All it does is tell the engine that,
at this point, the coroutine is ready to end this update and go on to the next one.
You need this in your code, but don’t worry too much about why at this stage.
I will go into some more detail below, but for now let us continue by looking at
how you would start this coroutine running:
StartCoroutine("Fade");

StartCoroutine() takes the name of the coroutine as a string. Once called to


start, the coroutine will do its thing and you do not need to do anything else. It
will either run until we tell it to stop, or it will run until the object is destroyed.
A key thing to note here is that the function is declared as type IEnumerator.
At first, I found this confusing, but it turns out that Unity coroutines use C#’s
built in system for iterator blocks. IEnumerator (Iterator Enumerator) was
intended to be a method for iterating through collections of objects. Coroutines
instead use IEnumerator to iterate through your coroutine code, starting or stop-
ping it between updates.
Another key point is that statement:
yield return null;

You can think of IEnumerator like a cursor making its way through your
code. When the cursor hits a yield statement, that tells the engine that this update
is done and to move the cursor on to the next block.
Another thing you can do with coroutines is to pause the code inline for a
certain amount of time, like this:
yield return new WaitForSeconds(1.0f);

Using WaitForSeconds(), you can quite literally pause the coroutine script
execution inline. For example:
IEnumerator DebugStuff()
{
Debug.Log("Start.");
yield return new WaitForSeconds(1.0f);
Debug.Log("Now, it’s one second later!");

Earlier in this section, I mentioned that the yield statement is used to tell
Unity the current update is done and to move on to the next. In the code above,
rather than just telling Unity to move on to the next bit of code, it tells Unity to
wait for the specified number of seconds first.

1.1.5 Namespaces
Namespaces compartmentalize chunks of code away from each other. You can
think of a namespace as a box to put your own scripts in, so that they can stay
separated from other code or code libraries. This helps to prevent possible

6 1.     Making Games in a Modular Way


overlaps between code libraries (APIs and so on) and your own code, such as
duplicate function names or variables.
To use namespaces, you wrap the entire class in a namespace declaration
like this:

namespace MyNamespace
{
public class MyClass() : Monobehaviour
{
}

When you want to refer to the code from another class, you need to be either
wrapped in the same namespace or to have the using keyword at the top of the
script, like:

using MyNamespace;

1.1.6 Finite State Machines (FSM)


A finite state machine is a commonly used system for tracking states. In video
games, they are used to track game states. Example states might be GameLoaded,
GameStarting, LevelStarting, and so on. The term finite state machine sounds a
lot more complicated than it usually is. The actual code often takes the form of a
long case statement that will execute code or call functions based on the current
game state. For example:

Switch(currentGameState)
{
Case GameStates.GameLoaded:
GameLoaded();
Break;
Case GameStates.GameStarting:
GameStart();
Break;

Having code in each case statement can get unruly. To counter this, I like to
split all of the code out into individual functions. This keeps the code tidy, easier
to manage, and easier to debug when things do not go to plan.

1.2  Naming Conventions


Naming conventions vary from programmer to programmer and even between
companies. Some games companies have strict guidelines on how code is con-
structed and how to name variables, where they can and cannot be used, and so
on, which is mostly in place to make it easier for other programmers to be able to
understand the code and pick it up when they might need to. You may not need
to take it that far, but I do feel that some organization of your naming helps. In
this book, I follow a very loose set of rules for naming variables – just enough to
make it easier for me to know quickly what type of variable I am looking at or
what its purpose might be. Those are:
Object references (Transforms, Components, GameObjects, and so forth)
are prefixed by an underscore (_) symbol. For example, a variable to hold a

1.2 Naming Conventions 7


reference to a Transform might be named _TR. A variable used to talk to a Game
Manager might be named _GameManager.
Most of the objects in this book (players, enemies, and more) derive from the
ExtendedMonoBehaviour class, which contains a few variables that are used
more commonly than others:

_RB Used to reference a Rigidbody attached to this object.

_RB2D For a 2D Rigidbody reference.

_TR The Transform of the object.

_GO The GameObject.

Where scripts need to be initialized before they can be used, in this book we
always use a Boolean variable named didInit which gets set to true after initial-
ization. You can use didInit to make sure initialization has completed.
Many programmers frown on the idea of declaring temporary variables, but
I like to have _tempVEC and _tempTR variables available for whenever I need to
refer to a quick Vector3 or another Transform inline. Having these variables
already declared is just a little quicker than having to declare a new Vector3 each
time, or to make a new Transform variable.

1.3  Where to Now?


Think about how your objects work together in the game world and how their
scripts need to communicate with each other. I find it helps to make flow dia-
grams or to use mind-mapping software to work things out beforehand. A little
planning early on can reduce a whole lot of code revisions later. That said, it is
perfectly O.K. for your structure to grow and evolve as you develop it further. The
framework shown in this book went through many iterations before reaching its
final form.
Try to break up your scripts into smaller components to help debugging and
flexibility. Also try to design for re-useability; hard-coding references to other
scripts reduces the portability of the script and increases the amount of work you
will need to do in the future when you want to carry it over into another
project.

8 1.     Making Games in a Modular Way


2 Making a 2D Infinite
Runner Game

This chapter is different from the rest of the book and a dramatic diversion from
what you may have seen in its first edition. Elsewhere in the text, I focus mainly on
the code behind the games. This chapter offers up a step by step tutorial to using the
framework to make a 2D infinite runner game. The goal of this chapter is to dem-
onstrate two things: 1) How quickly you can turn around a game when you have a
framework in place that takes care of a lot of the repetitive tasks. 2) The basics of
how this books framework fits together to give you some background knowledge
before we get down into nitty gritty of the framework code in Chapter 3.
This infinite runner (Figure 2.1) has a character that can move left, right, and
jump. Platforms are spawned off-screen and moved to the left to create the illu-
sion of movement.
I have already set up animations and imported the required graphics into the
example project to get things going quickly. As the focus of this book is more
toward code, project structure, and programming, I want to avoid using up too
many pages on making graphics or on Unity editor-specifics. The Unity docu-
mentation features plenty of content on this.
Players jump and move to stay on the platforms if possible, with the score incre-
mented at timed intervals. If the player falls off the bottom of the screen, the game ends.
To accomplish this, we will need:

1. The C# Game Programming Cookbook (GPC) framework, which


is included as part of this book as a download from Github at https://
github.com/psychicparrot/CSharpUnityCookbookExampleFiles

2. A platformer character capable of running left, right, and jumping

3. Platforms (auto moving to the left) and a method to spawn platforms off
screen
9
Figure 2.1 In the Infinite Runner example game, players jump and run to try to stay on the
platforms for as long as possible.

4. A Game Manager script to keep track of game state and deal with scor-
ing and so forth

5. Sounds for jumping and the game ending

6. An animated character

With just the framework and assets, by the end of this chapter, we will have
a working game.

2.1  Anatomy of a Unity Project


Unity projects are made up of lots and lots of files. A game is made up of
Scenes full of GameObjects. GameObjects are all the objects that make up a
game. GameObjects may take the form of anything from an empty place-
holder to a fully animated 3d model. Every GameObject has something called
a Transform, which holds information on the rotation, position, and scale of
the GameObject.
Scenes are essentially just files that hold information about which objects to
load at runtime. They can contain GameObjects, or they can contain Prefabs.
Prefabs are essentially files containing information about one or more GameObjects
to load as though they are combined. By using Prefabs, you can reduce the time it
takes to build multiple Scenes by combining multiple GameObjects into one, sin-
gle, Prefab. Think of Prefabs as templates you make yourself by arranging
GameObjects in the Scene and then creating Prefabs from them as templates to
load and use elsewhere. When you drag a Prefab into a Unity Scene, it will appear
as one or more GameObjects – replicating the original structure you created when
you made the original Prefab. Since game projects are made up of many files, a lot
of your time in Unity will be spent manipulating files, turning them into
GameObjects, and Prefabs and then organizing and laying them out in Scenes. In
the next section, we look at how Unity helps you to do this and how the editor is
structured for exactly these kinds of tasks.

2.2  Open Unity, Then the Example Project


for This Chapter
Open Unity. At the splash screen, choose to open an existing project and browse
to the folder named Chapter2_UnComplete_ProjectFiles, which contains the
example project for this chapter. Open the project. If you have any trouble along
the way, or you just want to try out the completed game, you can find the

10 2.     Making a 2D Infinite Runner Game


Random documents with unrelated
content Scribd suggests to you:
to fail. Then she who watched him always would see and
understand. And, since the customs of the East are always the
same, as “when the evil spirit from God was upon Saul, David took
an harp and played with his hand; so Saul was refreshed and was
well, and the evil spirit departed from him,” so did Mea take her harp
and play before Rupert and sing over him in that full, sweet voice of
hers, the old, old songs of Egypt, some of which she no longer
understood, till at length for a little while he forgot his sorrows and
was refreshed. Moreover, there came to him one happiness, his sight
returned.
He was lying on an angarib, or native bedstead, during the heat of
the afternoon in the large, cool room that had been given to him,
since if he sat up for long his mutilated leg still pained him, whilst
over against him, Mea sat upon a stool. She had been playing to
him, singing as she played; but now her harp-like instrument lay at
her side and she watched him in silence, her chin resting upon her
hand. He knew that she was looking at him, for he could feel her
gaze, and was amusing himself by trying to recollect the exact
fashion of her beauty, which he had really seen but thrice—in the
temple at Abu-Simbel; when she bade farewell to him in the pass;
and lastly, when she appeared again at the head of her charging
regiment and decreed the doom of Ibrahim and his brigands.
This last time did not count, however, for then she was quite
changed, a valkyrie, an animated Vengeance, not a woman. Now he
amused himself by attempting to reconcile these two countenances
that both were hers, and in wondering what sort of face she wore
to-day. Useless as it was, from old habit, he looked towards her
through the pitchy, maddening darkness that hemmed him in like a
wall.
He looked, and lo! he saw. On that darkness there appeared
something soft and cloudy, and he knew that it was a woman’s
outspread hair. Then within the frame of hay arose a ghost-like face,
and in it shining eyes, from which tears ran down, and on it such a
look of utter tenderness as he never, never had beheld. Oh! it was
beautiful, that face; always it would have been beautiful, but to this
man who, after those weeks of utter blindness, beheld it first of
anything, it was like a vision out of heaven. And the look upon it!
Surely that, too, had been borrowed from some pitying angel in
heaven. Rupert turned his head, then looked again, thinking that it
would be gone; but no, it still was there, and now he could even see
the hand beneath the chin and the quivering of the lips which strove
to stifle back a sob.
“Mea,” he said, in English, “why do you cry?”
She sprang up, dashing the tears from her eyes with the back of
her hand.
“I no cry,” she answered, in a merry voice. “Bey, you not hear me
cry.”
“No, Mea; but I saw you. Your cheek was all wet, and you sat with
your chin upon your hand.”
She uttered some Arabic exclamation of joy, then snatched the
linen veil or wimple from her head and threw it over his face.
“Look no more,” she said, “not good for your eye to look too much
or you go blind again. Also,” she added, with a happy laugh, “great
shame of you to spy upon a lady when she no think you see. You
should tell her first you going look, then she put on proper face.”
“I don’t want to see any other,” answered Rupert gently. “At first I
thought that I dreamt, and that it was an angel, only angels don’t
cry.”
“I think that angels cry always if they see down here—such a lot
to cry over, Rupert Bey.”
Then she tried to control herself for a moment, and failed
miserably, for she began to weep and sob outright. She threw
herself upon her knees beside him and said:
“You ask me why I cry, I (sob) tell you (sob) all the truth. Because
you made like that for me, and—I no can bear it, it break my heart,
for my heart love you very much, you want to die for me; I—I want
to live for you.”
Rupert sat up on the angarib, throwing the white veil from his
head, whereon, at once forgetting herself in the danger to his new-
born sight, she placed her little hand across his eyes, and held it
there. The crisis had come, and he knew it; but how to deal with it,
he did not know.
“Don’t do that, dear Mea,” he said, in a troubled voice. “If you
think the light will harm me, tie the veil over my eyes. Then we will
talk.”
She came behind him and obeyed, while Rupert felt her hot tears
falling on his hair. It was an awful moment, but he sat still, holding
the bedstead with his hand and uttering no tender word of the many
that rushed unbidden to his lips.
“Now, Mea,” he said, “sit down; no, not on the angarib—there on
the stool.”
“I sit,” she answered humbly. “Talk on.”
“Mea,” he continued, with a desperate effort, “all this won’t do.
You are sorry for me, and it upsets you and makes you say things
you must not. Mea, I am married.”
He stopped, but she made no answer. He began to wonder what
she was doing, or if she had gone away and left him, as he devoutly
hoped.
Unable to bear it any more he pushed the bandage from his
forehead. No; there she sat silent and pained.
“I am married,” he remarked again, not knowing what else to say.
Then she looked up and asked: “You hate me, Rupert Bey?”
“Of course not,” he answered indignantly; “very much the
reverse.”
“I thank you. Then you think me not nice, ugly?”
“Indeed, no. You are one of the most beautiful women I ever
saw.”
“I thank you,” she said again, “I like hear you say that though you
no mean it. Then you angry with me because you lose your foot and
eye and get your people killed, though that old Bakhita’s fault, not
mine.”
“Please don’t think so, Mea. It was not you or Bakhita, it was what
you call Kismet.”
“Yes, I think it Kismet too. Kismet all round, Kismet here,” and she
laid her hand upon her bosom. “Well, then, you not hate, you not
think ugly, you not angry, and I—oh! I love,” and she put such
tender passion into the word that the room seemed full of it—“and I
great lady too in my own place, I, Tama, not dirt-born. Why you no
take me? See now!” and she stood up before him and turned slowly
round, “I not beautiful as you say, too small, too thin, but I not so
bad! I make you good wife, I give you children, I love you always till
I die. My people hate strangers, still they very glad you take me,
they love you too, they praise you much, they think you bravest man
in world; my emirs ask me this morning if I married to you yet as
they want make feast.”
Rupert pulled down the bandage over his eyes again; he thought
it best, muttering something about the light hurting him.
“Mea,” he said, in despair, “don’t you understand that I am already
married?”
“What that matter?” she asked. “Man can have two wives, four if
he like.”
“He can’t,” answered Rupert. “I beg you—don’t go on. It is not
right; it is more than I can bear. By our English law, he can only
have one wife, no one else—no one at all. You must have heard it.”
“Oh, yes, I hear, there at Luxor, but I think that all silly missionary
talk. White people do many things they say they should not do—I
see them and make note. Who know how many wives you have? But
if you no want me, mafeesh—all done with. I not trouble you any
more; I go and die, that all.”
“Unless you stop soon, you will make me go and die,” he said
faintly. “Mea, it is cruel of you to talk like that. Listen now, and do
not be angry, do not think that I am treating you ill. Oh, my dearest
friend, sit there and listen!”
Then giving up English, in which she would have found it difficult
to understand his arguments, he addressed her in Arabic,
expounding our Western doctrines and showing her that what she
thought right and proper, in the West was held a crime; that he had
passed his word, and it could not be broken, that he would rather
die than break it, that his honour was on it, and that if he violated
his honour, his soul would be as scarred and mutilated as his body
was that day.
Mea listened intently, and at last began to understand.
“Now are you angry with me?” he ended. “And do you still wish
me to stay here when I tell you that if I do there must be no more
of this love-talk between us which, in the end, might bring me to
ruin? If that is too much to ask, then to-morrow I go hence into the
desert to—” and he stopped.
“Nay,” she replied, in Arabic, “I am not angry with you, Rupert
Bey. I am angry with myself who tempted you to break your own
law. Oh! you are good, the best of men that I have known, and I will
learn to be good like you—only tell me not that I must cease to love
you, for that I cannot do; and oh! speak no more of going hence
into the desert to die, for then I should die also. Nay, bide here and
be my friend and brother since you may not be my husband. Stay
and forgive me who am ignorant, who have other customs, and was
not taught thus. Say that you will stay.”
“Yes,” he answered, in a hoarse voice, for he was more affected
than he dared to show, “I will stay till I can find an opportunity of
going home; and oh! Mea, do not suppose that I think the worse of
you. I honour you, Mea; next to my wife and mother, you are the
dearest to me of any in the world. While I live, I will remain your
friend. See, this is the token of it;” and leaning forward, he searched
for her hand and found it, then lifted it to his forehead and touched
it with his lips. Next instant he heard the rustle of her robes as she
left the room.
Thus did Rupert keep the oath that he had sworn to his mother
years before and come out safely from the fires of a very fierce
dilemma, and thus did a star arise upon the twilight of Mea’s soul—a
far, cold star, that yet was destined to lead her on to wondrous
heights, whence the way of the flesh seemed very distant, and that
of the spirit very near.
CHAPTER XV.
RUPERT MAKES OBEISANCE

For three days after the passionate scene that has been described,
Rupert saw no more of Mea. When he asked about her, not without
anxiety, Bakhita, who had taken her place as his nurse, informed him
that she had gone to a distant part of the oasis to inquire about her
crops, and to settle a dispute between two families as to some land.
For some reasons he wished that she would come back again, since
during those days Bakhita was very short with him; indeed, the word
“harsh” would scarcely have exaggerated her attitude.
“I know you are angry with me,” he said at last, “but you who are
wise and acquainted with our law, will understand.”
“I understand that you are a fool, Rupert Bey, like many of you
white men who think yourselves so good and clever. I wish you had
never come to Tama, for now my niece will go unmarried, and the
ancient race must die.”
“It is not my fault,” he answered humbly, “it is yours, Bakhita, who
would accompany me somewhat against my will, and thus have
brought ruin upon everybody.”
“Nay,” she answered crossly, “it was yours, who spied upon us in
the sanctuary at Abu-Simbel, and thus caused the libation to be
poured amiss. From that moment Tama became your slave, and the
god grew jealous and brought evil upon us all, especially upon you,
Rupert Bey.”
He laughed a little and said:
“You don’t really believe all that, do you, Bakhita? Those old gods
have been dead for many an age.”
“I am not sure what I believe,” she answered, “but departed faiths
still haunt the blood of those whose fathers held them, and the
ancient gods live on in other forms. To-day none worship ours, save
I, for Mea turned from him to you, and the people have forgotten
long ago. Well, I was sure that ill-luck would come, and so it has.
Still, I do not blame you, Rupert Bey, who are brave and honest, and
have dealt well by her whom you might have betrayed and left. Nor,”
she added, with a curious burst of conviction, “nor am I sure that
things will go so ill after all. You said to us one day that the spirit is
greater than the flesh, and that those who follow the spirit win at
last. Though you seem such a fool, perhaps you are right, Rupert
Bey. I think so at times, for, look you, I also have put aside the flesh
and followed the spirit all my life, and learned much, for do they not
call me wise and foresighted? Only,” she added reflectively, “perhaps
I have followed the false spirit, and you follow the true. Perhaps the
old gods are really dead at last, and new ones rule the world. But if
so, in the Soudan they are devils. Meanwhile, Rupert Bey, deal
gently with the flower whose stalk you have broken in your clumsy
hand, lest the air should soon lack its fragrance.”
On the third day Mea reappeared, looking rather pale and red-
eyed, but outwardly, at any rate, in a cheerful mood. Not one word
did she say then or afterwards to Rupert about their great argument
as to the moralities of the East and West. For whether she had been
visiting her crops, or perchance lying weeping on her bed, at least it
would seem that she had conquered herself, and was determined to
adapt her life to the conditions upon which they had tacitly agreed.
By now it was certain that his sight would be restored to Rupert, and
this joyful fact worked wonders for them both. For instance,
mounted on a quiet mule, which a servant led, whilst others ran
before, behind, and around him, and Mea herself rode at his side,
she conducted him about the oasis that was her heritage.
It was a large place, thirty miles or more in length by perhaps
fifteen in breadth, which would have supported a great population,
as once it must have done, for its soil, washed down from the
mountain-sides, was of a marvellous fertility and very well-watered.
The local methods of cultivation, however, were primitive, and as the
trade with outside people was very small, its inhabitants had no
incentive to grow more than they could consume.
“If I had the management of this oasis for ten years,” said Rupert
to Mea, after he had inspected most of it, “I would make you the
richest woman in the Soudan.”
“Then stay and make me so,” she answered, smiling. But he felt
that it was not the riches which she desired.
What interested him even more were the ruins of the great temple
which had evidently been devoted to the worship of Ra—that is, the
Sun as the robe and symbol of Divinity. It was of a late period,
Ptolemaic indeed, and not of the best workmanship, and there were
various passages in the inscriptions which seemed to suggest that it
was founded by some Egyptian prince of the thirtieth dynasty, who
fled hither after the reconquest of Egypt by the Persians. It
appeared that his descendants for many generations kept a kind of
royal state in this far-off oasis where nobody thought it worth while
to attack them. Indeed, on the sarcophagus of one of them who
died as late as the reign of Theodosius four centuries after Christ,
was an inscription pompously describing the deceased as “Beloved
of Ra, King of Tama and of Upper and Lower Egypt.”
The most impressive part of this temple, indeed, was the
mausoleum of the rulers of the oasis who had called themselves
kings. Probably because they could not afford to make for
themselves great separate tombs after the fashion of those in the
Valley of Kings at Thebes, they hollowed in the rock beneath the
temple a vast crypt, from which opened outside chambers like to
those of the Serapeum, the burying-place of the sacred bulls of
Memphis. At the head of this crypt stood a huge and solemn statue
of Osiris in his mummy wrappings, but wearing the crown and
feathers of Amen-Ra, and at its entrance was a great underground
pool or cistern of water, across which the bodies of the dead were
ferried, in imitation, doubtless, of the last journey across the Nile.
Certain of the side-chambers were bricked up, but others were
either never closed, or had been opened, and there in their
sarcophagi lay the dead.
In a past age some of the granite coverings and coffin-lids had
been removed, but the mummies remained inviolate, even their
golden ornaments were not disturbed. Those of one young queen,
or rather chieftainess, who had died a few years before the birth of
Christ were indeed of remarkable beauty and great value,
comprising a crown of gold filagree and enamelled flowers of
marvellous workmanship, inlaid pectoral and bracelets and a sceptre
of gold surmounted by a crystal symbol of the sun. Mea took them
from the body and arrayed herself in them and stood before Rupert
a queen of Egypt, as once he had seen her stand in the sanctuary at
Abu-Simbel. Very wonderful she looked thus with the lamp-light
shining upon her in that awesome, silent place.
“What are you doing?” he asked, for notwithstanding the bizarre
beauty of her decorations, it jarred upon him to see her ornamented
with these insignia of death.
“I try them on, Rupert Bey,” she answered. “As we cannot make
such things now I will borrow them from the lady, my long-ago
grandmother, to be buried in. Come here; I show you my tomb.”
Then she led the way past certain built-up chambers in which, she
informed him, her immediate predecessors lay uncoffined, to a
recess where was a magnificent sarcophagus of alabaster. It was
graved about with the usual texts from the Book of the Dead, but
had several peculiarities. Thus in its great interior were places for
two bodies with a little ridge of alabaster left to separate them. It
was quite empty, the massive lid which stood by its side never
having been put on. Also the spaces for the name, or names, of its
occupants were left blank, showing that those for whom it was
prepared rested elsewhere.
“Where are they?” asked Rupert, as with the help of Mea and his
crutch he scrambled down from the pediment of the tomb.
“Don’t know,” she answered, “perhaps die somewhere else, or
killed by enemy; perhaps quarrel, and no wish to be buried together.
I take their house when my time comes; just fit me.”
“Then you mean your husband to lie there too?” blurted out
Rupert, without thinking.
Holding the lamp in her hand she turned and looked at him with
steady eyes.
“Understand, Rupert Bey,” she said, “I have no husband, never—
never. All day I work alone, when night come I sleep alone. Then my
people build up this place—all, all, for I the last and nobody ever
come in here any more. Yes, build it up with stone of the temple and
make it solid like the mountain, for I wish to sleep long and quiet.”
Such were the oasis Tama and its antiquities. Of its people there is
little to say, save that they were grave in demeanour, rather light in
colour and handsome in appearance, especially the women, looking
much as the last descendants of an ancient and high-bred race
might be expected to look. The men, as we have seen, were brave
enough in war, suspicious and exclusive also, but indolent at home,
doing no more work than was necessary, and for the most part
lacking the energy to trade. Their customs as regards marriage and
other matters were those common to Nubia and the Soudan, but
although they talked of Allah they were not Mahommedans, and if
they worshipped anything, it was God as symbolised by the sun.
Indeed this was all that remained of their ancient faith, with the
exception of certain feasts and days of mourning, whereof they had
long forgotten the origin. Only a few of the old women before a
marriage or a burial, or any other event of importance, would
occasionally creep down to the vault and pour a libation to the
statue of Osiris that wore the crown and feathers of Amen-Ra, as, in
an hour of danger, Bakhita had made Mea do at Abu-Simbel.
This survival was interesting, but Rupert was never able to
discover whether it had descended from the ancient days, or
whether they had learnt the practice from the sculptures on the
temple and the paintings in the vault, which showed the departed
rulers and their wives and attendants pouring such libations before
this very statue. At least, of the old religion nothing else remained,
nor could anyone in Tama read the hieroglyphics. It was her desire
to acquire this and other learning, and to become acquainted with
those men and the wonderful outside world, whereof rumours had
reached her in her isolated solitude, that had caused Mea to disguise
herself and spend two years at the school at Luxor. Here, although,
as she found to her disappointment, they did not teach
hieroglyphics, she had accumulated a considerable quantity of
miscellaneous knowledge of men and things, including a superficial
acquaintance with the English tongue, in which she loved to talk.
Now she insisted upon continuing her education under Rupert’s
guidance, and as they had only one book, the instruction took the
form of lectures upon history, literature, art, and everything else
under the sun with which he had the slightest acquaintance. It was
a strange sight to see them in one of the big rooms of her house,
Mea seated at a little table and Rupert limping to and fro upon his
crutch, and holding forth on all things, human and Divine, such as
Egyptology—of which he really knew something; modern political
history, especially that of Africa, and religion. Indeed, the last played
a large part in their studies, for as it happened among the few
belongings that were saved from the saddle-bags of his camel was
Rupert’s Bible, that same skin-bound volume which had excited
Edith’s wonder and interest. Therefore it was out of this Bible that he
made her read to him, with the result that she learned from it more
than the letter. As he intended that she should, soon she began to
appreciate the spirit also, and in its light to understand much that
had puzzled her in Rupert’s conduct towards herself and others. But
the knowledge did not teach her to love him less, only perhaps she
honoured him the more.
So the weeks passed on, and strange as were the conditions of his
life, not altogether unhappily for Rupert. As yet it was impossible for
him to leave the oasis for the reasons that have been given, and
sometimes with a sudden sense of shame, he awoke to the fact that
this detention was no longer the agony to him that it had been at
first; that now indeed he could endure it with patience. Of course
the truth was that we are all of us very much the creatures of our
immediate surroundings, and that the atmosphere of this peaceful
desert home had crept into his being, bringing with it rest, if not
content. He had suffered so much in mind and body, and now he
was not called upon to suffer. So skilful was she in her dealings with
him, so well did she veil her heart in its wrappings of courtesy and
friendship, that he ceased even, or at any rate to a great extent, to
be anxious about Mea.
He tried to forget that passionate scene, and when he did think of
it his modesty prompted him to believe that it really meant nothing.
Eastern women were, he knew, very impulsive, also very changeful.
Probably what had moved her, although at the time she did not
know it, was not devotion to a shattered hulk of a man like himself,
but as she had said at the beginning, pity for his sad state of which
indirectly she was the cause.
Al least he hoped that it was so, and what we hope earnestly in
time we may come to believe. So that trouble was smoothed away,
or at any rate remained in abeyance.
For the rest those palms and mountain-tops, those bubbling
waters and green fields, that solemn, ruined temple and those
towering pylons, were better than the parks and streets of London,
or that hateful habitation in Grosvenor Square where Lord Devene
leant against his haunted marble mantel-piece and mocked. Indeed,
had it not been for Edith and his mother, Rupert would, he felt, be
content, now that his career had gone, to renounce the world and
live in Tama all his days. But these two—the wife who must think
herself a widow, and the mother who believed herself sonless, he
longed ceaselessly to see again. For their sakes, day by day he
watched for an opportunity of escape.
At length it came.
“Rupert Bey,” said Mea quietly to him one morning in Arabic as
they sat down to their usual lesson, “I have good news for you. By
this time to-morrow you may be gone from here,” and whilst
pretending to look down at the parchment upon which she was
writing with a reed pen, as her forefathers might have done twenty
centuries before—for paper was scarce with them—she watched his
face from beneath her long lashes.
The intelligence stunned him a little, preventing—perhaps
fortunately—any outbreak of exuberant joy. Indeed, he only
answered in the words of the Arabic proverb:
“After calm, storm; after peace, war,” and the reply seemed to
satisfy Mea, although she knew that this proverb had an end to it
“after death, paradise—or hell.”
“How, Mea?” he asked presently.
“A big caravan, too strong to be attacked, is going to cross the
Nile above Wady-Halfa and pass through the Nubian desert to the
shores of the Red Sea beyond the country that is held by Osman
Digna. Its chief, who is known to our people, and a true man, makes
the pilgrimage to Mecca. I have sent messengers to him. He is
willing that you should accompany him, only you must not say who
you are, and if they meet any white men you must promise not to
talk to them. Otherwise, you may bring him into trouble for the
befriending of a Christian.”
“I will promise that,” answered Rupert.
“Good! Then you leave here to-morrow morning at the dawn.
Now, let us go on with the lesson; it is my last.”
That lesson proved a very desultory performance; indeed, it
consisted chiefly of a compilation by Rupert of lists of books, which
he instructed Mea she was to send to Egypt to buy, as soon as there
was an opportunity, in order that she might continue her education
by herself. But Mea seemed to have lost all interest in the future
improvement of her mind.
What was the good of learning, she asked, if there was nobody to
talk to of what she had learned? Bakhita did not care for these
things, and the others had never heard of them.
Still she took the lists and said she would send for the books when
she could, that was, after the country grew quiet.
The rest of that miserable day went by somehow. There were
meals to eat as usual; also Rupert’s dromedary had to be got up,
and a store of food made ready for his journey. Mea wanted him to
take money, of which she had a certain amount hidden away—
several thousand pounds indeed—the products of her share of sales
of horses and corn which the tribe occasionally effected with
travelling merchants, who bought from them cheap and sold to the
Egyptian Government, or others, dear. But this he would not touch,
nor did he need to do so, for in his clothes when he was captured
were sewn about a hundred pounds, some in gold and some in
bank-notes, which he thought would be sufficient to take him to
England.

It was night. All was prepared. Rupert had said his farewells to the
emirs and chief men, who seemed very sorry that he was going.
Mea had vanished somewhere, and he did not know whether he
would see her again before he started at the dawn. The moon shone
brightly, and accompanied by the native dog that had led him when
he was blind, and having become attached to him, scenting
separation with the strange instinct of its race, refused to leave his
side that day, Rupert took his crutch and walked through the pylon
of the temple, partly in the hope that he might meet Mea, and partly
to see it once more at the time of full moon, when its ruin looked
most beautiful.
Through the hypostyle hall he went where owls flitted among the
great columns, till he came to the entrance of the vast crypt, a broad
rock-slope, down which in old days the sarcophagi were dragged.
Here he stopped, seating himself upon the head of a fallen statue,
and fell into a reverie, from which he was roused by the fidgeting
and low growlings of the dog, that ran down the slope and returned
again as though he wished to call his attention to something below.
At length his curiosity was excited, and led by the dog, Rupert
descended the long slope at the foot of which lay the underground
pool of water. Before he reached its end he saw a light, and limping
on quietly, perceived by its rays Bakhita and Mea, the former
bending over the pool, and the latter wrapped in a dark cloak,
seated native fashion at its edge. Guessing that the old gipsy was
celebrating another of her ancient ceremonies, he motioned the dog
to heel, stood still and watched.
Presently he saw her thrust out from the side of the pool a boat
about as large as that which boys sail upon the waters of the
London parks. It was built upon the model of the ancient Egyptian
funerary barges with a half deck forward, upon which lay something
that looked like a little mummy. Also, it had a single sail set. Bakhita
gave it a strong push, so that it floated out into the middle of the
pool, which was of the size of a large pond where, the momentum
being exhausted, it lay idly. Now the old woman stretched out a
wand she held and uttered a kind of invocation, which, so far as he
could hear and understand it, ran:
“Boat, boat, thou that bearest what was his, do my bidding. Sail
north, sail south, sail east, sail west, sail where his feet shall turn,
and where his feet shall bide, there stay. Boat, boat, let his Double
set thy sail. Boat, boat, let his Spirit breathe into thy sail. Boat, boat,
in the name of Ra, lord of life, in the name of Osiris, lord of death, I
bid thee bring that which was his, to north, south, east, or west,
where he shall bide at last. Boat, boat, obey.” *
* The Double and the Spirit here mentioned were doubtless those
constituent parts of the human entity which were known
respectively to the old Egyptians as the Ka (the Double), and the
Khu (the Soul itself). Of these, some traditional knowledge might
very well have descended to Bakhita.

She ceased and watched a little lamp which burnt upon the prow
of the boat, in front of the object that looked like a toy mummy. Mea
also rose and watched, while out of the darkness Rupert and the
dog watched too. For a little while the boat remained still, then one
of the numerous draughts that blew about these caverns seemed to
catch its sail, and slowly it drew away across the water.
“It goes west,” whispered Mea.
“Aye,” answered Bakhita, “west as he does. But will it bide in the
west?”
“I pray not,” answered Mea, “since ever from of old the west has
been the land of death, and therefore to the west of these waters lie
the sepulchres, and where the sun sets beyond the west bank of the
Nile, there for thousands of years our people laid their dead. Nay,
boat, tarry not in the west where Osiris rules, the cold and sorrowful
west. Return, return to the House of Ra, and in his light abide.”
Thus she murmured on, like one who makes a song to herself in
the Eastern fashion, all the while intently watching the little lamp
that showed the position of the boat. Having reached the western
edge of the pool, it seemed inclined to remain there, whereon Mea,
turning to Bakhita, began to scold her, asking her why she had
brought her there to see this childish play, and whether she thought
that she, Mea, who had been educated at Luxor and received many
lessons from the Bey, believed in her silly magic, or that a toy boat,
even though it did carry a man’s foot made up like a mummy, could
possibly tell whither he would wander.
“If the boat sails right, then you will believe; if it sails wrong, then
you will not believe. That I expected, and it is best,” answered
Bakhita drily, and at that moment something happened to the little
lamp that stood before the mummy foot, for suddenly it went out.
Now Mea grew positively angry, and spoke sharp words to Bakhita
as to her methods of divination and the benighted and primitive
condition of her intelligence in general.
“Were I to accept the augury of your boat,” she said, “I must be
sure not only that he will stay in the west, but that he will die there,
for look, the light is out.”
“Other things die beside men’s bodies,” answered Bakhita, in her
brief fashion; “their hopes, or beliefs, or perhaps their good luck—
who can say?”
As she spoke, suddenly out from the darkness of the pool into the
ring of light cast by the lamp which Bakhita bore, that fairy boat
came gliding. The gust of wind blowing down the western
sepulchres beyond the pool, which extinguished its lamp, had also
caught its sail and brought it back, half filled with water shipped in
turning; brought it back swiftly, but sailing straight to where Mea
knelt upon the edge of the pool. She saw it, and with a little cry of
joy, bent herself over the water, and stretching out her rounded
arms, caught the boat just before it sank, and hugged it to her
breast.
“Put the thing down,” said Bakhita. “You don’t believe in it, and it
is wet and will spoil your robe. Nay, the Bey’s foot is mine, not yours.
I brought it from the Wells.”
Then they began to quarrel over this poor mummied relic of which
Rupert thought that he had seen the last many a day before, while
he took an opportunity to beat his retreat. Bakhita and her ancient
spells were, as usual, interesting, though when they involved a lost
fragment of himself they became somewhat gruesome. But in such
things he had no belief whatsoever; they only attracted him as
historical, or rather as spiritual survivals. What moved him about the
matter was Mea’s part in it, revealing, as it did, that her interest in
his future had in no way abated. Indeed, he felt that it would be
long before he was able to forget the touching sight of this wayward
and beautiful girl, this desert-bred daughter of kings, snatching the
sinking boat and its grizzly burden from the water and pressing them
to her breast as though they were a living child. Meanwhile, the
accident that he had seen it did not make this farewell less difficult.
When at length he reached the house—for amongst the fallen
stones of the temple his progress with a crutch was slow—Rupert sat
down upon its steps, feeling sure that Mea would wish to see him,
and that it would be well to get that parting over. Presently the
mongrel at his side began to bark, and next minute he saw her
walking slowly up the path towards him, her cloak open and the
breast of her robe still wet where she had pressed the dripping boat
against it. He struggled from the step to meet her.
“Sit, Rupert Bey,” she said; “sit. Why trouble you to rise for me?”
“I cannot sit while you stand,” he answered.
“Then I sit also, on the other side of the dog. He look like the god
on the wall, does he not, what you call him—Anubis, brother of
Osiris? No, don’t growl at me, Anubis; I no hurt your master, you
nasty little god of the dead.”
“Where have you been, and why is your dress wet?” asked Rupert.
“Ask Anubis here, he wise, knows as much as his master. I been to
the burying-place and lean over holy water to look if I grow more
ugly than usual.”
“Stuff!” answered Rupert.
“You no believe me? Well, then, perhaps I thirsty and drink water.
Much weep make me thirsty. No believe still? Then perhaps I look in
water and see pictures there.”
“What pictures can you see in that dark place?”
“Oh, plenty, dark no matter. See things inside, like you when you
blind. I tell you what I see; I see you come back here, and so I
weep no more. I—I—happy. Make that dog go the other side, he
want to bite me now, he jealous because you look at me, not him.”
Accordingly the protesting Anubis was rearranged, and continued
his snarlings and grumblings from a safer distance.
“Some more of old Bakhita’s nonsense, I suppose,” said Rupert. “I
thought that you had given up believing in her myths and omens.”
“What mean myths and omens? No matter; Bakhita old fool, gods
old stones, believe in none of them. You say it, so all right. Believe in
you, and me—inside, what my heart tell me. My heart tell me you
come back. That why I happy.”
“Then I am afraid, Mea, that your heart knows more than I do.”
“Yes,” she answered, “think more; feel more, so know more. That
all right; what do you expect?” Then suddenly dropping her jerky
and peculiar English, Mea addressed him in her solemn and native
Arabic. “Hark you, Rupert, guest of my home, guest of my heart,
preserver of my body, who shed your blood for me. You think me
foolish, one who tries to warm her hands at the fires of the marsh,
one who plucks flowers that fade, and believes them immortal stars
fallen to deck her breast and hair. Yet she finds warmth in the marsh
fire, and in the dead flower’s heart a star. I believe that you will
come back, why or how it matters not, but to make sure you shall
swear an oath to me, you shall swear it by the name of your Jesus,
for then it will not be broke.”
“What oath?” asked Rupert anxiously.
“This: Sometimes lamps go out, and where we thought light was
there is great blackness. Sometimes hopes fail, and death stands
where life should have been. This may chance to you, Rupert Bey,
yonder in the cold, western land of the setting sun.”
“Do you mean that I shall find my wife dead?” he asked, with a
quiver in his voice. “Is that the picture you saw in your pool?”
“Nay; I saw it not; I do not know. I think she lives and is well. But
there are other sorts of death. Faith can die, hope can die, love can
die. I tell you I know not, I know nothing; I have no magic; I believe
in no divination. I only believe in what my heart tells me, and
perchance it tells me wrong. Still I ask you to swear this. If things
should so befall that there is nothing more to keep you in the West,
if you should need to find new faith, new hope, new love, then that
you will come back to Tama and to me. Swear it now by the name of
your God, Jesus; so I may be sure that you will keep the oath.”
“I do not swear by that name,” he answered. “Moreover why
should I swear at all?”
“For my sake, Rupert Bey, you will. Hear me and decide. I tell you
that if you do not come back, then I die. I do not ask to be your
wife, that does not matter to me, but I ask to see you day by day. If
I do not see you, then I die.”
“But, Mea,” he said, “it may be impossible. You know why.”
“If it is impossible, so be it, I die. Then it is better that I die.
Perhaps I kill myself, I do not know, at any rate I go away. I ask not
that you should swear to come, if it should make you break your
oath to others, only if there are no more oaths to keep. Now choose,
Rupert Bey. Give me life or give me death, as you desire. Make your
decree. I shall not be angry. Declare your will that your servant may
obey,” and she rose and stood before him with bent head and hands
humbly crossed upon her breast.
He looked at her. There could be no doubt she was in earnest.
Mea meant what she said, and she said that if he did not gratify this
strange wish of hers, and refused to give her any hope of his return,
she would die, or at least so he understood her; and was certain
that if she had the hope, she would not die and bring her blood
upon his head. Rupert looked at her again, standing there in the
moonlight like some perfect statue of humility, and his spirit melted
within him, a blush of shame spread itself over his scarred and
rugged features, shame that this loyal-hearted and most honoured
woman should thus lay her soul naked before him, saying that it
must starve if he would not feed it with the crumb of comfort that it
desired. Then he hesitated no longer.
“Mea,” he said, in the kind and pleasant voice that was perhaps
his greatest charm—“Mea, my law says: ‘Swear not at all’; I read it
to you the other day. Now, Mea, will my word do instead?”
“My lord’s word is as other men’s oaths,” she answered, lifting her
humble eyes a little.
Then he bent forward, resting on his knee, not as an act of
adoration, but because it was difficult to him to rise without
assistance, and stretching out his hand, took her crossed hands from
her breast, and bowing himself, pressed them against his forehead,
thus—as she, an Eastern, knew well—prostrating himself before her,
making the ancient obeisance that a man can only make with
honour to his liege sovereign, or to one who has conquered him.
“My lady Tama,” he went on, “after one other my life is yours, for
you gave it back to me, and after her and my mother there lives no
woman whom I honour half so much as you, my lady and my friend.
Therefore, Mea, since you wish it, and think that it would make you
happier, should I perchance be left alone—which God forbid!—I
promise you that I will come to you and spend my life with you until
you weary of me—not as a husband, which you say you do not
desire, which also might be impossible, but as a brother and a
friend. Is that what you wish me to say?” and he loosed her hand,
bowed to her once more in the Eastern fashion, with his own
outstretched, so that his fingers just touched her feet, and raised
himself to the step again.
“Oh!” she answered, in deep and thrilling tones, “all, all! More, by
far, than I had hoped. Now I will not die; I will live! Yes, I will keep
my life like a jewel beyond price, because I shall know, even if you
do not come, that you may come some time, and that if you never
come, yet you would have come if you could—that the marsh-light is
true fire, and that the flower will one day be a star. For soon or late
we shall meet again, Rupert Bey! Only, you should not have
prostrated yourself to me, who am all unworthy. Well, I will work, I
will learn, I will become worthy. A gift, my lord! Leave me that holy
book of yours, that I may study it and believe what you believe.”
He limped into the house and brought back the tattered old Bible
bound in buckskin.
“You couldn’t have asked for anything that I value more, Mea,” he
said, “for I have had that book since I was a child, and for that
reason I am very glad to give it to you. Only read it for its own sake
—not for mine—and believe for Truth’s sake, not because it would
please me.”
“I hear and I obey,” she said, as she took the book and thrust it
into the bosom of her loose robe.
Then for a moment they stood facing each other in silence, till at
length, perhaps because she was unable to speak, she lifted her
hands, held them over him as though in blessing, then turned and
glided away into the shadows of the night.
He did not see her any more.
CHAPTER XVI.
MEANWHILE

It was the last day of the old year when, had there been anyone
to take interest in her proceedings among so many finer vessels
going to or returning from their business on the great waters, a
black and dirty tramp steamer, whose trade it was to carry coals to
the East, might have been seen creeping up the Thames with the
tide. A light but greasy fog hung over the face of the river, making
navigation difficult, and blurring the outlines of the buildings on its
bank, and through it the sound of the church bells—for it was
Sunday—floated heavily, as though their clappers had been muffled
in honour of the decease of one of the great ones of the earth.
In his cabin—for after the suns of the Soudan the winter wind was
too cold to face—sat Rupert Ullershaw, dressed in a mustard-
coloured suit of reach-me-downs, somewhat too small for him, and
of a peculiarly hideous cut and pattern, which he had purchased
from a sailor. Physically he was in good health, but his mental
condition may best be described as one of nervous irritability born of
weeks and months of suspense. What news awaited him on his
arrival home, he wondered, and how would he, a discredited and
mutilated cripple, be received?
That he was discredited he knew already, for he had found an old
paper on board the ship, in which, on looking at it, his own name
had leapt to his eye. Someone had asked a question in Parliament
concerning him and his mission—why it had been sent, what were
the facts of the rumours of its annihilation, whether it was true that
this disaster had been brought about through the envoy, Lieutenant-
Colonel Ullershaw, C.B., having mixed himself up in tribal quarrels
over a native woman, and what was the pecuniary loss involved to
the country? Then followed the answer of the Secretary of State, the
man who had pressed him to go on the grounds of duty and
patriotism. It stated that Colonel Ullershaw had been despatched to
carry out certain confidential negotiations with a number of sheiks
on the borders of the Soudan. That according to the report received
from the Egyptian authorities, a native sergeant named Abdullah,
who accompanied him, had arrived at Cairo and informed them that
all the members of the mission, who were disguised as merchants,
had been attacked by a petty chief called Ibrahim and destroyed,
Abdullah alone escaping. That it appeared from this survivor’s
evidence that the attack was not political, but had its origin in
Colonel Ullershaw having unfortunately tried to protect two native
women who were travelling with him, one of whom, stated to be a
young person of some rank, was claimed by the sheik Ibrahim as a
wife. That the loss to the country, or rather to the Egyptian
Government, amounted to about two thousand pounds, of which
one thousand was in cash.
Arising out of this were other questions, evidently framed to annoy
the Government upon a small matter, such as: Was it true that
Colonel Ullershaw had been chosen over the heads of more suitable
persons, because his great family influence had been brought to
bear upon the War Office? To this the answer was that the deceased
officer’s record had been very distinguished, and he was chosen
because of his diplomatic experience, his knowledge of Arabic and
personal acquaintance with the sheiks, with whom it was necessary
to communicate: That, as the House would be aware, his family
influence as represented in that House, and, he might add, in
another place, was not likely to unduly influence Her Majesty’s
present advisers, of whom the gentlemen concerned were strong
and able opponents. (A laugh.)
The thirst for information not being yet appeased, an Irish
member asked whether it was true that a punitive expedition had
been sent to kill the chief whose wife Colonel Ullershaw had stolen—
(laughter); and whether the Government now regretted their choice
of Colonel Ullershaw as the head of this mission.
Answer: That such an expedition had been sent, but that it
appeared that Colonel Ullershaw and his party had made a very
gallant fight before they were overwhelmed, and that either he, or,
as was stated by some nomads, the lady, whom he had befriended,
with the help of her tribesmen had already killed the sheik Ibrahim
and most of his men, whose corpses had been seen by the nomads
hanging to some trees: That the Government admitted that their
choice had not been justified by events, but that he, the Secretary of
State, deprecated the casting of slurs upon very insufficient
information upon the memory of a brave and devoted servant of his
country—(hear, hear!)—whose mistakes, whatever they might have
been, seemed to have sprung from the exaggerated chivalry of his
nature. (A laugh.)
Another Irish member: Was it true that Colonel Ullershaw had
been married on the day he left England to enter upon this mission?
The Speaker: “Order, order. This House has nothing to do with the
domestic concerns of the late Colonel Ullershaw.”
The Honourable member apologised for his question, remarking
that his excuse for it must be that the country, or Egypt, had to pay
in lives and money for the domestic entanglements of Colonel
Ullershaw, in which he became involved among the desert sands.
(Much laughter and cries of order.) He wished to ask the Right
Honourable gentleman whether he was sure that the gallant Colonel
—(more laughter)—was really dead?
The Secretary of War: “I fear there is no doubt upon that point.”
The subject then dropped.

Turning over the paper in a dazed fashion—for the cruelty and


injustice of these questions and the insinuations so lightly made for
party purposes cut him to the heart—Rupert had come upon a sub-
leader which discussed the matter in a tone of solemn ignorance.
Being an Opposition organ, the leader-writer of the journal seemed
to assume that the facts were correctly stated, and that the
unfortunate officer concerned brought about the failure of the
mission and lost his own life by a course of action so foolish as to be
discreditable, in which, as it stated, “the ever-present hand of female
influence can unfortunately be traced.” It added that deeply as the
death of a man who had served his country well and gallantly in the
past was to be regretted, perhaps for Colonel Ullershaw it was the
best thing that could have happened, since it seemed probable that
in any event his career would have been at an end.
After reading this report and comment, Rupert’s common-sense
and knowledge of official ways assured him that, however unjustly,
he was in all probability a ruined man. On the charges about the
lady in the desert he might, it is true, be able to put a different
complexion, but it would be impossible for him to deny that the
unfortunate presence of Bakhita and Mea had been the immediate
cause of his disaster, or indeed that he had been spending several
months as their guest. Beyond these details, however, lay the
crushing fact that he who had been expected to succeed, had utterly
and completely failed, and by failing, exposed those who employed
him to sharp criticism and unpleasant insinuations. Lastly, the
circumstance that he was now a hopeless cripple would of course be
taken advantage of to dispense with his further services.
So convinced was he of the desperate nature of his plight that he
had not even attempted to offer any explanation to the Egyptian
Government, as he saw that his only chance lay in influencing those
at headquarters and persuading them to order a further local inquiry
in Egypt. Besides, he was anxious to get home, and knew that if he
had opened up the matter in Cairo, he would probably be detained
for months, and very possibly be put under arrest pending
investigations.
Rupert’s journey across the desert had been long, but unmarked
by any incident or danger, for they passed round Osman Digna’s
hordes and through country that was practically depopulated,
meeting but few natives and no white men. So far as Rupert was
concerned it was comfortable enough; since after the Arab caravan
had started from the neighbourhood of Tama, he found to his
surprise that Mea had provided him with a guard of twenty of her
best men, who brought with them a tent and ample provisions. He
ordered them to return, but they refused, saying that they had been
commanded by their lady to travel with him to the Red Sea as an
escort to the dog Anubis that had insisted upon following him from
the town, which dog they were charged to bring back safely when
he parted with it at the water. Then understanding what Mea meant
by this Eastern subterfuge about the dog and fearing to hurt her
feelings, should he insist, he suffered the men to come with him,
with the good result that he found himself regarded as a great
personage in the caravan.
At length they reached a little port on the Red Sea whence the
pilgrims to Mecca proposed to proceed by dhow to Suez, and, as it
chanced, found there this English collier that was taking in fresh
water. On her Rupert embarked with the pilgrims, passing himself off
as one of them, for the captain of the collier was glad to earn a little
by taking passengers. The last that he saw of the desert was his
Tama escort, who, having kissed his hand and made their dignified
farewells, were turning their camels’ heads homewards, the poor cur,
Anubis, notwithstanding his howls and struggles, being secured in a
basket which was fastened to the side of one of the said camels. No;
that was not quite the last, for as the boat rowed out to the steamer
which lay at a little distance, it passed a jutting spit of land that gave
shelter to the shallow harbour. Of a sudden from this promontory
there floated up a sound of wild, sad music, a music of pipes and
drums. Rupert recognised it at once; it was the same that he had
heard when he rode with Bakhita and Mea from Abu-Simbel, the
music of the Wandering Players, those marvellous men who refused
baksheesh.
As the morning mist lifted he saw them well, on the sandy beach
within twenty yards of the boat. There were the five muffled figures
squatted on the ground, three blowing at their pipes and two seated
opposite to them beating drums to time. As before they seemed to
take not the slightest notice of the passers-by, except that their
music grew wilder and more shrill. An English sailor in the boat
shouted to them to stop that funeral march and play something
funny, but they never lifted their heads, whereon, remarking that
theirs was a queer way to earn a living, caterwauling to the birds
and fishes, the sailor turned his attention to the tiller and thought no
more about them. But even on the ship their melancholy music could
be heard floating across the water, although the players themselves
were lost in the haze. Indeed, it was while Rupert read the report of
what had passed in the House of Commons in the old paper which
he found in the deck cabin, that its last wailing burst reached him
and slowly faded into silence.
At Suez the pilgrims left the steamer, but as she suited him very
well, and the fare demanded did not make any big hole in his £100,
he revealed himself as an Englishman and booked a passage on to
London. Now London was in sight, yonder it lay beneath that dark
mass of cloud, and—what would he find there? He had not
telegraphed from Suez or Port Said.
It was, he felt, impossible to explain matters in a cable, and what
could be the use, especially as then everything would get into the
Press? They thought him dead, or so he gathered from that paper,
therefore no one would incur extra suspense or sorrow by waiting
for a few more days, to find that he, or some of him, was still alive.
He longed to see his wife with a great longing; by day and by night
he thought of her, dreaming of the love and sympathy with which
she would greet him.
Yet at times doubts did cross his mind, for Edith loved success,
and he was now an utter failure, whose misfortunes must involve
her also. Could he be the same Rupert Ullershaw who had left
Charing Cross railway station nine months before, prosperous,
distinguished, chosen for an important mission, with a great career
before him? Undoubtedly he was, but all these things had left him;
like his body his future was utterly marred, and his present seemed
almost shameful. Nothing remained to him now except his wife’s
love.
He comforted himself. She would not withhold that who had taken
him for better or worse; indeed it was the nature of women to show
unsuspected qualities when trouble overtook those who were dear to
them. No; upon this point he need not torment himself, but there
were others.
Was he to tell Edith the dreadful secret of her birth which had
haunted him like a nightmare all these weary months? Sooner or
later he supposed that it must be done. And must he meet Lord
Devene, and if so, what was he to say when they did meet? Then
Edith would want to know the truth of this story of the lady in the
desert, which, of course, she had a right to learn in its every detail.
There was nothing in it. Mea was no more than a dear friend to him;
indeed he had thought of her but little lately, whose mind was so
preoccupied with other matters. Yet he felt that the tale of their
relationship, told exactly as it occurred, and he could repeat it in no
other way, might be open to misinterpretation, as the facts of his
escort of her and her aunt across the desert had been already.
Well, she would have to take his word for it, and even if she did
not estimate that quite as high as Mea had done, at least she knew
that he was no teller of lies. Then after these difficulties were
overcome, how was he to live? He had saved a little money, and
perhaps as a wounded man they might give him a small pension, out
of which his heavy insurance would have to be paid, if indeed it did
not absorb it all. There remained her father’s—he winced as the
word came into his mind—settlement upon Edith, but that income,
personally, he would rather starve than touch. Still his wife must be
supported in a way commensurate with her position. This outlook,
too, was so black that he abandoned its consideration and fell to
thinking of the joy of his meeting with his mother.
Here at least there were no ifs or buts. She would understand, she
would console; his misfortunes would only make him dearer to her.
For the rest, sufficient to the day was its evil—the morrow must take
care of itself. It was indeed sufficient.
Now while the old tramp lumbers up the Thames through the grey
December mist and sleet, let us turn for a few minutes to the
fortunes of some of the other personages in this history.
After her husband’s departure, Edith returned to live with Mrs.
Ullershaw, which was an inexpensive arrangement, and, as she
explained to Dick, the right kind of thing to do. Several letters
arrived from Rupert, the last written at Abu-Simbel the night before
he began his fatal journey, and some were sent in reply which he
never received. Then came the long silence, and after it the awful,
sudden catastrophe of which they learned first from a Cairo telegram
in an evening paper. Rupert was dead, and she, Edith, who had
never been a wife, was left a widow. The blow overwhelmed her. All
her card castle came tumbling about her ears. Now she could never
be the partner in a brilliant and successful career, and the husband
whose virtues she recognised, and of whom she would have been
proud, was taken from her into the darkness of a desert grave, he
who in due course should have made of her one of the richest
peeresses in England. Yes, now those gay dresses must be
exchanged for a widow’s weeds. She was furious with a Fate that
had played such a trick upon her; even her tears were more those of
anger than of sorrow, though in her fashion she mourned him truly.
Dick came to console her; he came very soon. Already he had
been at the War Office and mastered the points of Abdullah’s
garbled tale, which, as though unwillingly, he told to Edith, leaving
her to put upon it what construction she chose.
“It is nonsense,” she said angrily, for in her heart she did not
believe it at all. “Poor Rupert would never have got into any silly
mess with a savage.”
“Of course it is nonsense,” he answered, taking her cue; “but it is
not a question of morality, it’s a question of wisdom. By mixing
himself up with these women he brought about the murder of the
whole lot and the utter failure of his mission. In a way, it is as well
for him that he is gone, poor dear fellow, for he had completely
done for himself. Well, it doesn’t matter now.”
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