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#.
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
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 () {
}
}
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.
All LUA libraries use LUA51.dll behind the scenes.
You can use them but:
only on windows standalone (potentially osx standalone too given you find a lua51.bundle)
might potentially require pro
you are not able to fully unleash it as you can’t hook up into the lower level of unity
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.
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
“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!
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.