Building a Simple Game
A technical deep dive into concepts involved in developing a fun game
Q: “I’m a software engineer and would like to build my first game. Which concepts should I be familiar with that are unique to game development?”
I’m back from summer vacation, and figured it would be interesting to dive back into a less “typical” topic, but one that is both a fun one and very familiar to many of us: games.
We covered Game development basics at a high level last year, in a previous article with– game developer, former university lecturer and director of AI and Games, and author of the newsletter. In response, several of you said you’d like to learn more in a hands-on way and get into the code.
And so Tommy is back to tell us more about game programming in technical detail. I find it’s easier to learn concepts by using them, which is exactly what we do in today’s issue. Tommy explains the concepts behind GameObjects, Scenes and Prefabs and walks through how to build a simple game. Throughout, Tommy will point out similarities between game development and traditional software engineering: including how it feels like some parts of Unity “butchers” some object-oriented principles familiar from “traditional” software development.
In this article, we cover:
Introducing game engines. The concept of GameObjects, Scenes, Prefabs and Scripts.
Programming your own components. The importance of MonoBehaviour, how a frame is executed, threading basics, and implementing the player object we can move around with the “A,” “S,” “W” and “D” keys.
Building interactions. Using Prefabs to define coins; creating collision logic; the concept of triggers; debugging; and finishing the game so the player can collect the coins.
Structuring games with the MVC framework. Building games using the Model-View-Controller approach helps keep a clean structure, and will also help with debugging.
Leaning on your skillset. Having a background in “traditional” software engineering is a huge benefit for a game developer. With these basics, picking up game development is much easier.
Getting started. Resources to look to, to build your first game.
This article goes hand in hand with a tutorial in which we:
Create a new Unity project
Create a player object that responds to keyboard inputs.
Create coins of different value which players ‘collect’ by colliding with them.
You can follow the tutorial and create a simple game by downloading a version of Unity and Visual Studio Code, which are both free for personal use. To do the tutorial, follow instructions in paragraphs marked with: [Tutorial step]. Code scripts we go through can be found here.
As a heads up, Tommy is hosting his third annual AI and Games jam, 15-22 September. Over a week, participants build their own game utilizing AI in some form. Learn more or sign up if you’re interested.
With that, it’s over to Tommy.
1. An introduction to game engines
In my previous article, we briefly discuss the concept of a game engine. If you’re somewhat familiar with games, you may have heard game engines mentioned as a part of game development, and even marketing.
A game engine is a framework designed to support common functions, and provides a myriad of libraries to bootstrap development. This can include rendering for 2D or 3D graphics, physics and collision detection, networking, memory management, asset storage and streaming, ‘scene’ management (i.e. levels), artificial intelligence (AI) behaviour for non-player characters (NPCs), and much more.
Most games are built with an established engine by popular third-party providers like Unity, Unreal and CryEngine. Also used are proprietary engines owned by studios, such as EA’s Frostbite (Battlefield and FIFA), Ubisoft’s Dunia (Far Cry), and Snowdrop (The Division, Mario + Rabbids). These days, it’s rare a game is built using a custom engine, which is different to 20 years ago, when only a handful of engines existed as products to licence out to game studios.
Today, the Unity and Unreal engines meet most developers’ needs and many games are built in them. Studios seek to hire developers familiar with these engines, even if only to train them in their proprietary tools.
Which engine should you build with? This is an obvious question when starting a new project. The answer depends on which type of game you want to make, your target platforms, and your own – or the team’s – expertise.
Unity is increasingly popular with smaller teams and for single-developer projects built by one person, due to how easy it is to get started and iterate features quickly. Unreal has a steeper learning curve, but often produces slicker-looking games without much effort.
The pain points of Unity and Unreal engines differ greatly. It’s often said Unreal is a more practical engine for large AAA titles. Its creator Epic Games is a game developer which has shipped games for over 30 years, unlike Unity. What’s more, Epic Games has shipped games using its own engine since the Unreal series of 1998-2007, followed by the Gears of War franchise from 2006-2013. Since 2017, Epic Games has supported the ever-popular Fortnite.
Now, we’re going to dive into the practicalities of using a game engine. To help explain the fundamentals, we’ll implement a simple feature in Unity: a player avatar which collects items in its vicinity, and we also highlight specific aspects of the engine. You can follow along by installing Unity on your machine.
These are components of a typical game in the language of game engines:
Sprites for 2D graphics
Models and textures for 3D graphics
Physics elements like gravity
Cameras for rendering
Code that facilitates specific gameplay features
… and more!
Naturally, working with so many things requires structure, as there can be millions of components in a game project. So we need to create a structure to navigate all these elements.
All game engines contain an identifier for an individual object. The only difference between game engines is the given name of this individual object. Unity uses ‘GameObject’. At minimum, each GameObject has three core properties:
Name: Objects always have a unique identifier. They may also carry a ‘tag’ so they can be grouped together with other GameObjects for easy identification.
Transform: The position in the virtual game space which the GameObject occupies. This is stored as a 2D or 3D vector, depending on the design.
One or more components: A component in Unity is an element that can be added to a GameObject to enable some kind of behaviour or interactivity. By default, all GameObjects need to have the Transform component.
Scenes are how collections of GameObjects are stored. In traditional video games, each level can be an individual scene. You can also have multiple scenes that load in and out of memory based on specific contexts, such as elements like the main menu or credits screen.
Prefabs are templates to create GameObjects at runtime and are useful for common GameObjects which are frequently used. An example of a Prefab is the coins in Nintendo’s “Super Mario” series.
GameObjects can also be built manually within the game editor (in our case, we use Unity as the game editor). You can also generate a GameObject directly via code. Just know that it’s often expensive in memory reallocation to generate or destroy a GameObject at runtime. This is why object pooling is a common tactic for maintaining game performance.
Starting a new project
[Tutorial step] Let’s begin by creating a new 3D project in Unity. Start with Projects → New Project → 3D (Core). The engine will create a new scene like this:
We have two GameObjects: a Camera, and a Light source. Both are displayed in the ‘Hierarchy’ view:
Each is a unique object with multiple components which can be examined in the ‘Inspector’ window. For example, if you click on ‘Main Camera,’ you’ll see the ‘Inspector’ window on the right of the project: