D I G T E K

Loading...

Nullam dignissim, ante scelerisque the is euismod fermentum odio sem semper the is erat, a feugiat leo urna eget eros. Duis Aenean a imperdiet risus.

1

Introduction: What This Tutorial Is About

This book was written as an answer for anyone to pick up a modern programming language and be productive. You will be able to start a simple game in Unity 3D from scratch. By the end of this book, you will have the basic skills to eventually become a capable unity game programmer, or at least know what is involved with how to read and write some code.

You should have general computer skills before you get started. Come prepared; you’ll need a modern Windows or OSX computer capable of running modern software with an internet connection. After this book, you should be armed with the knowledge required to feel confident in learning more.

Each chapter has example code organized by chapter and section. We’ll try to make a fun project starting with the basic functions of a typical game and we’ll see how the basic game can be expanded upon and we’ll learn a bit about what’s involved with a larger project.

1.1 Why Read a Book: Why This Book May or May Not Be for You

You could go online and find videos and tutorials to learn; however, there is a disadvantage when it comes to learning things in order and in one place. Most internet video or tutorial websites may gloss over or dwell on a subject. You could skip ahead, but then what are you paying for?

Online content is often brief and doesn’t go into much depth on any given topic. It is incomplete or still a work in progress. You’ll often find yourself waiting weeks for another video or tutorial to come out.

Just so you know, you should find the act of learning exciting. If not, then you’ll have a hard time continuing through to the end of this book. To learn any new skill, a lot of patience is required.

I remember asking an expert programmer how I’d learn to program. He told me to write a compiler. At that time, it seemed rather unfair, but now I understand why he said that. It is like telling someone who wants to learn how to drive Formula 1 cars to go compete in a race. In both cases, the “learning” part was left out of the process.

It is very difficult to tell someone who wants to learn to write code where to begin. However, it all really does start with your preparedness to learn. Your motivation must extend beyond the content of this book.

You may also have some preconceived notions about what programming is. I can’t change that, but you should be willing to change your thoughts on the topic if you make discoveries contrary to your knowledge. Keep an open mind.

As a game developer, you should find the act of making a game, or simply learning how to make a game, is just as fun as playing a game. If your primary goal is only to make that game that you’ve had in your head for years, then you might have a problem. You may find this book a tedious chore if learning C# is just something in the way of making your game.

Computer artists often believe that programming is a technical subject that is incompatible with art. I find the contrary to be true. Programming is an art, much as literature and design are an art. Programming just has a lot of obscure rules that need to be understood for anything to work.

No, programming is not too hard to learn. Writing a massive multiplayer online role-playing game is quite hard. Learning how to write a simple behavior isn’t hard; like drawing, you start off with the basics drawing spheres and cubes. After plenty of practice, you’ll be able to create a real work of art. This applies to writing code, that is, you start off with basic calculations and then move on to the logic that drives a complex game.

1

1.1.1 Do I Need to Know Math?

With complex rules in mind, does programming require the knowledge of complex mathematics? Unless you are writing mathematical software, only a bit of geometry is required. Most of the examples here use only a tiny bit of math to accomplish their purpose. Mathematics and programming do overlap quite a lot in their methodology.

Math taught in schools provides a “best” solution. Programming results tend to behave a bit like a math proof, but the proof isn’t just another bit of math. Rather, the proof of your code means that your zombies chase after humans. A considerable amount of math required has been done for you by the computer. It is just up to you to know what math to use, and then plug in the right variables.

1.1.2 Programming as a Form of Expression

There is a deeper connection between words and meaning in programming. This connection is more mechanical, but flexible at the same time. In relation to a programming language, programmers like to call this “expressiveness.”

When you write words in literature, you infer most if not all of the meaning. When programming inference isn’t implied, then you’re free to define your own meanings for words and invent words freely.

One common merging of art and code appears within a video game. Anytime characters can react to each other and react to user interaction conveys a greater experience. An artist, an animator, or an environment designer should have some ability to convey a richer experience to the audience. A character artist can show a monster to his or her audience; but to really experience the creature, the monster should glare, grunt, and attack in reaction to the audience.

1.1.3 Games as a Stage with Lights

