Select Page

Why is C++ such a good choice for writing games?

The C++ language is used for many types of projects requiring speed, scale, and responsiveness. It’s also a great choice for video games. Why? The C++ language is more towards the low level of language abstraction but not all the way. And it’s because of this that a lot of people think that C++ is not used much anymore. That it’s been put aside in favor of higher level languages.

What about languages like Python or Javascript? Or Java or C#? They all have their use. It’s not like one language is better than another. That would be like asking which is better, a hammer or a tape measure?

So which language should you use? It really depends on the type of game that you’re building. But there’s a big advantage that C++ has over other newer languages that attempt to be easier to write. It all has to do with timing and how C++ allows you to write code that’s more deterministic than other languages due to the lack of garbage collection in C++. Listen to the full episode for more details, or you can also read the full transcript below.

Transcript

The C++ language is used for many types of projects requiring speed, scale, and responsiveness. It’s also a great choice for video games. Why?

Well, video games are written in many languages from assembly to graphical symbols you can snap together. The languages usually range from low level such as assembly to high level such as the graphical.

I saw a game design product at Target recently that consisted of a grid of squares. It looked like you place colored markers on the squares to define the background of a game. Yellow squares resulted in a coin in the game, blue squares caused water to appear, and brown were used for the ground and floating platforms. It was a simple system that promised you the ability t design your own game. Not a bad idea, really. The only problem is that it looked like you would be limited in the types of games you could design.

If you think about the languages available and rank them according to the level of abstraction, then the plastic squares on a grid would be at one end and assembly on the other end. The types of games you can create will be limited at one end and boundless at the other.

The C++ language is more towards the low level but not all the way. And it’s because of this that a lot of people think that C++ is not used much anymore. That it’s been put aside in favor of higher level languages.

What about languages like Python or Javascript? Or Java or C#? They all have their use. It’s not like one language is better than another. That would be like asking which is better, a hammer or a tape measure?

The difference between construction tools and languages though is that you can write a game with just a single language. You can’t build a house using just a hammer.

So which language should you use? It really depends on the type of game that you’re building. You can build a 2D game fast with Javascript that will run in a browser window. And maybe more. I’m not an expert with this so can’t say for sure. But I do know that Javascript is getting faster and more capable.

Python is a language that I’ve started learning about and using a bit. I haven’t tried using it for anything graphical. And for me, anyway, it starts getting hard to understand as the size of the program gets larger.

I’ve mentioned before that a game can get large and complicated quick. You really need some good libraries or a game framework to help you build anything more than a trivial game. There’s several 2D game engines available and some very popular 3D game engines.

Unity is a powerful 3D game engine designed to be used with the C# language. And Unreal is another powerful 3D game engine designed to be used with C++.

If you want to use Python or Java or any other language, then you should probably find a game engine that supports that language. Quite a few game engines compare themselves with C++ code. Or maybe the engine itself is written in C++ even though you can use a different language to program the specifics of your game.

I remember once when I was younger and watching one of my favorite TV shows, Magnum PI. There was a client who wanted to hire Magnum and when asked why said that all the other private investigators each said they were better than Magnum. The client thought about this and said that if every other private investigator was comparing themselves to Magnum, then there had to be a reason.

The same thing applies today to programming. It seems that I hear a lot of comparisons with C++. Just ask yourself why.

In many ways, Java, C#, and C++ are alike. They have similar features and capabilities. They have similar game engines available.

But languages like C#, Python, and Java in their move toward making programming easier, have included garbage collection. This means that when you need some memory, you get it and when you’re done, you just stop using it. The language will periodically go through your running program and remove anything that you don’t need anymore.

It’s a nice feature but requires a lot of work. The runtime code behind these languages doesn’t have any idea when you’re going to be finished with an object, so it has to examine everything to see if there’s anyway you might still be able to use that object. If it’s been completely forgotten about by your program, then the memory and other resources can be reclaimed. Think about the effort this needs. Not by your code. But by some other code that has to follow up and look everywhere to find discarded memory.

Think of it like this, we put public trash cans on streets and in parks for a reason. When you’re done with some sandwich wrapper, it takes a little extra effort to walk over to the nearest trash and throw it away. If there are no trash cans nearby, what then? Many of us will hold on to our trash until we do find a place to put it. But how many people do you see that just drop their trash? That throw trash out the window. I’ve seen people parking in a grocery store parking lot just open their car door, place a paper bag on the ground and drive away. Now think about how much harder it is to clean up trash that’s been left all over the place.

A language with garbage collection allows you to just drop your trash the moment you’re done with it. In a loop, no problem, you can drop hundreds or thousands of objects. It’s as easy as opening your hand and letting the empty drink cup fall on the sidewalk. Even though you don’t have to write any code to pick up and discard your trash, the language runtime will have code that does this for you. And this code will take time away from your running application because your code has to come to a complete stop while the trash is being searched for.

Now, C++ makes you walk over to the nearest trash can. It’s a little extra work on your part. But with smart pointers, it’s actually easy.

The important part is that there’s no need to stop everything while some other code goes around looking for trash. It becomes more predictable.

This is particularly important for games. Listen to episode 216 about the game loop to learn why this is important. Games are incredibly sensitive to timing. A garbage collection can really mess up this timing and cause the game to pause.

And the last thing you want is for your game to pause just as you’re about to make an important move.

C++ is not only fast, it scales well to large games, and it runs in a way that everything is under your control. There are no surprises where some other code takes time away from your program to search for things your code is done with.

Beyond just games, C++ is a good choice for any application that needs consistent performance. Your computer operating system itself is probably written in either C++ or C. Many server applications that need to respond quickly to requests will also benefit from C++. Anytime you need performance and reliable response times, consider C++.

There are other reasons to consider C++ that I’ll get to in a future episode.