Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Why not an embedded script interpreter rather than C# assemblies ???

Discussion in 'General Discussion' started by mat_muze, Feb 24, 2016.

  1. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,680
    You seem to live in a fairy tale world :). None of your mentioned frameworks/libraries support even the half of the platforms we do.
     
    tatoforever, schmosef and Kiwasi like this.
  2. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    OS-specific functions crop up all the time in all low level subsystems (even something as simple as file IO). Basic things like read/write sure probably no problem. Want to change permissions on a file? Large file? Want to read it in a more memory efficient manner cause you know the structure of the file? There is always a gray area. This is not even a more complex subsystem like threads or networking. The subset of the real interface represented in an interpreted language like Lua or Python is what gives it it's portability (less stuff for the interpreter author to write) but that is a double edge sword because it's going to ignore the OS specific ways that have more power and performance.

    Interpreted languages are so portable because they operate on subsets of the real power (and performance).
     
    tatoforever and Ostwind like this.
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,288
    I'd gladly trade support for 75% of them for better lightmapper and overall reliability, though. I still remember 5.2 release.
     
    Roni92pl likes this.
  4. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    340
    I guess most of us would prefer it like this. BUT more platforms supported = more money ;)
     
  5. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,340
    Happily, because we didn't opt only for interpreted languages in Unity, it is no longer an either-or question: it is practical to have both compiled C# code and, say, MoonSharp Lua, in the same project.
     
    StacyKit10, CDMcGwire and Ryiah like this.
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,156
    This seems like the best place to ask.

    Is it possible to compile assemblies and load them while a unity game is running? Say you include a sort of script editor in game, could you execute some script or call to a system tool to compile it, get the assemblies, and then load it into the game? People always say unity is the best game made with unity, so would it possible to extend that concept?

    I'd love to have a game where you build up the game from inside the game. Seems more than possible with asset bundles and the changes to how they work.
     
  7. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,437
    I just can image the amount of extra work needed to parse a simple line like "a+b" compared to executing one CPU instruction like "add eax, ecx".

    As to numbers, here is a comparison between jit-compiled LUA vs interpreted LUA: http://luajit.org/performance_x86.html

    NB: Those interpreters consume LUA's bytecode, not the original sources. Otherwise they would run times more slow.
     
    StacyKit10 likes this.
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,680
    Only on non-AOT platforms that run Mono. That generally limits you to editor and desktop.
     
    Tomnnn likes this.
  9. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,156
    I can do it for mac, windows, linux!? Thanks! Any links to a resource that would indicate how I go about compiling and where those files end up? Or if they get packaged into a bundle I can load, because I've already done that :D

    I don't mind not being able to do this on mobile, I'm more of a "the best use of a mobile device is a companion app / peripheral" sort of game developer.

    pls do give more details though, this could get me back into active development... and become a major talking point for future unity vs unreal debates ;) The ease of access of implementing mods in unity is simply... using unity.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,680
    Umm.. download Roslyn from github, build it, put it into streaming assets, at runtime do Process.Start() passing whatever arguments you want, and if it's successful, do Assembly.Load(path).
     
    kaiyum, mdrotar, Ryiah and 1 other person like this.
  11. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,156
    thanks, will do.

    --edit

    I can't seem to resolve an issue with my memory. Why do I want to do this if I could just make something with the editor and export a bundle? All this does is make the person developing the code not need to open an editor and leave the game open.

    Is there any benefit to this?
     
    Last edited: Feb 25, 2016
  12. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,288
    downloadable plugins/assemblies.

    The issue is it is an open road for malware though. Embedded language can be sandboxed away from the OS, while with C# you have access to System.IO and can wreck at least user's document folder.

    I may be wrong about it, though. I have not investigated if unity implements any kind of sandboxing for potentially dangerous system calls.
     
  13. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,437
    With C# you have access to the whole WinAPI.
     
  14. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,156
    Oh, I see. Well I'm a LAN enthusiast who doesn't release games to the general public so I think I'll take my chances with my friends trying to destroy one another's hardware. Though I do like being able to give things instructions and operating modes in games, it's going to be hard to compete with a modding system that allows you to do anything the engine / c# is capable of.

    You could have assets from any kind of game become part of another game. Maybe this will have unforeseeable consequences in the future where every game has every feature of other games because of overly accessible modding...
     
  15. Mwsc

    Mwsc

    Joined:
    Aug 26, 2012
    Posts:
    189
    As you mention, the window creation code must be written for each platform.
    It also has to be bound to the language.
    This work has to be done for each combination of language and operating systems.
    This is true for C# and for python. There is no difference.

     
  16. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    So the technical details of portability kinda go over my head. I know that I had an issue with using an XML related library in a C# script that didn't port to Windows Phone. So my guess is it's those kinds of portability issues that OP thinks interpreters would solve.

    However, this is a game engine. It's certainly more friendly to devs than a lot of other game engines. But the end product is not for devs, it's for players. So if the question is portability or performance, performance is going to have more weight. And in my aforementioned portability problem, I solved by using a more generic method of read/write.

    The more compiling done before running the game, the better the performance. Yes? The Unity Engine is built to make commercially viable games and not just pet projects. Yes?
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Yes. Have you ever compared a classic ASP application with a VB .NET application (just staying within the same language family)? Also, before Zend Optimizations, PHP performance is significantly lacking because of it's interpreted nature. Good compilers can perform optimizations when building the resulting output which help improve performance.

    Now, we should also consider performance in terms of Development. JavaScript is painful to write (as a web based scripting language) because of the fragility of the code you're writing. That's where TypeScript comes into play. It generates javascript, but during development it offers you some sweet advantages such as compile time type checking. Even though the resulting JavaScript is type agnostic, you write in a way that isn't that helps harden your code during development and make the troubleshooting much easier and code more bug free.

    That advantage trickles through to Unity as well using C# (or UnityScript for that matter since it's not really JavaScript and still compiles to IL). You get compile time checking of types and constraints and the ability to unit test and reuse code easily as well as leverage existing third party libraries.

    By using some cutesy-wootsy little scripting language that's interpreted at runtime you lose all of those capabilities, so your statement of "it just works properly" only holds true when you write it properly... but when you don't your problems become significantly harder to identify. Interpreted code and scripting languages have their place, but inside of medium to large scale professional development projects is not one of them.
     
    Ostwind, Ryiah and Kiwasi like this.
  18. seashell86

    seashell86

    Joined:
    Dec 6, 2013
    Posts:
    8
    I think using a statically typed Python syntax that compiled into C++ would be amazing. It would be even more amazing if you could bring over the most important modules in the standard library. Now that would be a language you could use for almost anything. I know there are some projects aiming to do this but none are quite ready for production it seems.
     
  19. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,148
    Use Boo and enable IL2CPP.
     
    Dustin-Horne likes this.
  20. StacyKit10

    StacyKit10

    Joined:
    Sep 13, 2016
    Posts:
    2
    I though sharing my professional experience might add to the topic...

    The company I worked for (4 years ago), as lead graphics engineer, chose Unity (over 6 years ago) specifically because it used C#. We had tried embedding a JavaScript interpreter, Boost.Python, and also Lua, with poor results. We ultimately chose Unity to try and increase the quality of code we could produce, as well as increase the hiring pool of developers we had access to.

    C#'s JIT gave us speed. It's wealth of online examples, made reference material trivial. It's much closer to C++, so we were able to train most of our C++ developers in C#. The amount of committed defects dropped substantially. Confidence levels in the majority of our teams grew as well.

    After we got Unity, we removed all other scripting solutions. The JavaScript interpreter was great (I think I was the only person who thought this), however, out of over 200 developers, only about 10 knew rudimentary JS; possibly only one person knew JS confidently enough to feel comfortable making games with it (you can probably guess who that was).

    While LUA is incredibly easy to integrate into C++, and very easy to modify (we added the same kind of changes Lucas Arts did to support thread-safety in SCUMM), it has little to no OO support: OO code creates smaller change lists. LUA is also pretty specific to being embedded in game engines. So this actually reduced our potential developer pool. Honestly, the only developers we had who wanted to use LUA were those with familiarity from courses taught at Full Sail.

    Python integration became a problem. We never really committed to this option as we liked the kind of developer C# brought.

    C# won us the most as it feels like Java, looks like C++, is easier to code than both of those, and made positions in our company more attractive, because it was a growing market, so many of our developers wanted it on their resumes.

    As a personal note: I'm still torn between Boost.Python, Spider Monkey, and Mono in my personal game engine; I really like the "Drop down console", the immediacy of interpreter languages, however, Mono is actually pretty easy to integrate into C++, and I know it will open up so many more options due to the it being JIT. I feel more comfortable pushing C++ code into it, than I do into an interpreted scripting language.
     
    Jamster, tatoforever, Ryiah and 3 others like this.
  21. derf

    derf

    Joined:
    Aug 14, 2011
    Posts:
    349
    Ouch! Oh my brain stubbed its toe when it "interpreted" that.
     
  22. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,068
    Android and iOS too.
     
  23. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,148
    It's still basically a beta and you need a recent device (iPhone 6, iPad Air 2, Samsung Note 5, Motorola Nexus 6, etc).
     
  24. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,068
    It's CryEngine you know... :D
     
  25. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,703
    Interpreting a programming language in realtime based on its sourcecode is extraordinarily slower than ANY form of compiled execution, even if its executing a bytecode rather than pure 'executable'. Having to parse the structure of the code and interpret it and do lexical analysis and all that stuff, it is quite computationally expensive. You're basically having to convert the code on the fly and run tasks based on it, and every single time the same bit of code has to be interpreted you have to do it afresh. This is why languages like Java came up with bytecodes and Just In Time (JIT) compilers, because the speed of interpreting in realtime is horrible. And yet, even JIT compilers, although perhaps capable of some realtime optimizations that are interesting, are still not as efficient as if the program is simply compiled already as executable machine language opcodes (binary). Unity IL2CPP compiler also, to my understanding, does this, converting the C#/.net backend into an actual executable, which gives even more performance boost. If your issue is one of performance concerns then you absolutely have to go toward a fully pre-compiled immedaitely executable binary, absolutely nothing else is going to be faster because it introduces conversion overhead. Sure there are benefits to realtime interpretation such as changing code and stuff on the fly etc.. so go use a scripting language like LUA or something..but that's more an exception than the rule and definitely not good for performance. Why use a translator when you can speak the native language.
     
  26. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,250
    Just a note that eval() in Unityscript does work with arbitrary text, probably only on desktop platforms though.

    Code (javascript):
    1. function Start () {
    2.     var x = 4;
    3.     var y = 0;
    4.     var runthis = "Debug.Log (x + 2); y = 1;";
    5.     eval (runthis);
    6.     Debug.Log (y);
    7. }
    --Eric
     
    Kiwasi likes this.
  27. songtianming

    songtianming

    Joined:
    Aug 3, 2018
    Posts:
    3
    Ryiah likes this.
  28. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,148
    Awesome. I wasn't aware that Tencent was maintaining Lua bindings for game engines. I have to admit though I'm far more interested in their bindings for UE4 than Unity. C++ can be very daunting for beginners. Being able to point them towards an easier solution will be nice.

    https://github.com/Tencent/sluaunreal
     
  29. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,148
    Are you positive you picked the right game engine? :p
     
  30. rainssong

    rainssong

    Joined:
    Nov 6, 2013
    Posts:
    7
    cause js/Lua is Interpreted language, but C# is Compiled language

    as lua works fine but I hate it, then I tried hard to find this:

    https://github.com/pjc0247/UniScript

    a C# interpreter, looks great, but a little confused
     
unityunity