Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Running scripts may be loaded before main UI #1726

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

catloversg
Copy link
Contributor

When we load running scripts, those scripts may run before the main UI is loaded. In that case, anything that interacts with the router will fail to run. For example, Singularity APIs that call Router.toPage() will throw an error, and that error may not be shown to the player because the main UI is not loaded.

How to reproduce:

  • Join Daedalus.
  • Run test code.
  • Reload and check the console.

If you cannot reproduce it consistently, you can forcefully delay the main UI: In src\ui\LoadingScreen.tsx, add another promise (e.g., new Promise((resolve) => setTimeout(resolve, 1000))) to [initSwc(), Engine.load(saveData)].

Test code:

/** @param {NS} ns */
export async function main(ns) {
  try {
    ns.singularity.workForFaction("Daedalus", "hacking");
  } catch (e) {
    console.error(e);
  }
  while (true) {
    await ns.sleep(1000);
  }
}

Closes #1707.

@d0sboots
Copy link
Collaborator

d0sboots commented Oct 24, 2024

This fixes a bug, so if push comes to shove I'll accept it. However, this is only fixing it for the specific case of script loading, it's not fixing the root of the issue. For instance, this won't fix the game (sometimes) crashing at load, or our tests flaking.

Edit: What I'm hoping for is a way for the router to actually be initialized before everything, in some guaranteed deterministic fashion.

@catloversg
Copy link
Contributor Author

catloversg commented Oct 25, 2024

In the latest commit, I add a queue that stores router actions when the main UI is not loaded. That queue will be processed after the main UI is loaded. This change should fix those crashes that happen because the router is used before the initialization.

How to test it:

  • In src\ui\LoadingScreen.tsx, delay loading the main UI.
  • In engine.tsx, before Engine.start(), call Router.toPage() and Router.back().

Instead of a crash, the game shows this warning popup:
Capture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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