views:

196

answers:

2

I'm trying to write a scripting engine to my C#/XNA game using IronPython and came across a problem.

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:

CCamera camera = new CFPPCamera(this);

CCamera is an abstract class and CFPPCamera inhertits from it. CFPPCamera has the following constructor:

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'.'

If I don't pass the Camera generation to the script, everything works fine.

RenderingStreamTesting and GFXEngine are two projects in the same solution, with RenderingStreamTesting being the base game project that GFXEngine references.

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.

What am I missing?

+1  A: 

Might the IronPython script end up being compiled as "Any CPU" IL (perhaps if your running this on a x64 OS)?

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.

I'm unsure though whether it is possible to control the platform setting in an IronPython project.

Peter Lillevold
I've been burned by different projects having different build versions. Whether or not this solves your problem I can't be sure. XNA is a strong point but my IronPython experience is limited.
Finglas
+2  A: 

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.

Dino Viehland