Advertisement

Honest question about C++ vs C#

Started by May 23, 2017 04:40 AM
11 comments, last by Nypyren 7 years, 6 months ago

So I have been thinking about trying to build my own game from scratch (I don't know how far I will go but I want to at least try) and the first issue i have to figure out is what language I should code it in.

Now back in the day I recall being told that for making a video game C++ was really the way to go, and while C# may be easier to approach, it doesn't have the same power that would be necessary if you were trying to get the most out of your game.
And while whoever told me that probably wasn't a programmer, I basically just took that as it is. After all, video games are pretty much the most complicated things that programming is ever used for; we're trying to create an entire virtual world that needs to play out in real time and update 60 times a second, it gets pretty complicated. So it wouldn't surprise me if the wrong choice of language might cost a game a few extra frames at a critical moment, or push the memory requirements just enough to cause noticeable problems.

But now it is a few years later, and while I still am pretty amateur when it comes to programming, I understand some things a lot better than I used to. And now as I look at the situation, I honestly question what I was told before. How much does it really matter what language we program a game in? I mean for the most part "what language it is in" doesn't even exist once the code is compiled, and how efficiently a program runs is going to come down far more to how it was written more than anything else. A few bad practices in the writing can tank the experience far more than a language choice ever could.
And on top of that, there are some noticeable examples of games that were written in other languages. Minecraft was written in Java, for Pete's sake, and while the visuals may seem outdated, it still is an engine that has to track an immense and dynamic world. And sure, something like Unreal Engine is written in C++, but that engine holds ties back to before C# was created.

So I'm honestly wondering - not only if it really makes a difference if you create a game/engine in C++ versus C# - but how it would make a difference, why C++ would be a better choice than C#. What kind of differences could someone really expect to see?

I've been learning how to write in C# and so it has become comfortable to me. I look at samples of C++ and it looks like I'd have to do more than just get used to a few nuances. So I find myself leaning toward wanting to write my code in C# just to make it easier on me.

And since "What kind of game are you making" is sure to come up (and probably be reasonably relevant) I guess I'll say, well, on the far end of the spectrum I would like to build a functional 3D engine along with some editing tools. Realistically I'd have something that intentionally is designed to look like an older game, but I still want something that runs efficiently. I hate the idea of having a simple game that takes up more resources than it should. If I were to style a game to look like something from an old era, I'd like my game to be able to run on an old computer. (Even if not "period accurate" I'd still want something that was close.)

Read my webcomic: http://maytiacomic.com/
Follow my progress at: https://eightballgaming.com/

The real answer is to use both if you need to, using whichever one is better at each part you need to write. You can interop them. The Unity engine is a big example of this.

The *vast* majority of the code you write will be effectively the same in both languages. You shouldn't be spending most of your time writing code to manage memory, or initialize variables (the two places where the languages differ the most). Most of the time you're going to be writing either simple gameplay code, or complex algorithms where the biggest difference will be in the structure of that algorithm rather than its incarnation in code.

The downside of trying to describe the differences to you before you choose which one to use is that you will be missing the same experience that people go through when they actually sit down to use both languages. So that's what I suggest you do: Use both languages - then you can make well-informed decisions that you KNOW are true, because the experience is yours and there's nothing lost in translation.
Advertisement
I'll try to answer taking into account all your statements/reasons/insecurities.

First and foremost you compare waaaaay different scenarios, like Minecraft being written in java and Unreal Engine being written in C++, than you sort of ask whether complex game engines could be written in C# instead of C++. Before moving forward with my answer, let's make this clear!
You have to be honest with yourself in what do you want to achieve because this choice is indeed dependent on your goals.

Honest short answer:
The important question is what do you want to build? Games? 2D games like Stardew Valley or Terraria, or simple looking 3D games like Minecraft with relatively simple physics simulations? These can also be made in javascript running in the browser even with reasonable performance...
Or engines and tool-sets for educational reasons or to be used for making simple games with? Also can be achieved if you are not shooting for Unreal engine heights.

Yes both games and reasonably complex engines can be done in C#, but you have to decide first what is your actual goal. It is a perfectly viable and comfortable language for games one or a small team wants to and can build!

Honest long answer:
I've been using both C, C++ and C# for a long while now, the former two for high-performance applications and I can tell you Unreal engine (or an engine with matching performance) most probably could not be written in C# alone because of the language design. C/C++ are system level languages and they are pretty darn good at allowing the programmer to explicitly lay out stuff in memory the way as he/she wants it, thus memory and memory access time being a pretty big influential factor in performance these days it can pump out the most of the hardware. C# can not, period. But pumping out the most of the hardware and having an application that runs well is a whole different world and it is just as easy or most probably even easier to write an application that runs well in C# compared to C/C++. This means making the new DOOM game in C# could be done, as all the features could just as well be coded in it, but due to its memory handling mode there is probably no way on earth to make it run on an XBox One or a PS4 (or even on a high-end PC), except if you write most performance intense parts in C/C++, but I'm guessing than a big portion will not be C# (most of the engine => rendering, physics, low-level AI...).

