How to import modules in addons in sdk v2?

0 favourites
  • 10 posts
From the Asset Store
Instantly Create Rogue-like Adventures with just 1 Layout! Include this IGM into any existing games.
  • I think it is related to: construct.net/en/make-games/releases/beta/r398

    Can C3 addon files (instance.ts) also import modules? Or are the above only for event scripting?

    I know there used to be post on this, but my google-fu is failing me.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Addon SDK v2 runtime scripts can now import modules as of r398+, but note currently those are currently only beta releases, and so if an addon does that it won't work in the latest stable release. Therefore I've held off from documenting it as it just means people making addons that won't work for most people. However if you want to experiment with it in an addon and hold off on publishing it until the next stable release, then the way to do it is:

    1. Add two extra runtime scripts: main.js and myModule.js (see existing docs on adding extra runtime scripts)
    2. In the editor plugin, call this._info.SetRuntimeModuleMainScript("c3runtime/main.js")
    3. As the content of main.js, use:
    import "./plugin.js";
    import "./type.js";
    import "./instance.js";
    import "./conditions.js";
    import "./actions.js";
    import "./expressions.js";
    

    Now you can write whatever you want in myModule.js, and import it in any other runtime script like normal, e.g.:

    import { Thing } from "./myModule.js"
    

    Basically if your addon does not call SetRuntimeModuleMainScript(), it creates a default one that imports all runtime scripts - in this case including myModule.js, which isn't how you want imports to work. When you call SetRuntimeModuleMainScript(), then Construct only imports main.js, and then all other scripts are loaded by imports - which allows you to choose not to import every runtime script, and instead set up a tree of imports.

    When the next stable update is out I'll update all the SDK samples to use SetRuntimeModuleMainScript() so you can use those as templates.

  • Thank you Ashley, appreciate the help and the addition of this feature.

  • Not quite working for me yet (I can get main.js working, but import of another module in instance.js runtime, no so much). I tried various file dependency types. I'll do globalThis for now and wait for the examples that come with the doc updates.

  • I've updated the addon SDK on GitHub to configure all addons to use modules. Then I can successfully test a module import by:

    1. Add mymodule.js in c3runtime folder, and add references (this._info.AddC3RuntimeScript("c3runtime/mymodule.js"); in plugin.js, add "c3runtime/mymodule.js" to file list in addon.json

    2. Write content of mymodule.js:

    export function GetMessage()
    {
    	return "Hello world!";
    }
    

    3. Import mymodule.js somewhere - I tried adding this to the top of instance.js:

    import * as MyModule from "./mymodule.js";
    
    console.log(MyModule.GetMessage());
    

    That successfully logs "Hello world!" to the console on startup for me.

  • Thanks Ashley, that helped me get it to work. I was over thinking it and using file dependency, etc.

    It is great using modules instead of other methods.

  • Ashley

    This works for me in preview.

    Does this work for you in export to html5 also? In my case it seems to put the files I want to use as modules into c3runtime.js, so they are no longer available for import using the file descriptor.

  • I tested this carefully when I developed it, and it should all be working.

    Note there is a difference on how modules work after export depending on if you minify: without minifying, then it preserves the file structure and keeps your imports; if you minify then everything is bundled in to a single source file, but that should not break anything as the bundler follows all the imports and preserves cross-references. If you do a dynamic import, things get tricker, but I think you can still configure that using a file dependency.

  • Ok, I realized that it may be because I am trying to use when refactoring an older SDK V1 addon. Perhaps that's where the issue is, this is meant only for SDK V2.

  • Yeah, you can't use any of this new modules support with SDK v1 addons, only SDK v2.

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