Skip to content

Instantly share code, notes, and snippets.

@Spartelfant
Last active October 6, 2022 15:27
Show Gist options
  • Save Spartelfant/e3b62c308c8d7e8203fcf13a8532691d to your computer and use it in GitHub Desktop.
Save Spartelfant/e3b62c308c8d7e8203fcf13a8532691d to your computer and use it in GitHub Desktop.
Bitburner ns2 script: a script to spawn any script without the 10-second delay imposed by ns.spawn()

Bitburner ns2 script: a script to spawn any script without the 10-second delay imposed by ns.spawn()

As handy as ns.spawn() is, the 10-second delay before it fires up the script can be annoying. However using ns.run() isn't always an option, for example due to RAM constraints, or when you want a script to relaunch itself with the same arguments but a different number of threads.

Which is why I made this incredibly simple script which does almost the same as ns.spawn(). My script costs 2.6GB of RAM, while ns.spawn() costs 2GB, so my script is only 0.6GB more expensive. Although you will need to use ns.run() to run the spawning script of course, which costs 1GB. So in total this way is 1.6GB more expensive than just using ns.spawn().

On the plus side, the script calling quickSpawn.js now has a lower RAM cost: down from 2GB for ns.spawn() to 1GB for ns.run(). And since quickSpawn.js is just being run, not being imported, its RAM cost (2.6GB) does not count towards the RAM cost of the script calling it.

If immediately after calling the quickSpawn.js script you terminate the running script (ns.exit()), you can get away with a 0ms delay. If you need a bit more time, no problem. But you're no longer forced to wait the full 10 seconds that ns.spawn() takes :)

Here's an example of the syntax to immediately respawn the current script with 4 threads and the same arguments:

ns.run(`quickSpawn.js`, 1, 0, ns.getScriptName(), 4, ...ns.args);
ns.exit();
/**
* Executes a script after a specified delay in milliseconds.
*
* This script takes a delay (in milliseconds) as its first argument, followed by the same arguments as ns.run().
*
* For example, to spawn a script with 4 threads after a 10 millisecond delay using the same arguments as the calling script:
* ns.run('quickSpawn.js', 1, 10, 'scriptToSpawn.js', 4, ...ns.args);
*
* @param {NS} ns
*/
export async function main(ns) {
const delay = ns.args[0];
if (delay > 0) {
await ns.asleep(delay);
}
ns.run(...ns.args.slice(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment