Archive for the ‘lua’ Tag
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) -
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.
– 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.