To do this, we’re going to use Unity 3D, a game engine. If you were to write a play, you would not have to build the auditorium for your performance. Building lights and generating electricity to run them would distract you from writing your play. Playwrights don’t often build lights and auditoriums from scratch, and there are plenty of venues you could use as your stage, which is where game engines come in.

Game engines such as Unity 3D, Unreal Engine, Crytek, and even Game Maker, or Godot have a lot of the basic game and math-related functions built for you. They are ready-built stages that allow you to focus on making your game. The necessary code for rendering and physics have been done for you.

1.2 Personal Information

I started in the 1990s cleaning up sprites for Atari. Back then the job qualifications were basically having Photoshop and a Wacom tablet. Oh, how times have changed!

I’ve worked for many different game studios, in both 2D and 3D, as an artist. I was exposed to many different software packages including Strata 3D on the Mac and Alias (PowerAnimator) on the SGI (Silicon Graphics International), among many others. I loved learning new 3D modeling, rendering, and animation applications. During my work as an artist, I used Maya, a popular 3D modeling package. While using Maya, I was introduced to the Maya Embedded Language (MEL) script. Today I use Modo with Python.

After working as a character rigger and technical artist, I learned many programming languages including MAXScript, MEL, and Python, which helped me to write tools for my fellow artists to use for speeding up production.

I eventually moved into the job of programmer as my primary job function. I wrote prototypes for Microsoft in Unreal using UnrealScript, a programming language much like JavaScript. This game studio was run by John Gaeta and Peter Oberdorfer, who are award-winning movie special effects directors.

I began my career path as an artist, then became a technical artist, and finally a programmer. This seems to be a common trend among a small variety of 3D artists like myself. I know quite a few folks who have followed the same career path.

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

1.3  A Brief History of Computer Programming: How Programming Came to Be

Basic insight into the inner workings of your computer may help understand why and how some text gets converted into computer instruction. The words that make up C#, or any other programming language get converted into what is called byte code.

Skilled programmers tend to know more about the inner workings of their computer. Like any trade or learned skill, the more you know about how your tools work, the better understanding of the entire process you’ll have.

To understand why programming tools work as they now do stems from knowing how they used to work. To understand how the current tools came about means knowing a bit of computer history and how computers used to work.

1.3.1 Mechanical Computers

The first programmable computer is arguably the Babbage machine built by Charles Babbage in the 1820s. Made of tens of thousands of moving parts and weighing several tons, it was a gigantic mechanical calculator that could display a 31-digit number. It was called the Difference Engine. in 1824 Babbage won a gold medal from the British Astronomical Society, by using his gigantic calculator, for correcting a set of tables used to chart the movement of planets through the sky.

In 1833, a countess named Augusta Ada King of Lovelace, commonly known as Ada Lovelace, met Babbage at a party where she was introduced to the Difference Engine. Several years later, it became evident that she understood the machine better than Charles himself. Despite being a housewife to the Earl of Lovelace, she wrote several documents on the operation of the Difference Engine as well as its upgraded version, the Analytical Engine. She is often considered to be the first computer programmer for her work with the Difference Engine and its documentation.

1.3.2 Logic

In 1848, George Boole gave us Boolean logic. It would take nearly a century between the Difference Engine and the first general programmable computer to make its appearance. Thanks to George, today our computers count in binary (1s and 0s), and our software thinks in terms of true or false.

In 1887, Dorr Eugene Felt built a computing machine with buttons; thanks to him, our computers have keyboards. In the 1890s, a tabulating machine used paper with holes punched in it representing 1s and 0s to record the US census. Back then it saved US$2 million and 6 years of work.

There was a moment in time where a trinary computer existed. In 1840, Thomas Fowler created a competing mechanical computer which worked with −1, 0, and +1. Even as late as 1958 an electronic version was created in the Soviet Union, but its usefulness lost out to binary which was easier to build and used less power.

In the 1900s, between Nicola Tesla and Alexander Graham Bell, modern computers were imminent with the invention of the transistor. In 1936, Konrad Zuse made the Z1 computer, another mechanical computer like that of Babbage, but this time it used a tape with holes punched in it like the tabulating machine. He’d later move on to make the Z3 in 1941.

1.3.3 Computer Science

In the 1940s, Alan Turing, a British computer scientist, designed and built an encrypted message decoder called the Bombe that used electromechanical switches for helping the Allied forces during World War II.

