Features - nrother/dynamiclua GitHub Wiki

Features

Here is a list of all features DynamicLua includes. Nearly every feature is explained with Code Snippets and the current implementation status. You should start by reading the QuickStartGuide if you're new to DynamicLua.

All these code snippets assume, that lua is an instance of the DynamicLua class like the following: (Read the QuickStartGuide for more details.)

dynamic lua = new DynamicLua.DynamicLua();

Complete Features

These features a completely developed and tested.

Lua Code Execution & Return Values

Just call a instance of the DynamicLua class like a function to excute code. You can save the return value in a variable of the corresponding type.

lua("print('hello world')"); //Prints "hello world"
double answer = lua("return 42");
dynamic primeNumbers = lua("return 3,5,7,11,13");
double first = primeNumbers[0]; //first = 3

Access to Lua variables

You can use an instance of the DynamicLua class like an array holding all global variables in Lua, or even access the variables as if they were members of this class.

lua("test='test'");
Console.WriteLine(lua.test); //Prints "test"
Console.WriteLine(lua["test"]); //dito

This works well for tables, and you can chain it!

lua("a={b={c=42}}");
Console.WriteLine(lua.a.b.c); //Prints 42

Access to Lua Tables

If you access a table from Lua you get a DynamicLuaTable-Object. You can access variables and call functions from this table like from the main Interpreter.

lua("tab={num=42}function tab.say(what) print(what) end");
dynamic tab = lua.tab;
Console.WriteLine(tab.num); //Prints 42
tab.say("hi"); //Prints "hi"

Call Lua Functions from C#

Calling Lua functions (buildin & user defined) is really easy: Just call them as if they were part of your DynamicLua instance.

lua("function say(what) print('I say '..what) end");
lua.say("hi!"); //Prints "I say hi!"

Call C# Functions from Lua

You can store C# Functions, wrapped in the correct delegate, in Lua Variables and call them from Lua as usual. You can even combine this with C#-Lambada Function. You might use the build-in delegates of .NET like Action<> and Func<> for wrapping the functions. Make sure to use the correct types in C# like double for lua number etc. Bug: When passing a table to a C# Method it is not correctly wrapped into an instance of DynamicLuaTable but as LuaTable. Refer to [workitem:438] for more Details.

static double Sqr(double number) { return number * number; }
lua.func1 = new Action<string>((what) => Console.WriteLine(what)); //With lambada
lua.func2 = new Func<double, double>(Sqr);

lua("func1('hello')"); //prints "hello"
lua("print(func2(42))"); //prints 1768

Lua Table Creation from C#

You can create a Lua Table from C# and access this tables instantly.

dynamic tab = lua.NewTable("tab");
tab.num = 42;

Assign Metatables from C#

To assign a Metatable to another table use the .SetMetatable(DynamicLuaTable)-Function like this:

dynamic tab = lua.NewTable("tab");
dynamic mt = lua.NewTable("mt");
tab.SetMetatable(mt);

Lua Metatable usage from C#

Lua Tables with associated Metatables can be used from C#. This feature has an extra page with a bigger code example. Please refer to this site for more Details:

In development Features

These features are currently developed and not complete yet. Some of them may work, some not.

Planned features

These features are still planned and are not working/implemented.

⚠️ **GitHub.com Fallback** ⚠️