Advertisement

Should I learn c++ for game dev?

Started by September 25, 2015 12:15 PM
24 comments, last by Kayhen 9 years, 3 months ago

Outside low latency and high performance things, the big deal with learning C/C++ is that you are forced to deal with proper memory management, which is a little(a lot?) opaque with languages like Java (or completely hidden in "esoteric" languages like ML or other functional mass destruction weapons) . In my opinion proper memory management is a must-have requirement of every programmer and software developer, doesn't matter it the tools you manage at works doesn't involve explicitly pointers or not.

"Recursion is the first step towards madness." - "Skegg?ld, Skálm?ld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/
Do you want to work on a AAA development team making games for consoles and/or PC? You have to learn C++. No other language is supported on all three consoles plus PC. And the other languages typically have additional overhead that most AAA developers don't want to pay.

Working on your own game or in a small indie group? Write in whatever language you like, within platform and performance restrictions you set yourself (i.e. you're probably going to have to write Java at some point for an Android game, and Objective-C for an iOS game, assuming you don't use some sort of wrapping library). Minecraft is written in Java. Most Unity-based games are written in C#. Even Python has a set of libraries for game development.

Learning C++ is still beneficial however, as it does not hide nearly as much of the computer from you.
Advertisement


you are forced to deal with proper memory management

Yes, traditional c++ encourages manual memory management.

In most large modern projects though manual memory management is discouraged. For example in unreal engine, the example given above, a lot of stuff has pointers managed for you and reference counted and deletions strictly marshalled to ensure that you don't delete something that another part of the engine still holds a reference or pointer to. Very rarely do you find yourself calling new or delete.

My advice if you're starting out in C++ is avoid as much dynamic memory management as possible. Allocate on the stack and let the runtime library deal with freeing when things go out of scope. Only use raw pointers when you really must. Once you start on C++ you will know what all this means.

As for should you start on C++ at all, definitely you should and sooner rather than later, as a programmer it's one of the most useful things to know and it's understanding opens the door to many other programming languages and libraries.

Good luck and have fun!


you are forced to deal with proper memory management

Yes, traditional c++ encourages manual memory management.

In most large modern projects though manual memory management is discouraged. For example in unreal engine, the example given above, a lot of stuff has pointers managed for you and reference counted and deletions strictly marshalled to ensure that you don't delete something that another part of the engine still holds a reference or pointer to. Very rarely do you find yourself calling new or delete.

My advice if you're starting out in C++ is avoid as much dynamic memory management as possible. Allocate on the stack and let the runtime library deal with freeing when things go out of scope. Only use raw pointers when you really must. Once you start on C++ you will know what all this means.

Yes, modern C++ and big projects doesn't have a lot of manual memory allocations and alignments. In modern C++ and big project you build you own allocators, your own reference counter smart pointers, your own pools, etc... But to proper understand or even more to proper build your own "automatic" memory management system you need to know how to deals with pointers, pointer arithmetic, alignments, proper deallocation and of course leaks thrown from exceptions (mainly from constructors). Otherwise it's better to go with C# or Java if you do not understand the tool you are using.

Of course using STL containers on beginning is a good to start. But when you need to build your own system, you must understand all the annoying and traditional/C_with_classish things.

"Recursion is the first step towards madness." - "Skegg?ld, Skálm?ld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/
Even reference counting is a hell of a lot more manual than garbage collection though.
Ref-counting still forces you to think about strong vs weak pointers, circular references (which become leaks in Ref-counting, but are solved with GC), interactions with raw-pointers, thread-safety of destructors, etc...

One of the reasons we use C++ is that it lets us choose which (mostly automated) memory management system to use, and also lets us still micro-optimize memory placements and locality of reference.
e.g. on more than one engine that I've worked with, there's been no global new/malloc heap -- instead, when new'ing an object, you have to manually specify which specific heap/pool/stack it will be placed in.

C++ also lets us create multiple objects within a single contiguous allocation - a pattern I've seen in multiple engines is two-pass-initialization. Fisrt a system "dummy allocates" all its required objects, which just increments a counter to measure the memory requirements. Secondly it actually allocates one single large block of that size, and performs the same logic except this time it actually creates all its required objects within that allocation.

C++ is a standard. It's the most commonly used language in Programming simply because you have control over EVERYTHING. That's what makes it so fast and effective. But it's also incredibly complicated when you go into detail. However, I recommend starting with something easier if you are new to programming in general. Or... you are a wide eyed hopeful person. It's not really an easy language to learn, and a LOT of game programming is based on technical, scientific, and mathematical knowledge. Less so if you are just a scripter.

I'd recommend starting with Lua and the LOVE game framework first. Lua is easy to learn, newbie friendly, and would be a great introduction point. There are also some really good tutorials from PacktPub, and around the net to help you get started.

Once you feel comfortable, and like programming, move onto C++... starting at ground zero. You need to learn the language, and basic algorithms before you can really do anything amazing with it.

Advertisement
Lua is easy, but it is also VERY minimalistic.
- Only one container type, learning about using the right data structure for the job goes out the window.
- It does not have some common statements like "continue" (mailinglist ends with a loop+break to emulate "continue" http://lua-users.org/lists/lua-l/2006-12/msg00440.html I wouldn't dare explaining that to a new programmer)
- Language is 1-based rather than 0-based. You need the latter for easy 'div' (integer /) and 'mod' (often %) operations.
- 0 is not false:
$ lua
  > if 0 then print("true") end
  true
- No classes (out of the box), but metatables for inventing your own notion of classes.

These language design decisions make Lua less useful as first language to learn, imho. New users pick up the first language as reference point. You better start with a language that is closer to the usual languages.

Mm, I said Lua, because you can learn how loops and stacks work without being constrained to types. When the OP feels comfortable, he/she can then move on to C++ which is a statically typed languge.

With a foundation in how loops work, it's easier to understand how types function without worrying about some other more critical foundation. And then with types, to classes, then polymorphism, algorithms, then patterns.

This is just going on a whim of simplification. I don't know the OPs capacity, and I learned C++ as a first language with minimum problems.

Learn C++.
Don't learn with C++.

fin.

If you want a career as a programmer, then learn C++ at some point in time, because it's what many game-dev companies use, more importantly because it's a hard-to-use, overly-complex, low-level systems-programming language that is a rite of passage for any experienced programmer. You should also learn C at some point in time.


I agree, except the C part. Not really sure what value learning C really brings to the table in this day and age. If you get a job in embedded systems or hardware I suppose, but for most programmers, even game programmers, Im not really sure what they'd get out of it?

This topic is closed to new replies.

Advertisement