Calling C# object method from IronPython

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-03-17T00:05:54Z Indexed on 2010/03/17 0:11 UTC
Read the original article Hit count: 1109

Filed under:
|
|

I'm trying to embed a scripting engine in my game. Since I'm writing it in C#, I figured IronPython would be a great fit, but the examples I've been able to find all focus on calling IronPython methods in C# instead of C# methods in IronPython scripts.

To complicate things, I'm using Visual Studio 2010 RC1 on Windows 7 64 bit.

IronRuby works like I expect it would, but I'm not very familiar with Ruby or Python syntax.

What I'm doing:

        ScriptEngine engine = Python.CreateEngine();
        ScriptScope scope = engine.CreateScope();

        //Test class with a method that prints to the screen.
        scope.SetVariable("test", this); 

        ScriptSource source = 
          engine.CreateScriptSourceFromString("test.SayHello()", Microsoft.Scripting.SourceCodeKind.Statements);

        source.Execute(scope);

This generates an error, "'TestClass' object has no attribute 'SayHello'"

This exact set up works fine with IronRuby though using "self.test.SayHello()"

I'm wary using IronRuby though because it doesn't appear as mature as IronPython. If it's close enough, I might go with that though.

Any ideas? I know this has to be something simple.

© Stack Overflow or respective owner

Related posts about ironpython

Related posts about ironruby