bitburner-src/markdown/bitburner.ns.spawn.md
David Walker 1c20a24079
MISC: Make spawn able to have 0 delay (#1333)
This eliminates a hole where spawn was unrelaible, because other scripts
could jump in and steal the RAM. It's not an API break, because 0 used
to be an invalid value.
2024-06-28 18:41:41 -07:00

47 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [spawn](./bitburner.ns.spawn.md)
## NS.spawn() method
Terminate current script and start another in a defined number of milliseconds.
**Signature:**
```typescript
spawn(script: string, threadOrOptions?: number | SpawnOptions, ...args: ScriptArg[]): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| script | string | Filename of script to execute. |
| threadOrOptions | number \| [SpawnOptions](./bitburner.spawnoptions.md) | _(Optional)_ Either an integer number of threads for new script, or a [SpawnOptions](./bitburner.spawnoptions.md) object. Threads defaults to 1 and spawnDelay defaults to 10,000 ms. |
| args | [ScriptArg](./bitburner.scriptarg.md)<!-- -->\[\] | Additional arguments to pass into the new script that is being run. |
**Returns:**
void
## Remarks
RAM cost: 2 GB
Terminates the current script, and then after a defined delay it will execute the newly-specified script. The purpose of this function is to execute a new script without being constrained by the RAM usage of the current one. This function can only be used to run scripts on the local server.
The delay specified can be 0; in this case the new script will synchronously replace the old one. (There will not be any opportunity for other scripts to use up the RAM in-between.)
Because this function immediately terminates the script, it does not have a return value.
Running this function with 0 or fewer threads will cause a runtime error.
## Example
```js
//The following example will execute the script foo.js with 10 threads, in 500 milliseconds and the arguments foodnstuff and 90:
ns.spawn("foo.js", {threads: 10, spawnDelay: 500}, "foodnstuff", 90);
```