Calling C# object method from IronPython
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 I开发者_StackOverflowronRuby 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.
I've just had this... you don't publish all your code but I'd guess that SayHello is public which is correct, however the class containing the SayHello function also needs to be public so that Python can see it.
It may have been because you did not declare your class "test" to be "public" to make it visible to IronPython.
精彩评论