What you get from C/C++ compared to C# is simple (high-level look): much better control over memory (possibility of much better performance) and a tiny bit more performance out of the box (being fully pre-compiled and more easily optimized languages they are faster runtime, but this is not significant nor important for most games).
What you get from C# compared to C/C++ is also simple (high-level look): a generally simpler and a more strict language + easier day-to-day work-flow (probably less bugs, e.g.: no stack corruption, no implicit casting issues, probably no memory leaks at all ;) ) and some nice high-level language features (e.g.: runtime type information/reflection + better serialization constructs etc...)

Some real-life examples:
Games made in C# (note: these were not made in Unity3D or another combination of C/C++ + C# and these are just some few famous examples, there are probably a zillion more):

Magicka
Stardew Valley
Terraria
Bastion
Transistor
Bloodline Champions

As you can see the list is pretty diverse, there are some 2D games, some 3D games and there is even a MOBA game similar to League Of Legends (Bloodline Champions was a relatively popular MOBA back when it came out, it has a successor now from the same studio).

Engines made in C# (note: some may use a rendering backend or a physics library written in C/C++ but other than these parts the lion's share is C#):

Xenko
Wave
Duality
Otter2D

Again a pretty varied list, Xenko and Wave being full-blown 3D engines with huge tool-sets even adequate for AAA games, Duality being similar to Unity2D in pure C# and Otter2D working like a really-high level game development SDK.

About low-end hardware. I regularly check my games on my older PC-s. My previous title Operation KREEP runs perfectly fine (60 fps) on my 6 years old single core 1.6Ghz intel atom CPU + 1GB RAM + Radeon Mobility 5430 GPU laptop. Probably it would work perfectly fine on the XBox 360 too (can be compiled to it, but haven't tested it) which is a 10+ year old HW. The only caveat is the GPU requirement as XNA, the rendering backend I used, dislikes trash-can intel GMA cards, but works on those too but performance can be pretty bad.

Hope this helps :) ,
Br.

Blog | Overburdened | KREEP | Memorynth | @blindmessiah777 Magic Item Tech+30% Enhanced GameDev

So realistically, in my opinions on this... it really wont make that much of a difference unless you plan on making a "Large" game, C# is a pretty powerful language that can do a lot of things ya know. It doesn't have the in depth kind of optimisations you can make with C++ really, but as mentioned it really depends on the scope of the game youre creating. I doubt that as a single dev your game would be to a stage where it really requires the additional control / optimisation gains you can get with choosing C++ over C#.

I've coded a few basic games using C# and its generally pretty fast to work with and I've not really had any issues with the game running poorly and I mean Unity utilises C# and look at the amount of unity games around that run pretty damn well.

I would also agree with Nyp though and suggest you do aim to learn / work with C++ for a while too, its a great language to just have under your belt, and working with it for a while would give you the chance to see what you can do with it and make a more informed choice of if you feel your project would benefit from it, or would benefit from the ease and speed that could be gained with C#

Moving to For Beginners.

So I'm honestly wondering - not only if it really makes a difference if you create a game/engine in C++ versus C# - but how it would make a difference, why C++ would be a better choice than C#. What kind of differences could someone really expect to see?

For why one is a better choice than another... In very broad terms, C++ is better suited toward systems-level work, and C# is better suited toward faster development. It has fewer abstractions about the machine, meaning it provides more options for experienced developers to tune performance. C# provides many useful abstractions which make development much faster and follow more natural thought patterns, meaning it is easy to get more work done quickly.

For games choosing a language ... I've worked on many games that use both C++ and C# in the game at once. Most major games are built from a large collection of languages. The game I'm working on at the office now has six mainstream languages (C++, C#, assembly, Lua, Python, and a bit of SQL) and several custom languages for AI scripting and such. Then there is the back-end with Java, JavaScript and HTML, SQL, python, and I'm sure a few more.

For what differences someone could be expected to see ... For a well-written program, the end user should not see any difference at all. For the programmers, C++ code will tend to be a little more difficult to follow, and it will require more discipline to follow good practices. There will be more time spent compiling, and many features will save nanoseconds at a time due to the minutes-long (and often hours-long) builds. The C# code will be a little more easy to follow, but the developers will be probably be fighting a battle for performance, particularly toward the end of the project. If good choices were not made early on, the battle for C# performance can be quite severe, causing huge amounts of code to be rewritten following better practices.

For your skill level just starting out ... It doesn't matter. Both are good, and both can do far more than you are able to use right now. If you become a professional game developer in the next several years you will be expected to know both languages (and several others) as part of the standard programmer skill set.

C++ isn't magically faster by default. C++ can be faster by a huge amount when experts use it's many dark secrets to carefully organize bitwise data layouts in RAM and craft perfect data access patterns. People who don't have 10 years of C++ experience aren't going to be doing this stuff... So at an expert level, C++ has more tools. At a beginner level, both are similarly fast, but C++ is much easier to write horribly buggy code.

An analogy if I may:
Race cars are really fast, and race teams build their own cars... Therefore I should build my own car if I want to go fast!
1) If I tried to finish a lap at high speed in an F1 car, I'd end up crashing due to my inexperience with its power.
2) if i tried to build an F1 car, it would take too long and I'd give up.
3) if i just got into my 10 year old Japanese sedan, I would be comfortable driving it and I could complete a lap without crashing. Sure I'm going slower than the race team, but infinitely faster than I would be if I was trying to copy their methods!
4) now, if you put the professional race driver in my sedan, he will beat my lap times by far... And likewise the code from a C# expert will be far faster than the code of a C++ beginner!

