Search Unity

Add Scripting interpreter to teach code

Discussion in 'Scripting' started by tbll, Jun 5, 2016.

  1. tbll

    tbll

    Joined:
    Jan 19, 2013
    Posts:
    31
    Hi everyone,

    I am currently working on a project where the purpose is to teach programming by playing a game. Therefore, I want to include a Javascript interpreter.

    I started working with Jurassic (https://www.assetstore.unity3d.com/en/#!/content/2345) : it perfectly works when I run the game inside the editor or in Standalone builds. However, my target platform is WebGL and I don't find a way to make it work, I receive this error : "Attempting to call method '(null)' for which no ahead of time (AOT) code was generated."

    My code :
    engine.SetGlobalFunction("Tirer", new System.Action<double>(_Tirer));
    engine.Execute("Tirer(1);");

    I understand WebGL uses IL2CPP and that can mess up some things. But anyone have an idea to make it work?

    If not, anyone has another solution to integrate a javascript interpreter inside a WebGL build?

    Thanks in advance.

    Big kiss from Paris under the rain :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I have no direct experience with the WebGL target platform, but here is a possible explanation:

    The code stripping settings in Unity may be on. This means that at build time it might be deciding that you didn't use certain classes/methods, so it strips them out. To disable this function, under Player Settings, under Other Settings, disable "Strip Engine Code"

    An alternate explanation might have something to do with certain namespaces not being accessible for security reasons on the WebGL build. It might not let you random add/remove methods, or they might need to be declared in advance, etc. I know little of JS's details but this could be something to look at.

    Big kisses back from Costa Mesa in the sunshine! :)
     
  3. tbll

    tbll

    Joined:
    Jan 19, 2013
    Posts:
    31
    Unfortunately, it does not work.

    Instead of the previous error, I get this one: "Unsupported subtype of MethodBase."

    Any other idea.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    If your actions changed the error, you have either introduced a new error that happens earlier, or you have found the problem and moved it or changed it in some way.

    Anyone else here have any AOT/JIT experience on this web platform??
     
  5. tbll

    tbll

    Joined:
    Jan 19, 2013
    Posts:
    31
  6. AlonsOh

    AlonsOh

    Joined:
    Jan 11, 2017
    Posts:
    6
    Hi, I'm working on a similar project with the same jurassic engine and webgl platform, and getting the same Reflection.Emit error on Evaluate and Execute, did you happen to solve the problem? does anyone have an idea of how to solve this? or have an alternative runtime interpreter? even a pseudo will do for me, no need to actually access unity core classes, just interpret and compile code and get a result (in javascript).
     
  7. AlonsOh

    AlonsOh

    Joined:
    Jan 11, 2017
    Posts:
    6
    If anyone finds this and is still looking for an answer, I found another engine, it's called Jint by Sebastien Ros, here is the link to the git repository: https://github.com/djkrose/jint-unity
    All you have to do is compile the project, get the Jint.dll and Microsoft.Scripting.Core.dll into your assets and you are good to go, it's similar to jurassic and this works fine with Unity WebGL.
    Code (CSharp):
    1. using Jint;
    2.  
    3. //...
    4.  
    5. Engine _engine = new Engine();
    6. Debug.Log(_engine.SetValue("MyGlobalVariable",0)
    7.   .Execute("MyGlobalVariable++;")
    8.   .GetValue("MyGlobalVariable").AsNumber()); //1.0
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748