Reading and Writing Code – Getting Started with Unity Game Development

By Brian Moakley Jul 18, 2023 #code #unity

So you want to make games? Believe it or not, you’ve picked the perfect time to dive in. The tools are quite intuitive to use, and while development will require some math, there are a TON of communities and resources that will help you out.

Note: This is the first article in a series of articles that will teach you C# in the context of using Unity. For other articles, see the Beginning C# with Unity content listing

Reading code for the first time can be a little intimidating. (Photo by Michelle Tresemer)

That’s the easy part. The scary part – the part that probably breaks you out in cold sweat – is reading and writing code. More specifically, learning the C# programming language. Learning a programming language can be overwhelming, but it’s not hard like learning a spoken language. Even the simplest spoken language can take years of study for a simple conversation. It may take only a few weeks to read and write code. In that time, you can create some pretty wild games.

Some might be thinking, “Code looks hard! Just look at <insert famous show>”

Movies are not reality. Especially when it comes to code. Oftentimes, on movies and television shows, you’ll see a “hacker” typing furiously on a workstation, while reams of code flow by the screen. Code is depicted in glowing letters looking more like magical runes.

That’s not good code. At least, not in the practical sense. What is good code? There’s no better way than diving in.

Reading Code for the First Time

So what is good code? In my opinion, good code is well-documented and easy to read. It’s not meant to be obscure. In fact, good code is boring code.

Take this bit of code:

fun printName(name: String, numberOfTimes: Int) {
  repeat(numberOfTimes) {
    println(name)
  }
}

This is actually Kotlin code. Kotlin is a programming language designed for writing Android apps. Take a moment to break it down.

Start with the first line.

It looks weird but it defines something called printName. On that line, there is something called name and another thing called numberOfTimes. On the next line, you see something called repeat. It’s repeating based on the numberOfTimes. Then there is something called println otherwise known as printLine. It’s printing the name to the screen.

In summation, this block of code will print out a message to the screen a fixed number of times. Here’s how it is used:

printName(name = "VegetarianZombie", numberOfTimes = 10)

Here is the result:

VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie
VegetarianZombie

Congratulations!

You’ve read some code. While the computer will ultimately run the code, people read, write, and maintain the code. Easy-to-understand code can produce fewer bugs and happier customers.

Note: Hollywood almost never gets code right. The Tumblr blog MovieCode actually tracks down the source code depicted in movies. It describes the origin of the code and what it is doing. The code often has nothing to do with the plot.

You’re going to spend a lot of time writing code. This begs the question, what do you use to write code? Before answering that question, take a moment to think about it from another perspective.

Learning How to Write Code

Imagine you are a writer, looking to publish a short story. What tool does a writer use? You might use a word processor program like Microsoft Word or jump on Google Documents. These programs provide a lot of general tools for writing all sorts of text. But these tools may be too general. Instead, you may choose Scrivner. Scrivner provides tools for novelists.

But, you can also choose to write your story in an email editor or even in a simple text editor. Some may even choose to write their story with pen and paper, or even on a typewriter. George Lucas chose to write the Star Wars prequels on giant yellow legal pads.

These two machines can produce code. One is far easier than the other (Photo by Glenn Carstens-Peters)

The same goes for code. There are fancy editors with tons of tools built into it, but you can use anything. You can even use a typewriter (although you’d have to scan in the resulting pages). Code is just a simple text file that the computer reads and “compiles” into machine instructions.

Starting out, you should use what is known as an Integrated Development Environment (IDE). These used to cost thousands of dollars back in the day, but these days, they offer free versions. This will turbocharge your code writing and save you time.

Yet, at the end of the day, when you write code, you are actually writing a simple text document. That’s it. It’s not magical. It’s not even special. It is just text that you can write in an email.

So as you can see, reading and writing code isn’t something exotic. It’s just a skill that anyone can learn. For this series of articles, you will use an IDE, but as you start making games, you should keep in mind that an IDE is just one tool in your toolbox, not the entire toolbox.

Now that you tasted a little code, it’s time to get busy. To do this, you first need to install Unity itself. You’ll do this with Unity Hub in the next article.

By Brian Moakley

Brian Douglas Moakley is a writer and technologist who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply

Discover more from Jezner Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading