Skip to main content
gamedev.net
🔒 Locked

Using C# as a scripting language

Started by Cipher3D Oct 31, 2004 at 3:24 PM 9 replies 1.9k views
Original Post
Cipher3D
Cipher3D
I'm creating a C# scripting engine for my game, but how do I expose my game's classes to the scripts? For example, how would I have my scripts call a function within the game?
I eat heart attacks
nagromo
nagromo
What language are you using for the game itself? If it's in C/C++ you can just import functions from a .dll with something like:

[DllImport("kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long Frequency);

You can google for more info.
Cipher3D
Cipher3D
I'm using C#, and I'm dynamically compiling C# scripts for my game...

Within my C# scripts I need to call a function. For example:

Game code:

public class MyClass{     void MovePlayer(int distance);     void Loop()     {         ExecuteScript("Thisscript.script");     }}


Script code:

void Script(){     MyClass.MovePlayer(3);}
I eat heart attacks
Cipher3D
Cipher3D
Boo.
I eat heart attacks
coldacid
coldacid
You have to pass the assemblies containing the classes to be exported for scripting to the CS compiler. The flag for the MS compiler is: /r:

I'd strongly set against this idea, however. Even with C# there are security implications by doing this. It's possible for someone to write a script for your game that could possibly do things such as completely remove a person's HK_CURRENT_USER key tree in the registry, especially if you aren't careful with your assembly references. I'd go, if I were you, with a more traditional scripting language such as Python or Lua, or even SmallC.
evolutional
evolutional
I started a thread on .NET scripting. Got some really sweet replies too - might be worth an eyeball.
nuvem
nuvem
Quote:
Original post by Cipher3D
Boo.

I agree, Boo would be a nicer .Net lang to use for scripting
Washu
Washu
Well, two things really.
First: You will either need to use Managed C++/COM to expose your game functionality. Or you can use C function wrappers.
Secondly:
Quote:
I'd strongly set against this idea, however. Even with C# there are security implications by doing this. It's possible for someone to write a script for your game that could possibly do things such as completely remove a person's HK_CURRENT_USER key tree in the registry, especially if you aren't careful with your assembly references. I'd go, if I were you, with a more traditional scripting language such as Python or Lua, or even SmallC.

I can do ALL of those same things in all 3 of the languages you mentioned. If he's using C# he can setup an AppDomain that allows only certain assemblies to be loaded, along with security settings to disallow access to pretty much anything you don't want exposed. You can't do that in Python, Lua, or SmallC.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Cipher3D
Cipher3D
Well, I want my scripts to access certain methods/classes in the host that's calling the scripts. I have a seperate AppDomain set up for it, and I can run scripts, except that they can't access any of the classes in the host executable...how would I do that?
I eat heart attacks
evolutional
evolutional
When I did it, I added a referenced assembly to the CompilerParameters of the compiler created by the code provider. I eventually abandoned the project because once you have the assembly created, you can't easily get rid of it.

The next thing I'm looking at is VSA (Visual Studio for Applications), basically it seems like an the .NET version of VBA. I've not used it too much at present as I went onto use plugins (because this is a work project). But when I get the time, I'll go back to VSA because it seems to be where it's at, yo.
JanusFury
JanusFury
I just wanted to point out that .NET 2.0 will allow dynamic generation of methods with something called LCG, and allow you to garbage collect generated methods when you're done with them. That provides a semi-fix for the garbage collection issue with assemblies that was mentioned earlier.

Also, it is supposedly possible to load an assembly in its own AppDomain and then get rid of the custom appdomain to get rid of it when you're done, but I'm not sure how well that would work.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.