C# and Lua
I created an application that uses the Mono implementation of LuaInterface, and it works beautifully without any hitches on Linux. I can't get LuaInterface to work with the Windows version of my application at all. I've spent two solid days trying to get this to work and I feel like I'm missing something really basic.
The 'latest' LuaInterface is compiled against an earlier version of .NET and won't work with 4.0. I turned up a single link to a .dll compiled against 4.0, but the link is dead. I tried to grab the LuaInterface source and compile it against 4.0 myself, but Visual Studio 2010 failed to open the solution because the project types were not supported (or some error to that effect).
I've tried MonoLuaInterface for the Windows version; although the author says that it works on Windows it plainly does not. The error I keep running into is that no matter what I do, I can't get luanet.dll to be detected by the program. I've tried putting it into the same directory as the executable, I've tried setting LUA_PATH
and LUA_CPATH
at start up, and no matter what happens it just throws at the first mention of the luanet.dll. I can't even get the sample projects to build; they can't find luanet.dll and are just dead in the water.
I tried NLua as well, and that was just a nightmare.
So my question is: does anyone know of any other (working) implementations of Lua on .NET? I'll rewrite most of the glue code if I have to, I just need to get this working. Alternatively, can anyone point me toward some solution for the luanet.dll issue, or even an answer to the LuaInterface source issue?
I know this is a really broad question, but I'm completely lost and have absolutely no idea where to even start trying to solve this. Thanks in advance to anyone with any ideas.
Okay, I've made some progress. I tried NLua again, and remembered what its problem was. After a little bit of experimenting, here's what I found out.
The Lua scripts I use interface with the rest of the program through an imported type, called the EventManager, which is loaded at the start of the relevant script. The EventManager is a singleton, and right away I assign the instance using the code:
EVENTMANAGER = luanet.import_type("Program.EventManager")
Event = EVENTMANAGER.Instance
From that point on in the code I call the exposed methods using Event:whateverfunction(). This actually works fine if I call the code using NLua's DoString() method. The problem comes in when I try to call those methods within a coroutine on the Lua side, which is how everything is actually written. When I do this, NLua throws a NullReferenceException. Stack trace is as follows:
System.NullReferenceException at NLua.MetaFunctions.GetMethod(LuaState luaState)
at KeraLua.NativeMethods.LuaNetPCall(IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc)
at KeraLua.Lua.LuaNetPCall(IntPtr luaState, Int32 nArgs, Int32 nResults, Int32 errfunc)
at NLua.LuaLib.LuaPCall(LuaState luaState, Int32 nArgs, Int32 nResults, Int32 errfunc)
at NLua.Lua.DoString(String chunk, String chunkName)
So I'm closer to figuring this out, but I still don't know where to go about fixing this.
I dug up through my discussions on #mono, and I came across this gem.
https://github.com/cdhowie/Eluant
Disclaimer: I'm the author.
You can have a look at MoonSharp (http://www.moonsharp.org). It's a Lua interpreter written entirely in C# for .NET 3.5 and later (works on Mono and Unity), and it includes a remote debugger which can be used to debug the scripts embedded in your program through a web browser (so it works also with full screen games and windows services).
You can still use LuaInterface in .NET 4 even if its compiled to 3.5.
Just add useLegacyV2RuntimeActivationPolicy="true"
in your app.config:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
链接地址: http://www.djcxy.com/p/16006.html
下一篇: C#和Lua