Skip to content

API: Scripts using singularity on startup can throw Error: "Router called before initialization - toPage(..." #1707

Open
@alainbryden

Description

@alainbryden

Just capturing what's been discussed in Discord.

Player scripts invoked on startup (either because their running state is part of the save, or they are set as the startup script) can run before the game is fully initialized, and this can lead to an error if one of them triggers routing.

Normally, this error silently goes unnoticed (because the game's error handling isn't in place yet?), so the end-result is that the script that should have run on startup crashes and appears to have not run.

If the user script has any kind of error-handling though, they would catch an error like this:
image

Here's a sample script that can be used to reproduce the error fairly reliably just by running it, saving, then refreshing the game:

export async function main(ns) {
    try {
        ns.singularity.workForFaction("Tian Di Hui", "hacking");
        while (true)
            await ns.sleep(10000);
    }
    catch (err) {
        ns.tprint("ERROR: An unexpected error occurred and now this script must terminate:\n" + (`${err.message}\n${err.stack}` ?? err))
    }
}

You might have to update the faction to one you've joined.


As discussed, some possible solutions are:

  • Don't start any user scripts until after the game is done initializing (speed runners might be mad)
  • Just for the handful of singularity scripts which route, wrap the attempt to route in a try/catch. You can then either:
    • ignore the attempt to route and move on (might have weird side-effects e.g. relating to focus penalties)
    • Store the route destination in a variable, and once the UI is initialized, fulfill the route request ASAP. If multiple route-attempts occur, just save the most recent one? (I can only imagine this messing with scripts that try to interact with the UI, like auto-infiltration)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions