What Is Programming?
It’s all about writing code.
Programming is a process in which we organize data and use logic to do something with the data. The data is everything a computer can store; they can range from numbers to zombie characters in a video game. You do this by writing text into many files called source code. Source code written into text files replaces punch cards used by the computing machines half a century ago.
When data is combined with logic and then written into a single file, it’s called a class. Classes are also data and, as such, can be managed with more logic. Classes are used to create objects in the computer’s memory and can be duplicated to have a life of their own.
Classes are used to build objects. Each piece of data within the class becomes a part of that object. Different chunks of data inside of a class are called class members. Class members can also be chunks of logic called functions or methods. If that is not clear right now, don’t worry we’ll go into detail on that in Chapter 2.
Objects created from a class are called instances. In a game with a horde of zombies, each zombie is duplicated or instanced from a zombie class. Each zombie has unique values for each attribute or data element in the class. This means hit points and positions are unique for each duplicate zombie object.
Similar to families, objects can inherit properties from one another. The child, sometimes called a subclass, inherits attributes from its parent. For instance, the child created from a zombie may inherit the parent’s hunger for brains. To be useful, the child zombie can also add new objects and change the objects it inherited from its parent class. As a result, now the child zombie might have tentacles that the parent didn’t have.
Objects talk to each other through events and messages. Shooting at zombies can create an event, or in programmer terms, it “raises” an event. The bullet impact event tells the zombie class to then take necessary steps when hit by a bullet. Events command the class to take actions on its data, which is where functions come in.
Functions, also known as methods, are sections of logic that act on data. They allow your class to create additional events and talk to yet more objects. As the player presses the trigger and moves the joystick around, yet more events can be raised and messages can be sent. Events and messages allow the player to interact with your world; logic events and objects together build your game.
1.5.1 What Does C# Look Like?
A game in Unity 3D is made up of many different C# files. Each file can be referred to as a “class” and ends with the .cs file extension (which stands for C Sharp). This isn’t a class like those that are taught. It is more akin to biology in which classes are categories that define different groups of organisms.
A basic game might have several different classes, each one named according to its purpose. Each one of these classes contains the text that is your actual C# code. Just so you know, by default Windows will hide the extensions of your files. As a result, if classes appear as simply Weapon or Monster and there is no extension.cs, then you should go into the Tools menu, select Folder Options/View, and enable file extensions.
As an exercise, we’ll be building a simple game involving monsters and shooting. We’ll start with a basic skeleton with simple object classes and simple movement behaviors. From this we’ll build on some basic events when the player presses keys, moves the mouse, and shoots at targets.
1.5.2 Learning to Copy and Paste
A good artist creates, a great artist steals.
Code examples exist for you to copy and paste into your project. The content for this book and all downloadable content are no different. You’ll have to understand what the code does and fit it to your needs.
Every programmer copies code habitually. Whether it is from code written in a previous project, or code found on the internet. Programmers copy code to learn. Of course, it’s still important to know how to write new code from scratch; most programming classes in school will prefer that you do this for all your assignments. With practice, being able to recognize what to copy and where to paste is something you’ll only know from writing code to begin with.
Programming is a constant learning process. It is a language to command computers. Anytime you learn a new language, there will be plenty of words which you’ll have to look up. Add to this the fact that every programmer gets to make up new words, and you’ve got a language that always needs a dictionary.
This is one reason why programmers are usually fast typists. This is also cause for programmers to be picky about what keyboard they prefer.
In most cases, the projects in this book will be in some state where you can read text that is already in place. Most of the projects are in a more complete state where you’ll be able to run them and see an intended result.
As a programmer, I’ve gotten used to searching the internet for example code. Once I’ve discovered something that looks useful, I copy that code and paste it into a simple test case. After I’ve wrapped my head around what the code is doing, I rewrite it in a form that is more suited for my specific case.
Even if the code involves some fun trick, I’ll learn from that code. As I learn new tricks, I grow as a programmer. The only way to learn new tricks is finding the necessity to solve a new problem for which I haven’t already figured out a solution. Finding solutions to problems is a fundamental part of being a programmer.
1.5.3 Iteration
Programming is an iterative process. It’s irregular to write an entire class or function without testing it many times. The general flow is to write a part of a function, test it, add a bit more, test again, and repeat until the function accomplishes its goal. Even then, many functions often work together in sequence. Often, you’ll write a function, verify that it’s working, write another function, and verify some more. Then you’ll have them work with one another and test again, all the while you’ll be changing and re-arranging your code to fix mistakes.
Most of the time you’ll start off with a bunch of placeholders until you get around to finishing your code. As you’re adding features and finishing code, you’ll have chunks of work that aren’t finished. This is a part of game development and a part of learning as well.
1.6 Compiling: Turning Words into Computer Instruction
To test and use your code, it is time to compile. Compiling is the process of taking all of your source files and building bytecode. Unity 3D uses .NET, as it is a compiler to generate the bytecode, but Unity 3D does this automatically. Mono is an open-source compiler that runs on many different processors and operating systems.
The combination of the central processing unit (CPU) and operating system is often called a platform. Each platform requires a unique native machine code to execute or run. Building code for each platform is called a target. Unity 3D converts the bytecode into a native machine code and can target Mac, PC, Android, and iOS.
Native machine code is the set of instructions that directly talk to the CPU and operating system (think of the holes punched into a card and fed to a machine). Unity 3D is a simple way to generate the complex set of instructions for your computer to run. Code that talks directly to the hardware is referred to as “low-level” programming.
There are layers of software between you and the computer’s hardware. When writing C# for games using Unity 3D, your code is compiled by .NET. Unity 3D then takes the bytecode from .NET and compiles a target platform into a native machine code.
Both Unity 3D and .NET are the layers underneath your code and the computer’s hardware, putting you on a higher level. This is often referred to as a computer layer abstraction. That is why C# is usually considered a high-level programming language.
Programming at a lower level requires much more knowledge of memory management and a wide variety of other APIs that might include graphic cards, physics, sound, and everything else that runs a game. Writing for a layer involves an in-depth knowledge of both the layers below and above the one you’re writing for.
The computer hardware, such as CPU, graphics card, memory, and storage, live on the lowest level. Above them is the basic input/output system (BIOS) and software that starts the hardware when you press the Power button. Above that is your computer’s operating system and drivers that talk to the hardware. Finally, Unity 3D lives above the operating system and your code lives above Unity 3D.
That is why we’re going to use Unity 3D to write games and not start from scratch in raw C++. Otherwise, we’ll have to spend a few years learning about physics, rendering, and assembly language or the instruction set that your CPU uses to operate.
1.7 What We’ve Learned
We should have an idea of what C# is, and what is required of you to learn it. If you haven’t done so already, download and install the required software.
Unity 3D is free; there are no fees or royalties that need to be paid to the Unity 3D developers so long as you’re not building any commercial product. Git is also free to use and you’ll need that to follow the chapters on Git.
You’re even allowed to deploy your projects on anything Unity can target. Until you’re ready for a commercial launch, the free license is all you’ll need for learning and sharing your game with your peers on your computer.
Optionally, you may also want to setup an account on the Unity 3D site if you want to download and use any of the assets from their Asset Store. A Visual Studio Community account and a GitHub account may also come in useful later.
1.8 Leveling Up: The Path Ahead
The computer systems in place today have evolved from machines with moving parts. Telling these machines what to do involved setting knobs and switches. Today, we use text organized into statements and logic.
It is difficult to imagine a time when computers were so slow that simple calculations involved waiting for clattering switches to finish moving around to produce an answer. Today, computers complete many billions of calculations in a single second. All too often, we take for granted that computers rely on the same concepts and systems engineered nearly a century ago.
Understanding how computers work puts into context how we write software. It is possible to write software without this understanding, but it is harder to appreciate how it works.