Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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,363
    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.
     
    MD_Reptile likes this.
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,965
    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
     
    aer0ace, BinaryZERO and JoeStrout like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,616
    Why does being interpreted prevent malicious usage?
     
  4. 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:
    20,965
    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. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    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.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    AlejMC likes this.
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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).
     
    ysftulek and MD_Reptile like this.
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    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.
     
  10. steego

    steego

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

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    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.
  12. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    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.
  13. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
  14. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    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.
     
  15. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Looks interesting at glance, but unreadable (too small) :(
     
  16. 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.
  17. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    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
    Thaina and JoeStrout like this.
  18. wixette

    wixette

    Joined:
    Mar 30, 2020
    Posts:
    2
    I made an interactive BASIC runtime (derived from Microsoft Small Basic) that can be embedded in Unity games:

    https://github.com/wixette/isb

    Could be an alternative choice of Miniscript.

     
    Thaina and BenniKo like this.
  19. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    Was looking to start exploring scripting interpreted languages inside Unity but don’t really know what’s the current state of this. Today in 2021, should I go MoonSharp or MiniScript? Or maybe even the Python preview package?
     
  20. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    What's your use case? Player modding? If so, consider a language that your modders are already likely to be familiar with.

    If it's for adding your own functionality post-release, whatever language hooks into your code code most easily.

    In any case, make sure you can sandbox it easily so it doesn't expose too much functionality (e.g., OS functions to delete files).
     
    JoeStrout likes this.
  21. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    For what it's worth, MiniScript is still going strong and has an active user community. This month we're doing a "tweetcode" event, where we write cool programs (usually for Mini Micro, a MiniScript-based fantasy computer) that fit into a single tweet. See https://twitter.com/search?q=#tweetcode%20%23minimicro for some fun examples. :) (Note: the code itself is usually in the first reply to the original tweet.)
     
    Joe-Censored, AlejMC and TonyLi like this.
  22. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    Nice, glad to hear. Will definitely will start giving this a proper look.
    Thanks for the followup.
     
    JoeStrout likes this.