Skip to content
Lukas Joergensen edited this page Dec 12, 2020 · 1 revision

Why C#?

A common misconception is that the purpose of C# as a scripting language is performance. This is far from the case. C# should be notably faster, but your scripts shouldn't really do the heavy lifting. If you find yourself gaining major performance boost by changing scripting languages, it may be an indication that your scripts are doing too much heavy lifting and you probably want to move that over to C++.

Even disregarding comparisons to TorqueScript, there is a 70-90 instruction overhead on all method calls from C# to C++, and calls from C++ to C# are still stringly typed and still suffer from marshalling strings to other types.

The real reasons for using C# are:

  1. Tooling, you get editors, debuggers etc.
  2. Library, you can pull functionality down 'off the shelves' for writing PDF's, making REST calls etc.
  3. Familiarity for newcomers, to me this is a minor thing but it could draw in new audiences for Torque3D.

But can C# run cross-platform?

Yes, C# now runs on most platforms. Even Android / IOS, .NET 5 consolidates the different technologies under Microsoft's wings: Mono, .NET Core and .NET Framework. As a result, it should be more and more "sta

See this blog post for more information about Microsoft's efforts in this area.

Even if that doesn't pan out, you can still compile C# to native, so it should be theoretically possible to bundle the .NET runtime and run it on Android (as it's just Linux) and possible IOS as well.

Is it typed then?

This one is complicated. Calling from C++ to C# is very different than C# to C++ currently. When C# calls C++, it is typed and it calls, more or less, directly into the engine. (There is a trampoline, but I'm not gonna cover that here).

However, when C++ calls C#, it still believes it's talking to TorqueScript, so it marshals everything to strings and sends it through the Console. C# has hooks in the Console that allows it to handle these function calls before TorqueScript gets the chance.

Does it talk to TorqueScript?

Yes. If you want to talk to TorqueScript from C#, you would need to use the Call method as if you were calling TorqueScript from C++. TorqueScript can talk to C# similarly, it might even be able to call it directly but I haven't tested that too deeply.

Is it really a scripting language?

Well.. Depending on your definition.. It still requires compilation, and hot-swapping DLL's is not currently supported, so it's more of a middle-ground between C++ and TorqueScript. I'm hoping to add hot-swapping or looking into something like ScriptCS but I haven't had time for that yet. Getting a functional interface to the engine was the immediate priority.