While having a beer with an old friend yesterday, we started discussing the use of scripting languages in applications. Having seen a number of .net applications with embedded Lua scripting engines, I suggested that he look at LuaInterface, with the disclaimer that I hadn’t used it much myself.
Having nothing better to do this afternoon, I decided to download the whole thing again (the last version I’d downloaded had languishing in my download folder for about 10 months, and was probably out of date, if I could find it in the first place) and have a go at it. Attached are the results of my messing around. It’s by no means a best practice guide – or even a good practice guide, for that matter – but it looks like it’s working.
Getting lua to work in .net is simple enough; add a reference to luainterface.dll and put the luanet and lua51 dlls somewhere accessible to your application. In my case I added them as content in my VS project, and set the copy mode to “Copy Always”.
To initialise a scripting context, create a new instance of LuaInterface.Lua, which is as simple as:
LuaInterface.Lua m_ScriptingEngine = new LuaInterface.Lua();
To expose a .net object to the scripting engine, use the engine’s indexer, like so:
ScriptingEngine[“winform”] = this;
this allows the scripts to access the object and it’s properties as follows:
winform.Text = “Title Goes Here”
The lua syntax to call a method from a script is <object>:<method>(parameters) –
winform:Close()
The scripts can also access and create .net types, such as forms and buttons. The assemblies and the types must be registered first, as follows:
— Reference the .Net assemblies.
luanet.load_assembly(“System.Windows.Forms”)
— Import the specific types.
Menu = luanet.import_type(“System.Windows.Forms.MainMenu”)
MenuItem = luanet.import_type(“System.Windows.Forms.MenuItem”)
I’ve uploaded my example project. Nothing groundbreaking, but it may be useful for anyone who’s still testing the waters on this.
Further reference
Hey there,
This article is really something …. Definitly I find it very useful… Very well done…
Thanks for posting!!!!!
Regards
Marlon
thanks for posting!!! it really helped a lot…
need how to call a lua function in c#…please post it…
Thank you for your comments 🙂
To call Lua functions from your C# application, you will need to get the function from the instance of the scripting engine and use its Call method i.e.
Lua instance = new Lua();
…
…
LuaFunction myFunction = instance.GetFunction(“myLuaFunction”);
myFunction.Call();
Hope this helps!
Thank you very much, it works like a charm.
Thanks Igor 🙂
I would like to get your example project working. Unfortunately it fails using Visual Studio 2012 on 64bit Windows 7. I already tried replacing LuaInterface with the current version but it did not help. Did you every try to compile it again under a newer version of Visual Studio?
LuaInterface seems to be really a great tool – unfortunately it seems nobody is using it and there is almost no documentation – so for me it is really hard to get started. Your example seems to be structured very clearly so I was hoping to use it as a starting point.
Thank you for your comment, Michael. Unfortunately I haven’t done any .Net development in some time, so can’t be of much help on this subject – have you tried asking on the Lua users mailing list? http://lua-users.org/
Ok, no Problem, thanks for your answer! I have ask already on the lua mailing list but now I think that it is more a .net problem than a lua problem. So I will probably have to ask in a .net forum.