What order are custom files run in javascript?

I recently figured out that you can make custom files in javascript, meaning you can organize your code! However, I don’t really know what the order they’re run in, is it main.ts first, last, or all at the same time? When I define functions in either main.ts or a custom file, they can be used anywhere, so I’m kinda confused. @richard

4 Likes

I think it’s the order you create them OR alphabeticaly, but you can check by opening “pxt.json” (is that what is called? It’s below assets.json), pressing “edit settings as text”, and the files shown somewhere are compiled top to bottom. (I wrote that from memory so it may not be accurate.) WARNING, create a duplicate of your project BEFORE opening pxt.json, incase it corrupts your project

NOTE: use an init function rather than changing pxt.json if its the compiling order your worrying about (my voxel engine has an init function, in main.ts I think it’s initVar(), the definition is in vars.ts)

3 Likes

They’re run in alphabetical order except for main.ts, which always runs last.

7 Likes

I’m 75 percent sure it’s alphabetically expect for main .ts which run a last

4 Likes

Then why is a function defined in main.ts able to be used in other custom files?

3 Likes

It depends on when the function is called. Typcally, the only time this would pose a problem is if a function in main.ts is called in the equivalent of an on start block in a different file. If the function is called within another function, then main.ts would be read and interpreted before the function is called.

3 Likes

you can??? dang that’s useful, where did you see this???

do this:

myFunc() // call BEFORE declaration bc why not

function myFunc() {
    console.log(":)")
}

It works, and that is basically what happens with 2 different files

I was just curious what the “plus” button in the explorer did, so I pressed it, and it creates a new custom file

2 Likes

So that’s what it’s meant for!

@AlexK @ScriptScraper actually, they run based on the order of the entries of the files array in pxt.json (except for main.ts which always runs last). so not alphabetical order

4 Likes

Ooh … now that’s an interesting prospect … and now I have questions. :laughing:

Here’s an example: The current state of the files array in my ever-evolving Monopoly project.

{
    "files": [
        "main.ts",
        "README.md",
        "enums.ts",
        "attract.ts",
        "controller.ts",
        "images.g.jres",
        "images.g.ts",
        "settings.ts",
        "avatars.ts",
        "players.ts",
        "dice.ts",
        "firstroll.ts",
        "state.ts",
        "pausemenu.ts",
        "board.ts",
        "background.ts",
        "tests.ts",
        "properties.ts",
        "cards.ts",
        "taxes.ts",
        "strings.ts",
        "actionmenus.ts",
        "actionqueue.ts",
        "tutorial.ts"
    ],
}

It looks, more or less, like the order in which I created the files. Does the editor ever tinker with the order of the files array? If I needed to create a dependency where one file needs to load before another, would this be a “set it and forget it” scenario, or would I need to check pxt.json every time I added or deleted a file in the editor to make sure the ordering was “correct?”

1 Like

i don’t think we ever re-order things. i’m guessing removing a file is just a filter operation

1 Like

wdym? does this mean the order you created them?

yes, in general it will be the order you created them unless you manually change the order in pxt.json.

i think the only files that we ever insert outside of the normal order are the generated .g.ts and .g.jres files

3 Likes