Using Lua scripting in Unity

Hi there

Would it be possible to use scripts written in Lua scripting language in Unity?
I mean not directly, but as far as I know, there is this library called LuaInterface which allows you to use lua scripts in c#.

Link to LuaInterface: luaforge.net - luaforge Resources and Information.

So, would it be possible?

Thanks

As long as you compile it to an clr assembly and use it as a plug-in, it should work.

Let us know how that goes.

okay, I’ll give it a go :wink:

you mean compile the luaInterface library to clr assembly and use it as a plug-in in Unity?
If that then LuaInterface comes with a compiled .dll library file.

Or is it something else that you had in mind? I’m not too bright when it comes to .NET lol

Yes. That should do it.

Hm, it’s kinda weird.

The plugin works just fine and everything, but there are some weird things going on.

I’m trying the following code using LuaInterface documentation:

using UnityEngine;
using System.Collections;
using LuaInterface;

public class NewBehaviourScript : MonoBehaviour {

Lua lua = new Lua();
    
    void Start () {
        
lua.DoString("num = 2"); 
lua.DoString("str = ’a string’");
double num = (double)lua["num"];
string str = (string)lua["str"];
        
// Write to global variable ’str’
lua["str"] = "another string";
        
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

There are no errors, everything seems fine, but when I drag the script onto “Main Camera” or any other game object, Unity editor simply crashes…

I tried to see where the problem is, and it seems to be fine without crash with this:

using UnityEngine;
using System.Collections;
using LuaInterface;

public class NewBehaviourScript : MonoBehaviour {

    
    void Start () {
        
        
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

But as soon as I put Lua lua = new Lua(); in, it crashes:

using UnityEngine;
using System.Collections;
using LuaInterface;

public class NewBehaviourScript : MonoBehaviour {

	Lua lua = new Lua();
	
	void Start () {
		
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Any ideas? :shock:

Check the editor logs in \Users\USER\AppData\Local\Unity\Editor

Try compiling with pure clr and .net framework 2.0.

I’m guessing luainterface uses functionality missing in the Mono version used by Unity 2.6. Unity 3 will have an up-to-date Mono build, maybe that’ll help.

here’s what I found in the editor log:

Stacktrace:

  at LuaInterface.Lua..ctor () <0xffffffff>
  at LuaInterface.Lua..ctor () <0x0002e>
  at NewBehaviourScript.test () [0x00000] in C:\Users\boss\Documents\New Unity Project 2\Assets\NewBehaviourScript.cs:10
  at NewBehaviourScript.Start () [0x00000] in C:\Users\boss\Documents\New Unity Project 2\Assets\NewBehaviourScript.cs:16
  at (wrapper runtime-invoke) NewBehaviourScript.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff>
Receiving unhandled NULL exception
unity: Launch crash handler

Any ideas of a workaround? Should I give

a shot?

I was expecting luainterface to be a pure .net interpreter, but this is not the case. It seems to be wrapped around the native lua51.dll.

That said, I’m not sure this version of mono is up for the task.

ouch :cry:

So there’s no way at all of doing this then? Considering that there are versions of LuaInterface from 2004 onwards together with full source code?

edit:
I found several alternatives for LuaInterface which are compatible with Mono, let me try those…

Any update on this? Just sat down trying to implement LuaInterface in Unity 3 Pro, no real success.

All LUA libraries use LUA51.dll behind the scenes.

You can use them but:

  1. only on windows standalone (potentially osx standalone too given you find a lua51.bundle)
  2. might potentially require pro
  3. you are not able to fully unleash it as you can’t hook up into the lower level of unity
  4. you will have to find a way to get it to read the files from the right place (this was one of the larger problems I had in Unity 2.1 days and the way this stuff works didn’t change since then in any form)

LUA… hmm trying to remember, 4 years ago… some trick… I think I formatted that brain cell for some .Net junk…

Edit 1: ok so far so good, Macbook Pro, U3 … c# code

using UnityEngine;
using System;
using System.Collections;
using LuaInterface;

public class LUADEMO : MonoBehaviour {

	Lua l;
	// Use this for initialization
	void Start () {
		try
		{ 
			l=new Lua();
		}
		catch(Exception ex)
		{
			Debug.LogError(ex.ToString());
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

now on to phase 2…

Edit phase 2: not supported on the Mac

Edit phase 3: PC, Toshiba Laptop, doesn’t work either in U3

Apparently that interface library is not able to work with this version of mono.

Ok, after some fiddling, the good news, I got it working. The bad news, not with the latest version of LuaInterface, but with version 1.5.3, the last 1.x version.

Versions I’ve tried and failed to make it work: 2.0.1, 2.0.3
Versions I’ve tried and made it work: 1.2, 1.3.0, 1.4.0*, 1.5.3

*: the lua dlls supplied in 1.4.0 were not being recognized, so I simply used the lua dlls from 1.3.0 to make it work, but the LuaInterface code version 1.4.0 works

To make it work in the Unity Editor, the main lua dll plus an additional luanet.dll has to be somewhere Unity can find. In Windows, I copied them to the folder where Unity.exe resides.
Additionally, to make it work on standalone release builds, those two dlls have to be somewhere your .exe file can find. The easiest is to copy them to the same folder where the .exe file is. I assume for Mac standalones you have to do something similar.

The downloadable 1.x versions of LuaInterface also come with its source code bundled along, so you can use the source code version of LuaInterface if you don’t want it as a dll.

With the post here, I just realized having Lua is feasible for adding modability support for your Unity game.

486149–17063–$LuaInterface153Test.unitypackage (136 KB)

Great news!
I’ll try this when I have some free time, good job :slight_smile:

very neat <:

i tried out your test package and the LuaInterfaceTester worked just right but when i tried to register my own function with Lua and use it Unity would crash.
any suggestions would be appreciated

Try this: http://u3d.as/content/anomalous-underdog/unity-lua-interface-library/1AY

cool (:
thanks

Finally, I have found an Lua interpreter written entirely in C#.

https://github.com/nirvanai/AluminumLua

“AluminumLua does not use the DLR. Instead of generating an AST, the parser calls directly into an interface (IExecutor), that represents abstracted stack-based execution actions, such as Assignment or function Call.”

Know what that means folks? Yes, that means it will also work no problem with the restricted web player environment.

I have had to modify it slightly to read in remote files rather than local but it works great!

Horray!

why would you want to use lua, why not just learn unityscript or C# whats so special about lua

Lua can be written and executed dynamically ingame during runtime. Plus it’s great for some basic logic like open door when trigger X is activated. It’s not a replacement for C#, it’s an optional addition. With lua you can also introduce some modability to your games.