Book Image

iOS 9 Game Development Essentials

By : Chuck Gaffney
Book Image

iOS 9 Game Development Essentials

By: Chuck Gaffney

Overview of this book

Table of Contents (15 chapters)
iOS 9 Game Development Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Variables, constants, and primitive data types


While programming any application, either if new to programming or trying to learn a different language, first we should get an understanding of how a language handles variables, constants, and various data types, such as Booleans, integers, floats, strings, and arrays. You can think of the data in your program as boxes or containers of information. Those containers can be of different flavors or types. Throughout the life of your game, the data can change (variables, objects, and so on) or they can stay the same.

For example, the number of lives a player has would be stored as a variable, as that is expected to change during the course of the game. That variable would then be of the primitive data type integer, which is basically whole numbers. Data that stores, say, the name of a certain weapon or power-up in your game, would be stored in what's known as a constant, as the name of that item is never going to change. In a game where the player can have interchangeable weapons or power-ups, the best way to represent the currently equipped item would be to use a variable. A variable is a piece of data that is bound to change. That weapon or power-up will also most likely have a bit more information to it than just a name or number; the primitive types we mentioned prior. The currently equipped item would be made up of properties, such as its name, power, effects, index number, and the sprite or 3D model that visually represents it. Thus, the currently equipped item wouldn't just be a variable of a primitive data type, but be what is known as a type of object. Objects in programming can hold a number of properties and functionalities that can be thought of as a black box of both function and information. The currently equipped item in our case would be a sort of placeholder that can hold an item of that type and interchange it when needed, fulfilling its purpose as a replaceable item.

Note

Swift is what's known as a type-safe language, so we should keep track of the exact type of data and even it's future usage (that is, if the data is or will be NULL), as it's very important when working with Swift compared with other languages. Apple made Swift behave this way to help keep runtime errors and bugs in your applications to a minimum, so we can find them much earlier in the development process.

Variables

Let's look at how variables are declared in Swift.

var lives = 3      //variable of representing the player's lives
lives = 1          //changes that variable to a value of 1

Those of us who have been developing in JavaScript will feel right at home here. Like JavaScript, we use the keyword var to represent a variable, and we named the variable lives.

The compiler implicitly knows that the type of this variable is a whole number, the primitive data type Int.

The type can be explicitly declared as such:

var lives: Int = 3    //variable of type Int

We can also represent lives as the floating point data types double or float, as follows:

// lives are represented here as 3.0 instead of 3
var lives: Double = 3           //of type Double
var lives: Float = 3            //of type Float

Using a colon after the variable's name declaration allows us to explicitly typecast the variable.

Constants

During your game, there will be points of data that don't change throughout the life of the game or the game's current level/scene. These can be various data, such as gravity, a text label in the Heads-Up Display (HUD), the center point of character's 2D animation, an event declaration, or the time before your game checks for new touches/swipes.

Declaring constants is almost the same as declaring variables.

Using a colon after the variable's name declaration allows us to explicitly typecast the variable.

let gravityImplicit  = -9.8         //implicit declaration
let gravityExplicit: Float  =  -9.8 //explicit declaration

As we can see, we use the keyword let to declare constants.

Here's another example using a string that could represent a message displayed on the screen at the start or end of a stage:

let stageMessage  = "Start!"
stageMessage      = "You Lose!"   //error

Since the string stageMessage is a constant, we cannot change it once it has been declared. Something like this would be better as a variable using var instead of let.

Tip

"Why don't we declare everything as a variable?"

This is a question sometimes asked by new developers and is understandable why it's asked, especially since game apps tend to have a large number of variables and more interchangeable states than an average application. When the compiler is building its internal list of your game's objects and data, more goes on behind the scenes with variables than with constants.

Without getting too much into topics, such as the program's stack and other details, in short, having objects, events, and data declared as constants with the let keyword is more efficient than var. In a small app on the newest devices today, though not recommended, we could possibly get away with this without seeing a great deal of loss in app performance. When it comes to video games, however, performance is critical. Buying back as much performance as possible can allow a better player experience. Apple recommends that when in doubt, always use let at the time of declaration and have the compiler say when to change to var.

More about constants…

As of Swift version 1.2, constants can have a conditionally controlled initial value.

Prior to this update, we had to initialize a constant with a single starting value or be forced to make the property a variable. In Xcode 6.3 and newer, we can perform the following logic:

let x : SomeThing

if condition
{
  x = foo()
}
else
{
  x = bar()
}
use(x)

An example of this in a game could be as follows:

let stageBoss : Boss

if (stageDifficulty == gameDifficulty.hard)
{
  stageBoss = Boss.toughBoss()
}
else
{
  stageBoss = Boss.normalBoss()
}
loadBoss(stageBoss)

With this functionality, a constant's initialization can have a layer of variance while still keeping it unchangeable, or immutable through its use. Here, the constant stageBoss can be one of two types based on the game's difficulty: Boss.toughBoss() or Boss.normalBoss(). The boss won't change for the course of this stage, so it makes sense to keep it as a constant. More on if and else statements is covered later in the chapter.