– How come a game can have random glitchy moments if it’s all pre-written code?


I was playing FarCry Primal on PS4 and Mirror’s Edge catalyst – sometimes NPCs will get stuck in a wall or end up standing on the air. Sometimes I complete a quest on FarCry and the mission objective won’t go away until I re-spawn and an NPC will be standing 2 feet above the ground.

What causes weird minor glitches like these to happen ?

In: Technology

Well, glitches happen when the code tells the computer to do something that the programmer didn’t mean for it to tell the computer to do.

Let’s use the example of getting stuck inside of a wall. The game code says that if you’re touching the left side of the wall, then you can’t move right (elsewise you’d walk into the wall). If you’re touching the right side of the wall, you likewise cannot move left.

If somehow you end up inside of the wall, you cannot move either way because you are touching both sides. The programmers did not fix this because they didn’t expect you to end up inside of the wall.

Let’s take another glitch – this is how you got inside of the wall in the first place. In this game, you’re on a ladder, and you get shot. You don’t die, but your character is caused to leave the ladder and go through a ‘getting shot’ animation which moves then six inches to the side. Once the animation is complete, you’re inside of the wall.

One good way to think about it is that programmers take shortcuts.

When you see a character running across a complex terrain in a game, it really creates a very immersive illusion – it looks like the character’s feet are really touching the ground, and the gun in the character’s hand is bouncing due to gravity, and the character’s hair is blowing in the wind. When the character gets hit, it falls over in a very realistic way, it makes you think that the game is really simulating all of the character’s arms and legs in precise detail.

In reality, the game is taking shortcuts because computers just aren’t fast enough to actually simulate every character’s physical interactions with the virtual world in real time.

So when the character is running, you’re mostly just seeing a pre-programmed animation that looks real.

Most importantly, when the game tries to figure out if the character is going to hit a wall or a car or another character, it’s actually looking at an invisible spherical bubble around that character to see if that bubble intersects some other object’s bubble. If so, then it starts doing more precise calculations.

That bubble is a shortcut, and it’s one that works really well – except for when it doesn’t.

Game programmers will try really hard to fix all of those glitches, but there are just too many possible ways things can interact, so some will slip through.

It’s because it’s not “all pre-written code”. What you’re describing is more like a cut-scene, which are rarely glitchy.

NPC behavior, however, is usually dictated by more vague rules. A guard, for example, might boil down to something like “walk this path, until you hear noise, then try to follow the noise but don’t hit objects.” But it’s easy for the game to wind up in a situation where some of those rules conflict – like following the noise requires trying to walk through an object, or after following the noise it can’t get back to it’s assigned route – and then weird things can result.