Calling C# object method from IronPython
- by Jason
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.