Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Best runtime scripting language for unity?

Discussion in 'General Discussion' started by yoonitee, Nov 7, 2018.

  1. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,052
    I would like to run small script programs at run time in unity. For things like experimenting.

    Things like:

    Code (CSharp):
    1. for(x=1;x<100;x++){
    2.    AddNewMonsterAt(x*10,0,0);
    3. }
    And various things like that. So what do you think is the best scripting language to link into Unity that will let me call some of my c# functions at run time. And also have things like for loops and so on.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    12,436
    You have two basic paths available to you.

    C# itself is the first path. Choosing C# will allow you the most performance because the code will be compiled prior to execution (through a Just-in-Time compiler), but this can potentially allow modders to create malicious scripts and this path is not compatible with AOT platforms like iOS.

    https://assetstore.unity.com/packages/tools/integration/dynamic-c-82084

    Your second path is to choose an interpreted language which won't have the risk of malicious code and will run on AOT platforms but will be significantly slower. There are multiple entries on the Asset Store but the ones linked below have either been kept up-to-date or have positive recent reviews.

    https://assetstore.unity.com/packages/tools/integration/miniscript-87926
    https://assetstore.unity.com/packages/tools/moonsharp-33776
     
    JoeStrout likes this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    11,047
    Why does being interpreted prevent malicious usage?
     
  4. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,191
    Lua is fairly easy to implement. I sometimes use the same package what @TonyLi put in the Dialogue System. (And I'm currently not in the front of my Unity-computer so I can't link it)
    It of course depends how much performance you need. It can be slow if you use it for serious update-loop things.

    Not automatically, but if it's not a DLL, you can prevent it to call into your system. And of course you design the entire API what is published to the scripting language.

    I wouldn't call it protection against 'malicious code', because with DLLs and C# you can inject whatever you want at any time. But it protects against user mistakes.
     
    TonyLi and Ryiah like this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    12,436
    Like @LurkingNinjaDev mentioned it's more the "automatically" part but I didn't think about that at the time. Interpreted is much more easy to filter out the possibility of a mod being malicious because you can modify the interpreter to not allow certain instructions while a compiled approach makes it much more difficult to intercept malicious instructions.
     
    Last edited: Nov 7, 2018
  6. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,191
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    8,463
    The version in the Dialogue System has a lot of fixes. I'd push them to a public repository, but the original author didn't set one up. If anyone would like a copy of the updated LuaInterpreter, let me know and I'll make a unitypackage. There are other free Lua packages on the Asset Store, too, such as MoonSharp, which Ryiah linked above. And there's also @JoeStrout's Miniscript, also linked above.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,814
    Paging @JoeStrout. MiniScript seems like a good fit here.

    If you tightly control what commands your interpreted language has, you can reduce the possibility that someone writes malicious code. For example if you have access to full C#, you can start doing random stuff to the file system. If you only have access to an internal MoveCharacter command, you can't do much damage to the user's system.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,361
    Agreed. :) If you're interested, the forum thread is here. I'd be delighted to talk to you about your runtime scripting needs (including why, after writing three chapters of a book about Lua, I scrapped it and started designing MiniScript instead).
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    Also, you can make lua work in sandbox mode, which only allows access defined commands by user, and basics math.
    But you can make it as well having access to system files.
    And there are few more modes, which allows less, or more permitted access. For example you can get system time, but you can not read / write files.
    Mentioned moonsharp has such options.

    I used it not so long ago in sand box mode.
    I wanted only custom predefined methods, to be executed.
    Therefore, you can not do anything malicious via lua, for example like reflections, or attaching dll etc.
     
  11. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    857
    Well you made me curious at least...
     
  12. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    3,154
    I have just started to use Lua becasue Z-Wave uses it. It must be the most backward language in existing. I mean, this is how you foreach a array in that language.

    Code (CSharp):
    1. local devices  = {19}
    2. switchedOnDevices  = {}
    3.  
    4. for i, device in ipairs(devices) do
    5.    if luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device) == "1" then
    6.       table.insert(switchedOnDevices, device)
    7.       luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, device)    
    8.    end
    9. end
    or that static table.insert to mimic List behaviour. Interesting
     
    JoeStrout likes this.
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    Just for interested in programming languages family
    lua is roughly at 1 o'clock, next to PHP.
    C is at 10-11o'clock
    C++ at 8-9o'clock
    C# at 3o'clock
     
    JoeStrout likes this.
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,361
    Yes, you've pretty much got it. Lua looks attractive at first, but when you start to see the ugly, you realize it goes very deep. All variables global by default, arrays much harder to use than they should be, etc. I know it does the job it was intended to do, but I found I just couldn't stomach it after a while.

    MiniScript was designed from the beginning to have a clean, straightforward syntax, drawing the best features from modern languages like Python, C#, and VB.NET.

    It's interesting to ponder where it would fall in the language tree above. ...But now that I look at it, I'm not sure that diagram is very useful. It has Visual Basic and VB.NET at opposite sides of the tree, even though the latter is derived directly from the former and has almost the same syntax. It shows Xojo most closely related to Delphi, even though Xojo is also based on Visual Basic and has nothing at all in common with Pascal. It shows C, C++, and ObjC having nothing (!) in common, though in reality both C++ and ObjC were derived directly from C. Go figure. ¯\_(ツ)_/¯
     
    Ryiah likes this.
  15. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    Yep there may be few discrepancies. Perhaps would be needed more research to find most accurate graph.
    There are some overlaps and cross shares across languages too, as far I am aware. Which perhaps is hard to picture on radial graph. I know there is many graphs, but If anyone has better, what more important more reliable graph representation, it would be nice to share it.
     
  16. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,191
  17. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    Looks interesting at glance, but unreadable (too small) :(
     
  18. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,191
    I know, you will have to visit it. :D They have ENIAC too and Enigma and Babbage too! And many more.
     
    JoeStrout and Antypodish like this.
  19. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,052
    Miniscript looks really nice from what I've seen. Nice bits from python and visual basic.

    BTW In case anyone wants to implement yet another programming language here is one I came up with. See if you can guess what it does:
    Code (CSharp):
    1. 1..10 : x=>{
    2.     1..10 : y=>{
    3.             x*y >> " " : print;
    4.     }
    5.     newline;
    6. }
    I might be popular with the German audience because the verbs are at the end! (Not so good for intellisense though!) One could also have an "if" rule as
    condition ? action
    and a "while" rule as
    condition # action
    . Else might be
    ~?


    I think a class would just be implemented as exceptions. Just set something equal to a base class (default being Object) and add exceptions/additions such as:
    Code (csharp):
    1. Dog = Animal @ {
    2.     sound = "woof";
    3.     legs = 4;
    4. }
    (This also works if you think of it as a union between Animal class and an abstract class.)
    Then you would say
     dog = new Dog; sound dog : print
    . (As in sound of dog). OK, now I've got carried away!
     
    Last edited: Nov 8, 2018
    JoeStrout likes this.