0

I want to implement C# as the scripting language in my game.

My problem is, that my script will not compile if I want to use classes defined in the game core (exe).

The script looks like this:

using System;
using ConsoleApplication1;

class Script
{
private static void Call()
{
    Console.WriteLine("called");
}

public static void Init()
{
    Console.WriteLine("Script");
    Call();
    GameObject myO; // THIS IS WHAT I WANT TO GET WORKED,
 //IF THIS IS COMMENTED OUT, IT COMPILES FINE, GAMEOBJECT
 // IS DEFINED IN THE "ConsoleApplication1" NAMESPACE.
}
}

The script is compiled like in the MDX sample:

    static void Main(string[] args)
    {
        CodeDomProvider provider = new CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();
        //cp.CompilerOptions = "/target:library";
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.IncludeDebugInformation = false;
        cp.ReferencedAssemblies.Add("ConsoleApplication2.exe");
        cp.ReferencedAssemblies.Add("System.dll");
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, "script.cs");
        if (!cr.Errors.HasErrors)
        {
            Console.WriteLine("Success");
            cr.CompiledAssembly.GetType("Script").GetMethod("Init").Invoke(null, null);
        }
        Console.ReadLine();
    }

Is there any way to call functions or create objects defined in the "ConsoleApplication1" namespace via the script?

3
  • 2
    Did I miss the part where you state what compile time error you encounter? Commented May 13, 2011 at 17:47
  • Oh I´m so dumb... checked compiler error[0] and now it works, thanks Commented May 13, 2011 at 18:00
  • Is 'ConsoleApplication1.GameObject' defined in 'ConsoleApplication2.exe'? Commented May 13, 2011 at 18:01

1 Answer 1

2

This is a daily programming problem. It's not working and you think it should be working. So break it down. Instead of working on a big problem, work on a smaller problem. Just tackle trying to compile the script outside of your program. Once you get that working, then you can try to compile it as a script from inside your program, knowing that you've got the basic problem of references and compiler issues sorted out.

Something like:

csc /reference:ConsoleApplication1.exe script.cs

From the looks of it, it might be as simple as changing the reference from ConsoleApplication2.exe to ConsoleApplication1.exe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.