Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Using Lua scripting in Unity

Discussion in 'General Discussion' started by Deleted User, Jul 2, 2010.

  1. Deleted User

    Deleted User

    Guest

    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: http://luaforge.net/projects/luainterface/

    So, would it be possible?

    Thanks
     
  2. Diogo-Teixeira

    Diogo-Teixeira

    Joined:
    Apr 9, 2010
    Posts:
    494
    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.
     
  3. Deleted User

    Deleted User

    Guest

    okay, I'll give it a go ;)
    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
     
  4. Diogo-Teixeira

    Diogo-Teixeira

    Joined:
    Apr 9, 2010
    Posts:
    494
    Yes. That should do it.
     
  5. Deleted User

    Deleted User

    Guest

    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:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LuaInterface;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7. Lua lua = new Lua();
    8.    
    9.     void Start () {
    10.        
    11. lua.DoString("num = 2");
    12. lua.DoString("str = ’a string’");
    13. double num = (double)lua["num"];
    14. string str = (string)lua["str"];
    15.        
    16. // Write to global variable ’str’
    17. lua["str"] = "another string";
    18.        
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.    
    24.     }
    25. }
    26.  
    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:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LuaInterface;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7.    
    8.     void Start () {
    9.        
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    But as soon as I put Lua lua = new Lua(); in, it crashes:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LuaInterface;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7.     Lua lua = new Lua();
    8.    
    9.     void Start () {
    10.        
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18. }
    19.  
    Any ideas? :eek:
     
  6. Diogo-Teixeira

    Diogo-Teixeira

    Joined:
    Apr 9, 2010
    Posts:
    494
    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.
     
  7. Deleted User

    Deleted User

    Guest

    here's what I found in the editor log:
    Code (csharp):
    1. Stacktrace:
    2.  
    3.   at LuaInterface.Lua..ctor () <0xffffffff>
    4.   at LuaInterface.Lua..ctor () <0x0002e>
    5.   at NewBehaviourScript.test () [0x00000] in C:\Users\boss\Documents\New Unity Project 2\Assets\NewBehaviourScript.cs:10
    6.   at NewBehaviourScript.Start () [0x00000] in C:\Users\boss\Documents\New Unity Project 2\Assets\NewBehaviourScript.cs:16
    7.   at (wrapper runtime-invoke) NewBehaviourScript.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff>
    8. Receiving unhandled NULL exception
    9. unity: Launch crash handler
    Any ideas of a workaround? Should I give
    a shot?
     
  8. Diogo-Teixeira

    Diogo-Teixeira

    Joined:
    Apr 9, 2010
    Posts:
    494
    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.
     
  9. Deleted User

    Deleted User

    Guest

    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...
     
  10. mollstam

    mollstam

    Joined:
    Dec 29, 2010
    Posts:
    1
    Any update on this? Just sat down trying to implement LuaInterface in Unity 3 Pro, no real success.
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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)
     
  12. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    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
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using LuaInterface;
    6.  
    7. public class LUADEMO : MonoBehaviour {
    8.  
    9.     Lua l;
    10.     // Use this for initialization
    11.     void Start () {
    12.         try
    13.         {
    14.             l=new Lua();
    15.         }
    16.         catch(Exception ex)
    17.         {
    18.             Debug.LogError(ex.ToString());
    19.         }
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.    
    25.     }
    26. }
    27.  
    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.
     
    Last edited: Dec 29, 2010
  13. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,554
    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.
     

    Attached Files:

  14. Deleted User

    Deleted User

    Guest

    Great news!
    I'll try this when I have some free time, good job :)
     
  15. lunaDirus

    lunaDirus

    Joined:
    Jul 16, 2011
    Posts:
    11
    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
     
  16. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,554
  17. lunaDirus

    lunaDirus

    Joined:
    Jul 16, 2011
    Posts:
    11
    cool :)
    thanks
     
  18. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    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!
     
  19. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    why would you want to use lua, why not just learn unityscript or C# whats so special about lua
     
  20. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    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.
     
  21. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    Made me laugh.

    I guess modability could be cool, but lol at using a different language for "triggers" as an "advantage" over just using what you know to do it in the 1 second that it takes to make a trigger. :)
     
  22. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    Yes, you can even sometimes think of Lua scripts as a type of art asset rather than code because scripts are often the place where the very unique part of the game is expressed.

    Since C#, UnityScript and... boo are used in a very similar way to scripts by Unity you can easily get by without Lua but if you have an engine written in C/C++, then scripts are definitely the way to go. I like it because it allows me to change behaviour of the game by sending scripts through the internet via Xml.

    @Morning,
    I like your signature. It is very refreshing to see that I am not the only one who has concerns when it comes to DRM :)
     
  23. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    To each his own. Obviously door opening is basic, but for a bit more complex triggers I prefer scripting. Though not in unity because I am not aware of any stable scripting engine for unity.
    What scripting is really useful is debugging. I've been using python ingame console to debug stuff for a while now.
     
    EZaca likes this.
  24. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    Is Ontriggerenter not scripting? I mean you can check to see if the player is holding some secondary item to trigger a different set of events than if it's just the player standing in the trigger alone...in like 4 lines.
     
  25. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    Yes, but I need a lot of functions for each different trigger, it gets a bit cumbersome when your game has over a thousand of them. Having a thousand different lua files makes it easier to manage. Just my opinion, I'm not saying using C#/JS is bad.
     
  26. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    You do know that Mono is more than just a scripting engine, right?

    You can evaluate code expressions at runtime. This is a standard Mono / .NET feature, albeit one that's rarely used in Unity games. Possibly because many developers here forget that there is a full Mono implementation under the hood and assume the C#, Javascript and Boo support is somehow tied specifically to Unity itself.

    This is one of the dangers of using the term "scripting". That term is often used to imply that scripts are limited to the bounds of the application the scripts are written for, but Mono isn't just some glorified embedded script compiler. It's a full-fat application development platform in its own right.

    When it comes to Unity, scripting = programming; programming = scripting. There is no difference.
     
  27. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    That's interesting, I'll check it out.
    Maybe not that useful
     
    Last edited: Jun 12, 2012
  28. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    Mono.CSharp.Evaluator.Evaluate does not seem to be as useful as Lua for my needs. I cannot find a way to bind it to my game so that it can call external game functions. For this reason I dont think it is much use for scripting.

    Not to mention Lua is crazy portable so if this system is needed on another (non-.net) engine, scripts can simply be copied and pasted rather than translated into a new language. In other words, Lua interpreters will always be available to me whereas a full .net framework wont be.
     
  29. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    Any "Eval" function must, by definition, return something to the caller, so a statement on its own won't be sufficient. But take a look at the C# sample code in that link: statements can clearly be used. They just can't be all you use.

    Perhaps this would have been a better link. Basically, you can literally compile entire C# source files into assemblies if you wish. At runtime. Theoretically, the same base Mono classes should be available to every Mono-compatible language, so similar functionality may be available from Javascript and Boo too, though I haven't checked.

    Honestly, LUA doesn't do anything C#, Javascript or Boo cannot do just as well. Why nail on yet another scripting engine when Unity already has one that already does the job just fine and is also cross-platform? It just seems like reinventing the wheel.
     
  30. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    Have you actually gotten C# evaluator to work in unity? Just wondering.

    We get it, you don't like scripting over scripting. But what does it matter to you if others want or find it useful?
     
    Last edited: Jun 13, 2012
  31. Rico21745

    Rico21745

    Joined:
    Apr 25, 2012
    Posts:
    409
    I think he's trying to help you. Mono is already part of Unity, whereas Lua is not directly supported. To be honest his posts have been actually very informative to me, as I was unaware of this capability when it comes to Mono.
     
  32. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    I was unaware too and I'm not saying his comments are bad, but so far I haven't been able to get it to actually work. I've used JS's eval and it worked fine, but when it comes to C# it does not so far.
     
  33. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    Nope, but I've been casting about for a new tutorial to write. This could be a good fit. I'll have a play and see what comes up.

    No, but solicitation of opinions is implied by any post in a forum like this. I therefore offered my opinion and also asked why the OP wants to nail LUA onto an engine that already has a built-in scripting engine. I've not heard a good reason for doing so yet, but that doesn't mean there aren't any. I'm always willing to learn.
     
  34. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    Will be waiting for that.
     
  35. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    Since I revived this thread from 2010, I guess that gives me the new role of OP ;)

    A few reasons why I am currently happy with Lua:-

    1) The Mono.CSharp.Evaluator.Evaluate system is probably no more (or less) supported by Unity than Lua (it is simply a feature of the .net framework).

    2) The typical C#, US, Boo "scripts" used by Unity, are compiled in so therefore cannot be used for "runtime scripting"

    3) My issues with using the C# Evaluator stuff is that I have not yet confirmed it works on the webplayer under the very restricted (cut-down) version of the framework.

    4) There seems a lot of support towards Lua around the industry than using a C# runtime interpreter.

    5) Again, cannot confirm but Mono.CSharp.Evaluator.Evaluate probably wont work on the Microsoft version of .NET which unfortunately some of other coders use because of Visual Studio.
     
    Last edited: Jun 14, 2012
  36. returnString

    returnString

    Joined:
    Jul 10, 2010
    Posts:
    248
    .NET is pretty easy to reload at runtime using AppDomains. There's absolutely nada stopping you from writing a generic game in Unity itself and loading C# (or indeed any .NET language) from a folder at runtime. Really. Even with runtime reloading. It works for an engine that doesn't even support Mono by default, so it'll be trivial with Unity ;)
     
  37. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    Something tells me that the web player will not support this ;)

    It is against Apples "no compiler" app store policy too. (They do however allow Lua since 2010).

    Actually, this is fairly critical. Does anyone here even use a runtime scripting system for anything other than desktop (mac / win) applications? So far it seems like Lua really is the only choice.

    Afterall, one of the main reasons we are using Unity is so it targets the webplayer and iOS devices.
     
    Last edited: Jun 14, 2012
  38. returnString

    returnString

    Joined:
    Jul 10, 2010
    Posts:
    248
    I'm confused as to your use case frankly :p

    Do you want Lua so you can reload your scripts on the fly, or just because you like Lua? (if you answered with option 2, I have a list of great institutions you can check yourself into)
     
  39. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    We investigated various Javascript intepreters last year - JINT springs to mind - but didn't get them to work. Lua was easier. We were interested in scripting on Xbox 360, which would not support dynamic compilation.
     
  40. kpedersen

    kpedersen

    Joined:
    Oct 5, 2011
    Posts:
    9
    I want to be reading scripts in on the fly. I do not want to be re-loading .net IL assembilies as you suggested with the AppDomains.

    The issue still stands. We are using Unity so we can easily target the webplayer and iOS platforms. Since your solution can never work on these platforms I unfortunately need to be looking at something else.

    So far, AluminiumLua is the only interpreter with enough features that runs in the webplayer environment.
     
  41. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
  42. coder8

    coder8

    Joined:
    Jan 28, 2010
    Posts:
    6
    @kpedersen very nice find and thanks for the link. I have been looking for something like this for the past few weeks. For those that do not understand why you want something like this it would be hard to explain until you have tried it. I see there is also something similar for Python! All kinds of ideas now! A few points on why these are nice tools to have.

    More language integration means more people will try Unity (c# and unityscript are not my favorite languages) Boo is nice though.
    Lua and Python are widely used in the industry
    Access to their libraries.
    Skills are portable (they are good languages to know)
    Runtime execution
    Debugging
    Prototyping (fast feedback loop)
    Mods
    Unit Tests
    I can use it like a DSL (the main reason I want to use it) instead of having to make my own custom parser and lexer
    AluminumLua will work on all platforms
     
  43. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    Hmm. After a couple of days rooting through the Mono, Unity and MSDN documentation, it looks like the CodeDom stuff in C# under Mono is there, but it's a bugger to get it working on anything other than Windows. For some bizarre reason, Unity cannot locate the Mono compiler ("gmcs.exe") under OS X and, despite multiple attempts and tweaking of environment variables, I've had to throw in the towel on this.

    It may be viable—possibly—on OS X, given enough time, blood, sweat and tears, but life's too short and I need to move on to other (preferably paid!) projects.

    Also, support for this API is limited. It's not available on WebPlayer and mobile platforms as the API just isn't implemented for those.. Runtime code compilation using the CodeDom API is done by simply calling the relevant Mono compilers behind the scenes. These simply aren't installed on mobile platforms. Furthermore, a WebPlayer build wouldn't have any access to a Mono environment either as it's sandboxed.

    TL;DR: probably best to use a third-party solution instead. Sorry about that. Guess I did learn something from this thread after all! :)

    Boo does have some interesting features which might allow some scripting support, but I suspect the WebPlayer, Flash and mobile platforms still wouldn't support it. There's a reason why you don't tend to see a lot of mobile game mods.
     
  44. kersk

    kersk

    Joined:
    Jan 2, 2012
    Posts:
    56
    I was able to get Evaluate working in OSX by dropping the Mono.Csharp.dll directly into my project and using it like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class MonoEval : MonoBehaviour
    5. {
    6.     private string expression = "1+2;";
    7.  
    8.     void Start()
    9.     {
    10.         Debug.Log(Mono.CSharp.Evaluator.Evaluate(expression));
    11.     }
    12. }
    13.  
    This correctly prints out 3 for me.

    This won't work on mobile or the webplayer though.
     
  45. stimarco

    stimarco

    Joined:
    Oct 17, 2007
    Posts:
    721
    It never occurred to me to try just dropping in an assembly.

    I kept getting "cannot find gmcs.exe" errors, rather than errors relating to the availability of that particular API. No problem compiling. Just running. I spent ages trying to get Unity to call the Mono compiler inside its own App folder structure, to no avail. I even messed about with the environment variables, but that didn't work either.

    Isn't adding own assemblies to a project a Pro-only feature?
     
  46. jhillery

    jhillery

    Joined:
    Mar 13, 2012
    Posts:
    3
    Adding native code plugins is a Pro-only feature, but, as far as I know, the free version of Unity can use managed assemblies without any problems.
     
  47. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    If it's a plain C# assembly-dll then you don't need pro for that, and it should generally work on all platforms, subject to what the code actually does under the hood.
     
  48. echtolion

    echtolion

    Joined:
    Jun 16, 2011
    Posts:
    140
    If you want a user-friendly scripting language I'd suggest checking out Boo, I use it(Er, Unity does too but does anyone really use it? :p). It's really fast thanks to in memory assembly compilation + CIL(I got much, much better performance hosting Boo over Ironpython,) and very similar to python.

    Just my 2 cents.

    boo.codehaus.org/Boo+as+an+embedded+scripting+language

    I have no idea if it would work in the webplayer as I never tried it.
     
    Last edited: Jun 21, 2012
  49. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,554
    Another use case I'd like to clarify is I want to let players mod my game, but I don't want them access to the engine's innards (i.e. UnityEngine namespace) as that would let them cheat their way in a single-player campaign, but I do want to let them create their own custom campaigns, so I want to provide a sandboxed environment to them, and that's possible with KopiLua.

    I might also try to dabble with IronPython and see which one is better for my situation.
     
    chisake likes this.