I'm trying to write a scripting engine to my C#/XNA game using IronPython and came across a problem.
我正在尝试使用IronPython为我的C#/ XNA游戏编写脚本引擎,并遇到了一个问题。
public class Game1 : Game, IGFXEnabledGame
{
//stuff
}
In the Game1
constructor I do necessary initialization for script and then run it to create the Camera
object. I try to move the following hard-coded Camera initialization to a script:
在Game1构造函数中,我对脚本进行必要的初始化,然后运行它来创建Camera对象。我尝试将以下硬编码的Camera初始化移动到脚本:
CCamera camera = new CFPPCamera(this);
CCamera
is an abstract class and CFPPCamera
inhertits from it. CFPPCamera
has the following constructor:
CCamera是一个抽象类,CFPPCamera来自它。 CFPPCamera具有以下构造函数:
public CFPPCamera(Game game)
: base(game)
{
}
Script initialization:
脚本初始化:
InitScriptPath = "InitScript.py";
rootDir = AppDomain.CurrentDomain.BaseDirectory;
scriptDir = Path.Combine(rootDir, "Scripts");
scriptEngine = Python.CreateEngine();
scriptRuntime = scriptEngine.Runtime;
scriptScope = scriptRuntime.CreateScope();
scriptSource = scriptEngine.CreateScriptSourceFromFile(Path.Combine(scriptDir, InitScriptPath));
scriptScope.SetVariable("this", this);
scriptSource.Execute(scriptScope);
Script code:
脚本代码:
import clr
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
clr.AddReferenceToFile("../../../../../GFXEngine/bin/x86/Debug/GFXEngine.dll")
import Microsoft.Xna.Framework
from GFXEngine.GFX.Camera import *
from GFXEngine.GFX import *
camera = CFPPCamera(this);
The code compiles without warnings or errors, however on script execution I get an error:
代码编译时没有警告或错误,但是在脚本执行时我收到错误:
Unable to cast object of type 'RenderingStreamTesting.Game1' to type GFXEngine.GFX.IGFXEnabledGame'.
'无法将“RenderingStreamTesting.Game1”类型的对象强制转换为GFXEngine.GFX.IGFXEnabledGame'。'
If I don't pass the Camera generation to the script, everything works fine.
如果我没有将Camera生成传递给脚本,一切正常。
RenderingStreamTesting
and GFXEngine
are two projects in the same solution, with RenderingStreamTesting
being the base game project that GFXEngine
references.
RenderingStreamTesting和GFXEngine是同一解决方案中的两个项目,RenderingStreamTesting是GFXEngine引用的基础游戏项目。
From what I found the problem might be caused by referencing different assemblies. I double checked and all references are made to native .NET .dlls and all third party libraries are listed in common directory.
从我发现的问题可能是由引用不同的程序集引起的。我仔细检查了所有引用都是对原生.NET .dll进行的,所有第三方库都列在了公共目录中。
What am I missing?
我错过了什么?
2 个解决方案
#1
3
It's probably the AddReferenceToFile call. Most likely you're ending up with the same assembly loaded into multiple loader contexts. If XNA is in the GAC I would recommend putting GFXEngine in the GAC or putting it in the same directory as the entry point EXE and use AddReference. That way everything should get loaded into the same context.
它可能是AddReferenceToFile调用。最有可能的是,你最终将相同的程序集加载到多个加载器上下文中。如果XNA在GAC中,我建议将GFXEngine放在GAC中,或者将其放在与入口点EXE相同的目录中,并使用AddReference。这样一切都应该加载到相同的上下文中。
#2
1
Might the IronPython script end up being compiled as "Any CPU" IL (perhaps if your running this on a x64 OS)?
可能IronPython脚本最终被编译为“任何CPU”IL(也许如果你在x64操作系统上运行它)?
I've experienced problems with this where my app assembly was set to target the "Any CPU" platform while my XNA library targets x86. Changing the platform on the app assembly to x86 also solved the problem.
我遇到了这方面的问题,我的app组件设置为以“任何CPU”平台为目标,而我的XNA库以x86为目标。将app程序集上的平台更改为x86也解决了这个问题。
I'm unsure though whether it is possible to control the platform setting in an IronPython project.
我不确定是否可以控制IronPython项目中的平台设置。
#1
3
It's probably the AddReferenceToFile call. Most likely you're ending up with the same assembly loaded into multiple loader contexts. If XNA is in the GAC I would recommend putting GFXEngine in the GAC or putting it in the same directory as the entry point EXE and use AddReference. That way everything should get loaded into the same context.
它可能是AddReferenceToFile调用。最有可能的是,你最终将相同的程序集加载到多个加载器上下文中。如果XNA在GAC中,我建议将GFXEngine放在GAC中,或者将其放在与入口点EXE相同的目录中,并使用AddReference。这样一切都应该加载到相同的上下文中。
#2
1
Might the IronPython script end up being compiled as "Any CPU" IL (perhaps if your running this on a x64 OS)?
可能IronPython脚本最终被编译为“任何CPU”IL(也许如果你在x64操作系统上运行它)?
I've experienced problems with this where my app assembly was set to target the "Any CPU" platform while my XNA library targets x86. Changing the platform on the app assembly to x86 also solved the problem.
我遇到了这方面的问题,我的app组件设置为以“任何CPU”平台为目标,而我的XNA库以x86为目标。将app程序集上的平台更改为x86也解决了这个问题。
I'm unsure though whether it is possible to control the platform setting in an IronPython project.
我不确定是否可以控制IronPython项目中的平台设置。