Between the 1930s and the 1950s, Turing informed the computer scientists that computers can, in theory, solve anything calculable, calling this concept Turing completeness. All of these components were leading toward our modern computer.

In the mid-1940s, John Von Neumann demonstrated, with the help of his theory, that the computer can be built using simple components. In this way, the software that controls the hardware can add the complex behavior. Thanks to Tesla and Bell, the Z3 was made completely of electronic parts. It included the first use of logic while doing calculations, making it the first complete Turing-complete computer. In 1954, Gordon Teal at Texas Instruments introduced the silicon-based transistor, a key component for building solid-state electronics that are used today in every computer.

1.3.4 The Origin of Software

The Z3 was programmed using Plankalkül, the first high-level programming language, invented by Zuse shortly after he finished building the machine. In 1952, Grace Hopper created the first compiler software that could translate human-readable words into machine operations. For the first time, the programmer didn’t need to know how the transistors operated in the hardware to build software for the computer to run. This opened computer programming to a whole new audience.

Between the 1950s and 1970s, computers and computer languages were growing in terms of complexity and capability. In 1958, John McCarthy invented LISP (for LISt Processor) at the Massachusetts Institute of Technology (MIT). Object-oriented programming appeared in Simula 67 in 1967. Imperative and procedural programming made its appearance in Pascal in 1970.

1.3.5 Modern Computer Language

Bell Labs, started by Bell in the 1920s, hired Dennis Ritchie in 1967. In 1974, Ritchie published the C programming language that has become the most popular computer programming language. In 1983, Bjarne Stroustrup introduced C++ as “C with Classes.”

In 2001, Microsoft released C#, a combination of several programming language paradigms taking the concepts from C, C++, LISP, Haskell, and others, and incorporating the ideas and concepts into a single language. Today, C# is in its seventh major revision, whereas C++ is in its seventeenth revision. C# is an evolving language having new features and concepts. Each revision makes the language more flexible and most important, more expressive.

1.3.6 The Future of Computer Languages

The International Business Machines Corporation (IBM), a US company founded in the 1920s, is currently engineering new technology using particle physics discovered nearly a century ago. Modern spintronics derived from the work of German physicists in the late 1920s.

Without particle physics and physicists such as Friedrich Hermann Hund, we wouldn’t understand quantum tunneling, and the solid-state storage or universal serial bus (USB) thumb drive wouldn’t exist. After the discovery and confirmation of the Higgs boson, you might ask “Why bother?” This question might not get a good answer for another hundred years, but it’ll be worth it.

C# is not the last computer language; many others have been introduced just recently. Google has introduced Kotlin specifically for developing apps for Android and Apple has introduced Swift for their iOS platform. Microsoft, not to be outdone, introduced Q# for the future of quantum computing.

Using Q# for game development isn’t likely, at least for now. Programming languages have advantages and disadvantages in different disciplines. The purpose of programming languages is to allow human readable text to be converted into instructions that run on a computer’s CPU.

1.4 C#: A Flexible Programming Language

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

1.4.1 C# Paradigm

In poetry, a haiku follows a 5-7-5 syllable tempo. A sonnet follows an iambic pentameter with 10 syllables per line and rhymes in a specific pattern. Not all prose needs to end in rhyme like haiku, but many do. Rhyming and tempo are paradigms used in poetry.

Modern programming paradigms include object oriented, functional, imperative, and logic programming. Some programming languages use one or more of these paradigms together. For instance, F# is an imperative, object oriented, and functional language. However, Haskell is purely functional.

C# (pronounced “see sharp”) is a combination of many different paradigms. As the name infers, C# is a C-style language and primarily expressed using the object-oriented paradigm. How C# expresses its different paradigms, and how you choose to write with it, is greatly up to you. Due to this flexibility, C# offers a variety of uses reaching far beyond game development.

You may have heard of C++, another multiparadigm language. It is often used by programmers who need detailed control over memory management and many other aspects of their final product. This attention to detail and design is required to produce a refined final product.

C# is an imperative language, that is to say, it carries out operations in order. It is also object oriented, so each object is allowed to do things on its own. Finally, it is also memory managed, aka garbage collected, so your computer’s memory is organized for you. However, it does provide the flexibility to manage memory if you require that level of control. Just as well, C++ has recently added garbage collection features to match C#.

Leave a Reply