Skip to content

Why init() functions?

Ideally, none of your code actually runs on its own, it's either ran by the mod loader when your mod loads or by the game itself through Mixins.

The different functions

Currently, there are four init functions:

  • init(pml): The regular init, gets called with an instance of PolyModLoader (pml) after everything in PolyTrack gets declared, but before anything runs.
  • postInit(): Gets called right after the polyInitFunction gets ran, closely ahead of init. NOT THE SAME as onGameLoad.
  • preInit(pml): Also passed the instance of pml, gets ran before any polytrack code is ran at all, only valid place to register global mixins.
  • onGameLoad(): Gets ran right after PolyTrack finishes loading entirely, shortly before main buttons appear.

Please keep in mind that any function call that starts with pml. in any other page assumes that your init is declared with the argument called pml like in the example above. In simple terms, pml is the instance of PolyModLoader.

Adding them to your mod

If you're coming straight from the quick start, your mod class looks like this:

js
class YourMod extends PolyMod {

}

Every init function goes inside your mod class, and has to be declared using lambdas.

js
class YourMod extends PolyMod {
    init = (pml) => {
        this.pml = pml; // so pml is accessible outside of init (not neccesary)
        // regular init
    }
    postInit = () => {
        // post init
    }
    simInit = () => {
        // sim init here
    }
    // other callback methods are declared the same way
}

It is recommended to add any useful function you add into your mod's class so other mods and in-game code can use them as well.

Functions can be defined outside of your main class, but any code outside will be ran whenever your mod is imported by PolyModLoader, regardless of if it's loaded or not. This causes unpreditable stuff, so keep your code ran in init and functions called from mixins.