Integrating Unity with Lua in Two Minutes

Why would Lua be helpful to a Unity project?

Modding, in a word. Lua is a powerful, fast and simple scripting language that can be embedded into applications to provide a sandbox environment for developers or ‘modders’ to interact with your core gameplay without having to repackage the entire game.

A practical example

You can find the demonstration project here - https://github.com/DGoodayle/UnityLuaIntegration

What’s inside?

Third-party DLLs that provide an interface for Mono C# and Lua – KopiLua and KopiLuaInterface. These two DLLs do 99% of the heavy lifting, the only bit you have to take care of is exposing functions to your new Lua sandbox.

The code I’ve provided is just a stripped down application that simply sets up a C# Binding, runs a Lua file which then tells the C# Binding about one of its functions. The application then calls that function within Unity and outputs a message sent from Lua.

I hope that this code can provide as some sort of base-plate for larger usage of Lua inside Unity.

Code

LuaManager.cs

using UnityEngine;
using System.Collections;
using LuaInterface;

public class LuaManager : MonoBehaviour {
	//Reference to the Lua virtual machine
	private Lua luaVirtualMachine;
	//Filename of the Lua file to load in the Streaming Assets folder
	public string LuaFileToLoad = "";
	public void Start(){
		//Init LuaBinding class that demonstrates communication
		LuaBinding binding = new LuaBinding();

		//Init instance of Lua virtual machine (Note: Can only init ONCE)
		luaVirtualMachine = new Lua();
		//Tell Lua about the LuaBinding object to allow Lua to call C# functions
		luaVirtualMachine["luabinding"] = binding;
		//Run the code contained within the file
		luaVirtualMachine.DoFile(Application.streamingAssetsPath+"/"+LuaFileToLoad);		
		//Trigger binding in c# to call the bound Lua function
		binding.MessageToLua();
	}
}LuaBinding.cs
using UnityEngine;
using System.Collections;
using LuaInterface; //Reference the LuaInterface DLL

public class LuaBinding  {
	//Reference to bound Lua function set within Lua
	public LuaFunction boundMessageFunction;
	public void BindMessageFunction(LuaFunction func){
		//Binding
		boundMessageFunction = func;	
	}
	public void MessageFromLua(string message){
		//Output message into the debug log
		Debug.Log(message);	
	}
	public void MessageToLua(){
		//Call the bound function with a string as its first param
		boundMessageFunction.Call("Hello");
	}
}

luademo.lua

 -- Simple function to call the function inside LuaBinding.cs to out a message
function WriteFromLua(text) 
	luabinding:MessageFromLua(text)
end
 -- Tell LuaBinding.cs to bind the WriteFromLua function to its LuaFunction variable
luabinding:BindMessageFunction(WriteFromLua)

Closing Notes

This of course does not mean your entire application or game should be written in Lua. In fact i’m not quite sure if that’d be practical due to Lua using Reflection to invoke C# methods at run-time, feel free to experiment and let me know though!

I'm a London based game developer who likes to play around with engines and learn new things.

Next ArticleUnity MonoBehaviours in C#