C++ as Native Engine code and C# as Scripting

Started by
11 comments, last by Eul0gy 19 years, 12 months ago
Now, I use C# as a kind of scripting language and it works quite well. I use it for scripting within a game, where I load several scripts at once, compile them using the C# Compiler classes and invoke the code of the created assembly. Works fine for me. Furthermore I put the compiled assemblies in a separate AppDomain so I''m even able to unload the scripts, e.g. after a level.
If you have this kind of scripts (i.e. quite static), using the built-in compiler works perfectly.
If you need a more dynamic scripting language, i.e. reacting on user input, I''d suggest the System.Reflection.Emit classes using a dynamic assembly. I haven''t experimented very much with it, bit I''d guess they are faster for high-frequent script input as you can extend an existing assembly instead of always creating a new one.
The only Problem with Reflection.Emit is that you can only emit IL-statements. I would find it very cool if there was a C# to IL parser built in. However it isn''t - or I haven''t found it. What you can do is build your own small scripting language and a parser that can create IL-code and use System.Reflection.Emit to dynamically create runtime-usable types and methods. After all, .NET has a HUGE potential for scripting.

Regards,
Andre (VizOne) Loker
Andre Loker | Personal blog on .NET
Advertisement
hey Arild, you are right, but I hate being selectively quoted like that ... if you read my post it says ... IF the compiler is in the runtime (which it may be) then it should work ...

and it is, as you said, and it does, as you said ... but you definately made it appear that I made a statement I didin''t ... through context ... I said ... " cannot remember for sure if the C# compiler classes are part of the .NET runtime" .. and then said IF NOT it wouldn''t work
VizOne: are you sure you can extend existing assemblies using Emit? I really dont think so... afaik you have to create your own dynamic assembly, with dynamic types, etc.

I have been quite much looking for a scripting (i.e. dynamically typed and reloadable) language within .NET, yet I haven`t found any. The only one I found was "Python for .NET", which in fact is just a binding between the CPython and CLR. Not that it was bad, but it means you can not really create a managed class with the script, etc... There is a project of managed Python, called IronPython, but is in early stage of development.

But in case you are developing a C/C++ application and look for a scripting language, definitely embed Python.

This topic is closed to new replies.

Advertisement