Skip to content
Cody Tilkins edited this page Jun 25, 2022 · 3 revisions

Welcome to the Lua.NET wiki!

This page is going to explain the theory of using Lua.NET.

Preparing to use Lua

  1. Your first step is to decide on the version of Lua you are going to use.
  2. You will then compile that version of Lua to a dll.
  3. DLL naming matters.

Use any version supported. Non-latest patch versions are not supported yet. For example Lua5.1.5 is, but Lua5.1.4 is not, however it may still work. Because DLL function import attributes require a constant to feed into the directive, naming convention for Lua DLLs is in the format of Lua515.dll. Simply, "Lua" + LUA_VERSION_NUM + ".dll". The exception is LuaJIT, which outputs Lua51.dll when built. Simply add your Lua DLL in your source tree and dotnet should take care of the rest.

Using Assembly features

using static

Each Lua major and minor version supported has its own namespace. Lua51, Lua52, Lua53, LuaJIT. Inside is a Lua class which holds all constants and functions. You can always do Lua51.Lua.lua_pcall(...), however, using static {namespace}.{class}; floods your namespace with everything static and const found in a class. Using this with Lua.NET is probably very desired, as its more readable and a lot less typing.

using static Lua51.Lua;

class Program
{
  public static void Main(string[] args)
  {
    IntPtr L = luaL_newstate();
  }
}

using name = type

Because IntPtr is a type that doesn't allow polymorphism, you may rename it by including using lua_State = IntPtr; This will cut down confusion. Assemblies have a limitation where this form of 'typedef' isn't propagated, so this is making the best of what we have. While this works for reading the code, in VSCode the IDE will not pick up on this and will still show you IntPtr.

using static Lua51.Lua;

using lua_State = IntPtr;

class Program
{
  public static void Main(string[] args)
  {
    lua_State L = luaL_newstate();
  }
}
Clone this wiki locally