如何查看我的 IronPython 脚本正在生成哪些异常?



我正在尝试在Unity 3D项目中嵌入IronPython,但我遇到了一些我似乎无法弄清楚的错误。

我已将以下脚本附加到 Unity 中的游戏对象。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using IronPython;
using IronPython.Modules;
using System.Text;
public class IronPythonWrapper : MonoBehaviour {
void Start() {
ScriptTest();
}
public static void ScriptTest() {
// create the engine
var engine = IronPython.Hosting.Python.CreateEngine();
// and the scope (i.e. the Python namespace)
var scope = engine.CreateScope();
// execute the Python script
engine.ExecuteFile("Assets/Scripts/ipscript.py");
// grab the variable from the Python scope
string commands = scope.GetVariable<string>("commands");
Debug.Log(commands);
}
}

目标是让上面实例化的IronPython引擎执行以下简单的Python脚本,称为ipscript.py

commands = "Script executed."

当我进入播放模式(在 Unity 中(时,出现以下错误。

MissingMemberException: 'ScopeStorage' object has no attribute 'commands'

有没有人知道我应该调查什么来解决这个问题? 也许我需要实现一些错误捕获,以便我可以看到 IronPython 在执行 Python 脚本时生成的任何异常?

你的 python 执行没有正确传递范围。因此,您的commands变量是在执行 python 文件后丢弃的瞬态作用域上创建的,显式创建的作用域不知道commands是什么。

该代码段应如下所示

engine.ExecuteFile("Assets/Scripts/ipscript.py", scope);

相关内容

最新更新