Which gets us to the last big concept: state. State is the current status of all the data in a program. That might be something really simple – in a little program that grabs information from an http server the ‘state’ might just be the address of the server, the name of a file on disk you want to dump the information to, and maybe an optional stored password that you might have given the program to access the website. In something big – like a character controller in Unreal – the state is all of the data that defines the situation – everything from the current game mode to the character’s position in the world to the whether or not the character has a health bar over his head.

John Carmack – the guy behind Quake and Doom – once said in his understated way that “all bugs come from the state of the program not being quite what you think it is”. Once you leave the realm of the trivial example, everything is about managing state. Some of that is simple stuff: When you spawn an enemy, you manage it’s ‘state’ by positioning it in a good place, by making sure it has health and ammunition, by turning on its AI, and so on. But if you make some object that has to live for a while – say, you attach a blueprint of your own design to an object to make it rotate in place – then you have to make sure you understand the state of both the Unreal object you’re using and any data you are hanging on to. For example, you might want your thing to rotate slowly at first and then speed up – you’ll need to save the current speed somewhere and then keep track of it. You might have a function that adds or removes speed from the rotation … so what happens if the rotation speed gets set below zero? Does it just stop? Does it rotate in the other direction? Are you dividing a number by the speed – since math does not allow division by zero, that will be a problem.

Because confusion about state is the root of most bugs, you need to pay attention to both the data and the functions that use the data to make sure your state is as simple as possible. The more things you’re trying to manage, the harder life becomes. So making programs or blueprints is usually an iterative process of subdividing what you’re doing into small, simple chunks without a lot of state. At each level of what you’re doing you want to be able to see and understand the parts you’re looking at – once you are having trouble, it’s probably time to see if you can partition the problem a little bit.

A lot of the time the right way to break up the problem is to create a little sub-universe of data and functions that work on that data. Programmers usually call that a ‘class’ or a ‘struct’ – basically it’s a way of grouping some information and some functions that work on that information together. Classes are an organizational tool – you can do most of what you need to do without them – but they are a big help in keeping the chaos at bay.