Home > C++, Programming > Using C# for game scripts (Part 1)

Using C# for game scripts (Part 1)

April 22, 2008

Alright, today I’m going to continue where my previous posts (Loading Map/Game Data from XML) left off. Those posts showed you how to get game data into your game, but how do you define game behavior? Simple! With scripts.

Now, there are lots of solutions for scripting available with one of the mose popular being Lua. However, I want this to be simple and easy, so we’re going to be using C#. I mean, what’s the point of doing all this setup that needs to be done with Lua when we have a perfectly working compiler and development environment sitting at our fingertips? =)

Ok, so first things first. When we are creating scripts that are going to be run, we need to have some time of entry point into the script; that is, we need to know how to execute the script. One simple way is to create and interface or a base class that defines the available methods that we can actually call on our script.

Continuing with the example of a text-based adventure, let’s figure out how we might allow for developers to create commands the user can type to interact with the game. All we need to do this will be the name of the command and an entry point into the script.


public abstract class Command {
   private string name;

   public string Name {
       get { return this.name; }
       set { this.name = value; }

   public virtual void Execute(Game game, string arguments) {

Now we have the base command in place, next we need a script that actually does something interesting. With this, you’re just going to have to bear with me as I won’t be posting the details of the Game class, but just assume the properties on it exist. =) Besides, this is more of a proof-of-concept.


public class MoveCommand : Command {
   public override void Execute(Game game, string direction) {
       direction = direction.ToLower();

       // Determine if the player can move the way they wanted to.
       Area currentArea = game.Map[game.Player.Position];
       if (currentArea.HasExit(direction)) {
           if (direction == "north") game.Player.Move(0, 1);
           else if (direction == "east") game.Player.Move(1, 0);
            else if (direction == "south") game.Player.Move(0, -1);
            else if (direction == "west") game.Player.Move(-1, 0);

            currentArea = game.Map[game.Player.Position];
        else {
            TextBuffer.AddTextLine("Invalid direction to move: " + direction);

It should be fairly obvious what this is script is supposed to do, after all it is called MoveCommand. 😉 Basically we are just going to move the player in one of the given directions with the Area that we are in has an exit/door in that direction.

We know have two-thirds of the puzzle. The next question to answer is, “How the heck do I get that into my game?”. The answer is much simpler than you might think:


// You need to define these somehow, maybe from a backing XML file. 😉
string scriptPath;
string scriptFileName;
string scriptTypeName;
string commandName;

// Create the actual type in memory so it can be used.
CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.CompilerOptions = "/target:library";
compilerParams.GenerateExecutable = false;
compilerParams.GenerateInMemory = true;
compilerParams.IncludeDebugInformation = false;


CompilerResults result = provider.CompileAssemblyFromFile(compilerParams, Path.Combine(scriptPath, scriptFileName));
if (!result.Errors.HasErrors) {
    Type type = result.CompiledAssembly.GetType(scriptTypeName);
    Command cmd = Activator.CreateInstance(type) as Command;
    if (cmd != null) {
        cmd.Name = commandName;
        this.availableCommands.Add(cmd.Name.ToLower(), cmd);

Simple huh? Heh, well… if you know what all those lines of code do then it is. Basically all we are doing is having the CodeDom compile that piece of code for us. This results in an assembly which we can then use reflection to retrieve an instance of the MoveCommand from this newly compiled assembly.

You might be wondering where you put this in your game code. Well, I put in my Initialize method which is one of the first methods that gets called. I actually wrap it in a LoadCommands method which goes through and compiles all of my game scripts, which are of course, defined using an XML file. =)

This approach works fine and dandy for script files that are independent of one another, but what if we want scripts to be able to use methods defines in other scripts? Well, for that we’ll need to compile all of the scripts into a single assembly. I’ll cover how to do that in my next post.


Categories: C++, Programming Tags:
%d bloggers like this: