3

我正在尝试在我的游戏中嵌入脚本引擎。因为我是用 C# 编写的,所以我认为 IronPython 非常适合,但是我能够找到的示例都集中在调用 IronPython 中的 C# 方法而不是 IronPython 脚本中的 C# 方法。

更复杂的是,我在 Windows 7 64 位上使用 Visual Studio 2010 RC1。

IronRuby 像我预期的那样工作,但我对 Ruby 或 Python 语法不是很熟悉。

我在做什么:

        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);

这会产生一个错误,“'TestClass' 对象没有属性 'SayHello'”

尽管使用“self.test.SayHello()”,但这个确切的设置在 IronRuby 上运行良好

不过,我对使用 IronRuby 持谨慎态度,因为它看起来不像 IronPython 那样成熟。如果它足够接近,我可能会去。

有任何想法吗?我知道这一定很简单。

4

2 回答 2

5

我刚刚有这个......你没有发布你所有的代码,但我猜 SayHello 是公共的,这是正确的,但是包含 SayHello 函数的类也需要是公共的,以便 Python 可以看到它。

于 2010-05-06T16:50:21.233 回答
1

这可能是因为您没有将您的类“test”声明为“public”以使其对 IronPython 可见。

于 2010-05-11T09:31:23.917 回答