December 31, 2004

Java or C#?

Now that I have successfully implemented prototype translators from both Java and C# to C++ (see my previous post), I have some thoughts on what would make a good language to use in combination with C++ for higher level game code. I am not proposing the replacement of C++. C++ is an excellent language for all kinds of games related problems.

Rather, I am proposing using reflection oriented language like C# where it is clearly beneficial to do so. The clear win is the ability of an alternative language to integrate more cleanly with the asset pipeline than is possible with C++. I have also considered the idea of using scripting languages. I am going to eliminate scripting languages from my shortlist. Not because they don't have a place in games development, quite the opposite, but because they cannot be used in all the cases where there is a need for a clean binding between game code and game assets.

I want a language that can fill the role that UnrealScript fills in Unreal titles, but in a console friendly way.

I am left with Java and C# on my shortlist. What other possibilities are there? I want a high performance statically-typed language that can compete head-to-head with C++ in most areas of game development. I envision maybe 50% of the code being written in C++, with the rest being written partially in Java or C# and partially in a scripting language like Lua or Python.

Lisp is an option. Naughty Dog seemed to use it effectively for a number of projects before loosing their Lisp guru. They are also searching for an alternative to C++. I have a broad understanding of Lisp but I have never written any program of significant size in it so I am hardly an expert. But I think this is a good point. Few games programmers have anything but a very basic understanding of Lisp. Why rock the boat? The key role I am trying to fill is for a language that can bridge the gap between code and assets. There is no need to switch to a whole new programming paradigm. Java and C# are ideal because, when used in the way that I envision, they will be like C++ with reflection. An experienced C++ will become an expert in either of these languages in a matter of weeks.

With respect to the role I want it to fill, Java has a killer flaw. It does not allow objects to be allocated on the stack or embedded within other objects. This would not be a problem for a scripting language. But it means that Java cannot go head-to-head with C++ in the areas where I need it. The memory management overhead would be too much. Compare:

struct GameObject1

{
Matrix LocalToWorld;
Vector Velocity;
Vector Acceleration;
};

struct GameObject2
{
Matrix *LocalToWorld; // all allocated separately on the heap
Vector *Velocity;
Vector *Acceleration;
};

Another significant failing in Java is its inability to use anything other than pointer types as template parameters. You can't do this:

template <typename T>

class Vector
{
size_t size;
T* array;
// ...
};

void foo()
{
Vector<int> intArray;
}

The best you can do in Java is a vector of pointers to ints, where each int is allocated on the heap separately. Massive overhead. C# has neither of these flaws.

Having taken garbage collection out of C#, are there any remaining reasons why it should not perform as well as C++? That makes a good subject for a future post...


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?