Compilation Order question when using js eval()

Hi all,

I have a question regarding the Unity compilation order. I’ve read all of the information at Unity - Scripting API: about script compilation, and understand that. My problem is that I am using a dialogue engine written in js, where the rest of my game is written in C#.

All of the js code is contained in Standard Assets, so it will be compiled first. Then all of the C# code is contained in the Resources folder, so it is compiled after the js. In this way the C# can reference the js, but not the other way around.

My problem is that I need the dialogue engine, written in js, to be able to check game state variables that are in the C# part of the code. I was hoping to do this through the use of the eval() function, which allows you to execute code input in the form of a string. My thinking was that the eval call does not occur until run time, and so by that point both the js and C# should be compiled and so code in the eval should be able to reference either one.

I have verified that the eval is not called until runtime, however I still receive an error:

CompilationErrorsException: script(1,1): BCE0005: Boo.Lang.Compiler.CompilerError: Unknown identifier: ‘DramaDebugger’

The code being executed in the eval statement is:

DramaDebugger.Instance.LogMessage(“loggingscript”);

DramaDebugger is a C# singleton class I have defined. Does anyone have any suggestions or ideas as to why this is still throwing a compiler error? Because I know that DramaDebugger has already been compiled by the time the eval call occurs.

Sorry for the long post, and let me know if anything is unclear or you need more information. Thanks!

Unity compiles the code in different assemblies. When the eval is executed, it searches for a class named DramaDebugger but can’t find it, because it’s only defined on other assembly.

You could load this assembly in the JS code and call the method using reflection, but I don’t know a good way to manually load the assemblies generated by Unity.

Or you could pass a reference to the singleton instance, in the C# code, and use it int the JS. Eg:

JSClass.js


var dramaDebuggerinstance;

...

dramaDebuggerinstance.LogMessage("loggingscript");

and in the DramaDebugger.cs:

(FindObjectOfType(typeof(JSClass)) as JSClass).dramaDebuggerinstance = 
                                                         DramaDebugger.Instance;

Just take care to pass the reference for every object you will need before using it.