5 ms·
As someone who has no idea about gamedev, I'm wondering about why gamedev in NET is not only possible, but encouraged, while being "nearly impossible" and disco
by self_awareness 4y ago
As someone who has no idea about gamedev, I'm wondering about why gamedev in NET is not only possible, but encouraged, while being "nearly impossible" and discouraged on JVM. I mean, both have garbage collection, both are JITed, both have nearly the same feature set.
Why is it that C# is that popular for gamedev, and only a few titles use JVM?
- peoplefromibiza 4y ago> Why is it that C# is that popular for gamedev Unity. edit: also C# allows precise measurement of memory usage and padding, while in Java one can only guess
- styx31 4y agoXNA?
- cesarb 4y ago> I mean, both have garbage collection, both are JITed, both have nearly the same feature set. There's one big difference in the feature set: unless things have changed very recently, the JVM lacks value objects.
- self_awareness 4y agoValue objects (and more) are a part of Project Valhalla: https://openjdk.org/projects/valhalla/ https://openjdk.org/projects/valhalla/, and Kotlin allows using them today, although with some hacky annotations.
- kroltan 4y agoThat is very true, but C# has had value types since its inception, so a lot of tooling exists to deal with them, both in the language and in third party code (libraries and engines)
- becquerel 4y agoC# has evolved in recent years to let you do fun stuff that sidesteps GC and JIT a lot of the time. There's proper AOT compilation now, mostly, and you can cut down on ref allocations massively if you know what you're doing.
- self_awareness 4y agoJVM also has off heap memory allocation to skip GC, and also has native AOT through GraalVM
- iliketrains 4y agoGame dev here. The reason is simple - performance. Unlike Java, C# has zero-overhead structs (custom primitive types if you will), so say a Vector3f is just 3 floats in memory, 12 bytes, no object overhead, no pointer indirection. This is a BIG deal. Array of vectors can be just raw data in C#, not an array of pointers like in Java. You can fairly easily map C# structs to C/C++ structs that GPU drivers need. C# even allows fixing struct fields to certain offsets to ensure identical memory layout with other languages. Other features are also critical to performance such as true generics. Say List<Vector3f> in C# is specialized for that type, no overhead from objects, no extra pointers, no casting. In C#, if you are very careful, you can get close to the performance of C++. I don't think the same can be said about Java, because the language does not give you the tools to achieve that.
- kroltan 4y agoYeah, Java's reply to performance was "let's make the GC better", C#'s was "let's make the GC optional". I don't think it was a strictly conscious decision (generics were reified long before XNA was even an idea), but for low-level game development it is imperative to have predictable memory usage and control.
- garganzol 4y ago.NET has the first class support of value types and generics. JVM is behind .NET in those aspects to varying degrees. Not as important, but still: C#, as a language, became pretty sophisticated during the recent years. It picked up quite a few functional idioms from OCaml, F#, Haskell and other functional languages, so you have pretty decent horsepower under your fingers. For instance, it can validate for possible null reference access during compile time now, thus nearly eliminating NRE (NullRefenceException) bugs in run time.
- self_awareness 4y agoTrue, C# is a nice language, but so is Kotlin. Java support for value types is being developed and has a dedicated JEP 401 [1]. Kotlin allows to use value types today, it also has nullable types. I'm not convinced if functional features are really a factor that decides what is popular or not; it can be attractive for hardcore programmers, but most of programmers are not that. [1]: https://openjdk.java.net/jeps/401 https://openjdk.java.net/jeps/401
- deleted 4y ago[deleted]
- rdw 4y agoIt has more to do with how game engines are built, making embeddability be the most important criteria. Most game engines are written as a large chunk of C++ code that runs each frame's timing, and the big subsystems like physics, particles, sound, and the scenegraph. All the important engineers will be dedicated to working on this engine and it behaves like a framework. The "game logic" is generally considered to be a minority of the code, and because less-technical people generally author it, it gets written in a higher-level "scripting" language. This creates some serious constraints on that language. It must be possible to call into the scripts from the main engine and the overhead must be low. The scripts often have to make calls into the data structures of the main engine (e.g. for physics queries) and the overhead of that should be low as well. It should also be possible to control the scripting language's GC because the main engine is pretty timing-sensitive. Memory consumption is pretty important as well. All these requirements point towards two implementations specifically: Lua (and LuaJIT), and Mono. Those two runtimes go out of their way to make it easy and fast to embed. A third option which a lot of engines pick is to write their own scripting language where they control everything about it. Any other language you can think of (with the possible exceptions of Haxe) will have some major hurdle that prevents easy embedding. The fact that you can compile multiple languages to Mono bytecode pushes some folks in that direction; if you're planning to write a lot of code in the scripting engine (not all of them do! See: Unreal) that's nice flexibility to have.
- kaba0 4y agoWhile the sibling comment regarding better control over memory layout is right, here we mostly talk about scripting a game engine where the performance critical part is largely inside the engine. So the reason is more likely simply “history”. But actually, C# was always meant as a “managed c++” in a way, while Java rather optimizes on the runtime level (which is helped by less control by the developer). Nonetheless, Java is more than fine for game development, besides the obvious Minecraft there are plenty small to big indie games written in Java.