Usage of import/export within the structure of an addon

0 favourites
  • 3 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • Introduction

    Hello!

    I'm using an addon that basically is able to convert CSV files into Jsons or Dictionaries. It used to work well with the scripting mode set to "classic", however this option is no longer available, and my app throws an error when executing and ceses to function.

    Problem

    I'm using papaparse in order to convert CVS files, and I'm not able to import the "Papa" object into the "expresions.js" file where I have the code that construct 3 executes at runtime. With the "classic" scripting mode I could simply use the object, but now it throws an error since it is "undefined". The following snapshot will give you a glance at how the plugin is setup and what my code looks like, as well as the error that it produces:

    Attempts at fixing the problem

    What I tried first is importing the module using the following statement in the "expresion.js" file, right below the "use strict" directive:

    import * as Papa from '../lib/papaparse.min.js

    Sadly, an error is thrown indicated that the module can't be loaded. I'm gessing that the path is no longer correct once contruct exports the addon and is using it on runtime.

    The file is included in the "addon.json" file's "file-list", and is also indicated as a dependency in the plugin.js file:

    this._info.AddFileDependency({ filename: "lib/papaparse.min.js", type: "external-runtime-script" });

    Call for action

    - How should I import a module with the new scripting system, if not the way I'm doing it nor what I tried to do already?

    - Could you point me to an addon that is using a library in a similar way, so I can figure it out through reverse enginiering?

    Thank you very much, I look forward to your answers!

  • I would guess you just need to access any global variables via globalThis, as described by our guide on changes for modules.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you for your answer Ashley, I was able to solve the problem thanks to it. I'm now going to elaborate a bit on what I had to do exactly, in case anyone else runs into a similar problem, but before that I'd like to take this chance to tell you that I love Construct, congratulations on a great game engine!

    Solution to my issue

    Contrary to what I believed, the error wasn't produced by the piece of code I took the screenshot of (expressions.js). It was produced by the "papaparse.js" library!

    It was the "root" parameter that was undefined, I simply had to substitute it by "globalThis", as Ashley indicated, and do the same to access the "Papa" object from

    "expressions.js":

    papaparse.js

    :

    expressions.js

    :

    Uninformed opinion

    I am by no means a javascript expert (just a junior coder in SngularStudios with a C++ background), but after doing a bit of research, the piece of code I had to modify looks like a UMD (Universal Module Definition).

    Would it be possible for Construct to pass the "globalThis" object through the "root" parameter whenever this function is called? This would allow libraries using this standard to be imported into Construct without having to modify them.

    Just an uninformed suggestion, please correct me if this is not possible

    Thanks again!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)