170 likes | 185 Views
Lua Scripting in C#. How to Set Things Up. Download LuaInterface Source code is available to recompile if needed 51Net4 available from 3103 site (ready for .NET 4) Unzip lua51.dll and LuaInterface.dll Place these DLL files in your project Add them as Reference
E N D
How to Set Things Up • Download LuaInterface • Source code is available to recompile if needed • 51Net4 available from 3103 site (ready for .NET 4) • Unzip • lua51.dll and LuaInterface.dll • Place these DLL files in your project • Add them as Reference • Add “using LuaInterface;” atop code
The LuaInterface Object LuaInterface l = new Lua(); // executing a single command l.DoString("a=4"); //reading a variable Console.WriteLine(l["a"]); // finishing off l.Close();
What’s Happening C# Code Main l Heap (Memory) LuaInterface Instance (internal runtime data) a 4
LuaInterface (LI) is Persistent • Edit variables in script space • Executing commands • Writing to variables directly • Note the value remains (i.e., think of them as properties within LI)
Running a Script In File C# Code Main l Heap (Memory) Script File (on Disk) LuaInterface Instance (internal runtime data) a Lua Code a = 4 4 LuaInterface.Lua l = new Lua(); l.DoFile(@"Scripts\SimpleScript.lua"); l.Close();
Errors… Must Change Property…
Lua and NET Interaction Heap (Memory) C# Code Main Worker w LuaInterface Instance (internal runtime data) MainProgram a 4 Worker LuaInterface l string Data DisplaySomething() { … } Script File (on Disk) Lua Code a = 4 MainProgram.Data = “Hello World” MainProgram:DisplaySomething()
Accessing CLR/NET Objects • In Lua, we can access attributes using the dot (.) operator • MainProgram.Data = "Hello World" • We can also invoke methods using the colon (:) operator • MainProgram:DisplaySomething()
Handling Events • In C#, you can define new events • public delegate void SimpleEventHandler(); • public event EventHandler SimpleEvent; • Then you can raise the event • SimpleEvent(this, null); • Subscribing to the event enables handling • SimpleEvent += new SimpleEventHandler(DoFunction);
Handling Events in Lua function handle_event() a = a + 1 MainProgram:DisplayState() end function handle_event_different() a = a + 10 end MainProgram.SimpleEvent:Add(handle_event) MainProgram.SimpleEvent:Add(handle_event_different) a = 0
Testing theEvent Handler l = newLua(); l["MainProgram"] = this; l.DoFile(@"Scripts\SimpleScript.lua"); Timer t = newTimer(newTimerCallback(TimerFired), null, 0, 1000); Console.ReadLine(); l.Close(); privatevoid TimerFired(Object state) { SimpleEvent(this, null); } function handle_event() a = a + 1 MainProgram:DisplayState() end function handle_event_different() a = a + 10 end MainProgram.SimpleEvent:Add(handle_event) MainProgram.SimpleEvent:Add(handle_event_different) a = 0
Further Reading • http://luaforge.net/projects/luainterface/ • http://www.chipmunkav.com/support/guide.pdf • http://lua-users.org/wiki/LuaInterface • http://karlagius.com/2007/09/29/luainterface/ • http://penlight.luaforge.net/project-pages/penlight/packages/LuaInterface/ • http://www.godpatterns.com/2005/07/using-lua-scripting-for-games.html