How to import modules in addons in sdk v2?

0 favourites
  • 6 posts
From the Asset Store
Telegram Mini Apps SDK is a plugin that allows impliment Telegram Mini Apps features.
  • 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.

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