Lib.EternaScript fails due to bad data access

  • 1
  • Problem
  • Updated 4 years ago
  • Solved
Did a little bit of digging, and there's a very small issue causing Lib.EternaScript() to fail. The script is acquired with the following code:
data = Script.get_script_sync(id);
script = data['script'][0];
However, if you look at the API's response for a script (like this one), everything is contained under the "data' object, so either the first line should be
data = Script.get_script_sync(id)['data'];
or the second line should be
script = data['data']['script'][0];
I'd assume the first one would be optimal.
Photo of LFP6

LFP6, Player Developer

  • 623 Posts
  • 109 Reply Likes

Posted 4 years ago

  • 1
Photo of nando

nando, Player Developer

  • 393 Posts
  • 74 Reply Likes
Which is why I tend to use a "private trick" in my scripts:

    Library.prototype.EternaScriptFunction = function(id) {
      var code, data, i, inputs, input, script, _i;
      data = Script.get_script_sync(id).data;
      script = data['script'][0];
      code = "";
      if (script['input']) {
        inputs = JSON.parse(script['input']);
        for (i = _i = 0; _i < inputs.length; i = ++_i) {
          input = inputs[i];
          code += "var " + input.value + "=arguments[" + i + "];";
        }
      }
      code = "Lib = new Library();" + code + script['source'];
      return new Function(code);
    };

// now use like this: var f = Lib.EternaScriptFunction(3378282);
// then call, passing parameters
var result = f(x, y);

I never got around to make that addition to the "standard" library though. Since you're raising the issue, there's now officially more than one guy (me) who's bothered by the fact, so I conclude that we now have a good enough reason to bother Jnicol/Caleb with it :)

Will keep you posted.
Photo of LFP6

LFP6, Player Developer

  • 623 Posts
  • 109 Reply Likes
I remember seeing you post that, and had issues with it for some reason, no clue why. Going back and trying it again though it does work like a charm. Such a little fix though, I'd like to see this work without the substitution as it's pretty useful. Thanks!
Photo of nando

nando, Player Developer

  • 393 Posts
  • 74 Reply Likes
Caleb seems to have pushed a patch minutes ago, using one of your proposed fixes. @LFP6: does it work now as you expect?
Photo of LFP6

LFP6, Player Developer

  • 623 Posts
  • 109 Reply Likes
Yep, works like a charm. Thanks!