Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding scripting functionality to .NET applications

I have a little game written in C#. It uses a database as back-end. It's a trading card game, and I wanted to implement the function of the cards as a script.

What I mean is that I essentially have an interface, ICard, which a card class implements (public class Card056: ICard) and which contains a function that is called by the game.

Now, to make the thing maintainable/moddable, I would like to have the class for each card as source code in the database and essentially compile it on first use. So when I have to add/change a card, I'll just add it to the database and tell my application to refresh, without needing any assembly deployment (especially since we would be talking about 1 assembly per card which means hundreds of assemblies).

Is that possible? Register a class from a source file and then instantiate it, etc.

ICard Cards[current] = new MyGame.CardLibrary.Card056(); Cards[current].OnEnterPlay(ref currentGameState); 

The language is C# but extra bonus if it's possible to write the script in any .NET language.

like image 601
Michael Stum Avatar asked Aug 01 '08 23:08

Michael Stum


People also ask

Can you do scripting in C#?

C# scripting is a tool for testing out your C# and . NET snippets without the effort of creating multiple unit testing or console projects. It provides a lightweight option for quickly coding up a LINQ aggregate method call on the command line, checking the .

What is application scripting?

Application scripting allows users to control an application's behavior by writing scripts. For example, users might write scripts to automate routine tasks. They might use scripts to add new features or to customize existing functionalities.


2 Answers

Oleg Shilo's C# Script solution (at The Code Project) really is a great introduction to providing script abilities in your application.

A different approach would be to consider a language that is specifically built for scripting, such as IronRuby, IronPython, or Lua.

IronPython and IronRuby are both available today.

For a guide to embedding IronPython read How to embed IronPython script support in your existing app in 10 easy steps.

Lua is a scripting language commonly used in games. There is a Lua compiler for .NET, available from CodePlex -- http://www.codeplex.com/Nua

That codebase is a great read if you want to learn about building a compiler in .NET.

A different angle altogether is to try PowerShell. There are numerous examples of embedding PowerShell into an application -- here's a thorough project on the topic: Powershell Tunnel

like image 88
Leon Bambrick Avatar answered Oct 15 '22 16:10

Leon Bambrick


You might be able to use IronRuby for that.

Otherwise I'd suggest you have a directory where you place precompiled assemblies. Then you could have a reference in the DB to the assembly and class, and use reflection to load the proper assemblies at runtime.

If you really want to compile at run-time you could use the CodeDOM, then you could use reflection to load the dynamic assembly. Microsoft documentation article which might help.

like image 32
Eric Haskins Avatar answered Oct 15 '22 14:10

Eric Haskins