146 points · by markatkinson · 9y ago · 95 comments
Is C# becoming the lingua franca of modern game engines?
1. The GC provides a huge time-saving net over a ton of possible memory errors. Not to mention the code is so much easier to read when you aren't having to deal with memory on every line.
2. The debugging experience is brilliant - because there is always metadata available, you can literally inspect everything.
3. Compilation time is super fast.
4. Code samples are available for pretty much everything. And when there aren't, you can always interop into C++ or whatever else.
Cons:
People unfamiliar with the language forget about the GC and let it do all their dirty work. They then get angry when it bites back because of all the allocations they are doing each frame. The answer is to use pooling, and structure your code in a way that works with the GC (not against it), and all these problems go away.
As others have pointed out: the CLR has had a concurrent low-latency GC for a long time now and now that it's all open-source there is nothing preventing Unity from adopting it.
I haven't noticed garbage collector related issues with mono, although I don't play the type of games where that would be most noticible. I have seen a number of games with memory leaks that require a restart after a while, but that is the case under windows as well. I've recently stopped buying Daedalic games because they can't manage to make a simple adventure game with almost no movement work on a system with 4GB memory, but they are by no means the only ones with this problem.
C# on top of C++ might not help that much other than encouraging more game designers to use C#.
Mono today has two code generators: Mono's built it one, and an LLVM one. Both generate code after Mono loads and processes the assembly.
Unity developed a different front end to the runtime that does the compilation from IL to C++, the result still requires the Mono runtime to provide services at execution time.
They are currently upgrading their Runtime and are making a version with all the greatest features available coming.
It's hard to emphasize how bad the Unity Mono GC is. It's a Mark-and-Sweep garbage collector, which is like the simplest-yet-least performant approach to GC there is. Nearly every other GC in production use in other stacks is better.
I am not up to date on the state of this in the latest Unity releases, I just know games I play on PS4 and OSX made with Unity stutter in places where they don't stutter on Windows.
GCC was for almost twenty years quite bad versus other commercial compilers, yet it is worshipped nowadays for its performance.
https://forums.unrealengine.com/showthread.php?2574-Why-C-fo...
- As an engine and its community grows, there is increasing pressure to expose more of the its native C++ features to the scripting environment. What starts out as a sandbox full of toys eventually grows into a desert of complexity and duplication.
- As the script interface expands, there is a seemingly exponential increase in the cost and complexity of its interoperability or "interop" layer where C++ and script code communicate through a multi-language interface for calling functions and marshaling data. Interop becomes very tricky for advanced data types such as containers where standard scripting-language idioms differ greatly in representation and semantics from their templated C++ counterparts.
- Developers seeking to take advantage of the engine's native C++ features end up dividing their code unnaturally between the script world and the C++ world, with significant development time lost in this Interop Hell.
- Developers need to look at program behavior holistically, but quickly find that script debugging tools and C++ debugging tools are separate and incompatible. Seeing where script code had gone wrong is of little value if you can't trace the C++ that code led to it, and vice-versa.
Aren't those core tenets of game production? :)
A mixed C/C++ callstack is so benign and common that it likely won't even register as being a "multi language" debugging experience. You get a single unified stack display, breakpoints, the works. There's a caveat or two around exceptions - that's about it. Everything just works.
C#/C++ isn't quite as solid, but it's still ahead of Python/C++ IME. Namely, Visual Studio will again show you a single unified callstack. There are more warts - again involving exceptions, as well as crash dumps and after the fact debugging. I've occasionally needed to break out WinDbg to deal with this.
Python/C++ is a bit harder. If you don't have a debug engine that can unify callstacks, however, there's still stuff you can do - dumping out both C++ and Python callstacks (and possibly locals) programmatically on assertions and in crash handlers, for example. I've done this for Java/C++, ActionScript/C++, Squirrel/C++, and probably more that I've forgotten.
"Own scripting language"/C++ is harder still: you must build your own everything. Debug engines are big projects - I haven't even completed my standalone one for brainfuck. If you dump your callstacks, you not only have to use the APIs that let you do so, but write them in the first place.
I dabbled with the Daydream Google VR NDK C++ sample app and did a demo app for myself of simple shapes moving around. While I could do simple things, I couldn't imagine doing the type of games others have done on the platform (or Oculus, or Vive) without Unity. For most people the question will be, why reinvent the wheel?
I don't know if C# will be the lingua franca of modern game engines, but for the foreseeable future, I think it will be the lingua franca of VR.
Valve's The Lab plumped for Unity because of the state of Unreal's forward renderer at the time but they wrote their own renderer. This was fair while back and both engines have moved on since then.
I went and looked up all the major game engines I could think of:
Blender: Python only
CryEngine: Lua and C#
Gamebryo: C++ only
Unity: C# and Java (Boo is no longer supported)
Unreal: C++ only (UnrealScript no longer supported)
Amazon Lumberyard: Lua (maybe C++ too?)
id Tech: C++
I was actually surprised at how common Lua was, but now that I think about it I have heard of it being used increasingly in games.
We are talking about the game scripting language, the game engine is always coded in C++ for triple-A games, with only a handful outliers. It's different for indies. We all know how the "Managed DirectX" and the incompatibile successor XNA turned out, the C# DirectX community is dead, end of life and last supported on the old XBox360, dead like Silverlight https://en.m.wikipedia.org/wiki/Microsoft_XNA .
http://news.xbox.com/2016/03/14/letter-chris-charla-idxbox-u...
Who would be so stupid and learn-resistant to use C# a third time for games?? Well there are those sheeps, that believes and do everything for their PR department.
http://news.xbox.com/2016/03/14/letter-chris-charla-idxbox-u...
XNA was a victim of the usual internal wars between Windows Dev and DevTools R&D units, about which way to build software (.NET vs C++).
Hence why they are so supportive of Unity nowadays, which is the main way to develop HoloLens applications.
Unfortunately. It's a decent language, but it just seems a bit too heavy to use as a scripting language, but not as good as C++ for heavy lifting.
I'm doing some stuff on Godot, and will probably just end up using GDScript for basic stuff, and GDNative for more intensive stuff (It's a C/C++ API for shared libraries that you can load up through the editor).
> but it just seems a bit too heavy to use as a scripting language, but not as good as C or Pascal for heavy lifting.
Are two variations of that statement I have heard among games developers since I am in computing.
But wouldn't expect that in under 3-5 years. Just an observer from the sidelines.
You can see an example of troubleshooting lock hitches using a 3rd party tool to get an idea of the complexity. https://www.youtube.com/watch?v=RE04LQffZfs
I recommend running the Roslyn Clr Heap Allocation Analyzer extension for VS for a few days - it helps to learn which language features and APIs implicitly allocate objects (or box value type objects).
Plus, modern GC implementations are super clever and efficient (as opposed to the implementation used by Unity, which unfortunately rather sucks, but I hear they're working on swapping it out).
If the game is relatively simple, and the heap relatively small, it's not a big problem. But if you're trying to make a large, complex simulation that's actually pushing boundaries, the GC becomes a constant adversary. People always talk about allocations, but not allocating during a frame is just the minimum price of admission. The real trouble is the static characteristics of the heap, like size and graph complexity. With manual alloc, at least the problem is straightforward: don't leak, and don't dangle. With a black-box GC, where you can't even hint to it about the lifetimes of objects, you have to be much, much more aware of memory management, then go through a lot of different non-idiomatic contortions regarding every single thing you put on the heap.
The question: "Why is this interesting to me." Is entirely different and we cannot possibly start to answer that for you.
We can give you the executive summary though. This brings access to UE4 to people without c++ knowledge. You can compare it to any managed wrapper around an unmanaged library. Some will curse it, others love it.
They had serious reason to not provide any official scripting support in UE4. Basically there no single language and runtime that would work well across all platforms: between Python, Node.js, Mono none of them work on every platform Epic target. Some don't have good support for consoles and other for mobile platforms and maintaining fork is hard even for Epic. So instead they try to extend blueprints instead since for them there are no such problems with C++.