If I wanted to make a career out of driving fast I should take the time to learn how cars are assembled... And if you want to make a career in game programming you will need to eventually learn both C++ AND C#!

But for now, it's best to stick with whichever one you find easier, and for what it's worth, if I was to start a career in race driving right now, I'd go buy a Go-kart first :D

Advertisement

Just wanted to note that I really love the race car driver analogy. I'll steal it from you.

As has been stated, the real answer is both if you need to. I have programmed with C, C++, C# JS, and Assembly for more microcontrollers than I care to remember. I personally am a die hard C and Assembly coder. I mostly write systems level code, ISR's , drivers, system services, artificial intelligence and video processing, etc. In fact I only use a few features of C++ because quite frankly the computer doesn't work that way and the less you try to make the computer run code that is written different than it operates, the faster your code is likely to be In My Opinion. Working with C++ and C/ASM its easier to shoot yourself in the foot, but there is also more flexibility to optimize your code. So what I end up with is a lot of classes that encapsulate both the data and related functions, but don't use a lot of OOP gymnastics. This keeps my code clean, readable and most importantly FAST. I've had customers remark before about how fast my code runs and they cant believe I met all the requirements in such a small binary. I've even had instances where management forced me to include garbage (usually a raw RGB of the view from my office window) in the DLL because the customer refused to believe a e.g. 75K DLL could do what it did.

So my recommendation is to program in C++, or C/ASM if you have the determination. It's is much easier to learn C# after learning C++ than the other way around, and in the end, all programmers become C/C++ programmers.

Thank you all for providing so much with these replies! This was all very helpful.

The real answer is to use both if you need to, using whichever one is better at each part you need to write. You can interop them.

Whathe WHAAAA?!?!

I personally am a die hard C and Assembly coder.

OMG You sound like you are even older than me! (Mad respect though.)
For what it’s worth… My “dream” code/engine wouldn’t be something comparable to Unreal Engine, that is, it wouldn’t be comparable to modern Unreal. But… Maybe Unreal Engine 1 or 2? (Or maybe just an editor like that.)
What I’ve been inspired by is this video where a guy creates an old sector-based engine from scratch, right before our eyes. I can’t stop thinking about that. He built that renderer with only a few days worth of work. I want to do that. And while yes I know that there is a big difference between how fast a veteran who knows his game can work versus someone like me, but even so it inspires me to think that it is possible for someone like me to do the same thing, even if it takes a lot more time.
And I love how he wrote it to render off of software (as far as I can tell) instead of relying on OpenGL or Direct3D. I’d love to do the same! …But I suspect I’ll change my mind once my toes get wet and let the hardware do the heavy lifting. I’m also a bit inspired by reading this article about the Quake 2 engine (and other articles on that site about other engines) that make me want to play around with controlling and fine-tuning a base renderer.
For the last 20 years of my life I’ve had wild dreams about doing this kind of stuff, but I’ve always believed that it was so far out of my reach that it wasn’t worth trying. Suddenly I’m getting glimpses that this stuff might be something I can actually understand. And I want to try. I need to see what I can do.
…
What I have been thinking about for the engine is a sector-based engine that would actually work in true 3D, combining the ease of creation of a sector-based engine like Build or Doom, but with the versatility of a fully 3D BSP engine like Quake and Unreal. It’s quite simple, really. At its core it would render in the same was as the Build Engine but with key changes to the editor.
Of course, it wouldn't be practical for modern ultra-high-poly games, but if I wanted that I would just use UE4.

Read my webcomic: http://maytiacomic.com/
Follow my progress at: https://eightballgaming.com/

I am by no means an I experienced game developer. I have experience with C professionally, C++ academically and C# as a hobbyist. If you are currently comfortable with C# I would continue down that path. It is far better to have intermediate skill in one language than to be a beginner in a few languages. If you are not an advanced programmer and you desire to make a game I would consider Unity3d as you can use your existing C# skills.

I encourage you to sty in your lane get strong with C# and then start developing projects using C#. It's a good language it's modern and fun to code in.

The Quarry Works Creed

We who shape mere stone must always envision cathedrals

This topic is closed to new replies.

Advertisement