2018-08-30 22:27:47 +02:00
.. _netscript_misc:
2018-02-24 23:55:06 +01:00
Netscript Miscellaneous
=======================
2018-08-30 22:27:47 +02:00
.. _netscript_ports:
2018-03-03 22:05:33 +01:00
Netscript Ports
---------------
2023-02-28 12:59:37 +01:00
Netscript Ports are endpoints that can be used to communicate between scripts and across servers.
2018-03-03 22:05:33 +01:00
A port is implemented as a sort of serialized queue, where you can only write
2023-01-06 06:06:34 +01:00
and read one element at a time from the port. Only string and number types may be written to ports. When you read data from a port,
2018-03-03 22:05:33 +01:00
the element that is read is removed from the port.
2018-09-11 20:56:23 +02:00
The :js:func: `read` , :js:func: `write` , :js:func: `tryWrite` , :js:func: `clear` , and :js:func: `peek`
2018-03-03 22:05:33 +01:00
Netscript functions can be used to interact with ports.
2023-02-28 01:54:04 +01:00
When using the functions above, the ports are specified by passing the number as the first argument and the value as the second.
2023-01-06 06:06:34 +01:00
The default maximum capacity of a port is 50, but this can be changed in Options > System. Setting this too high can cause the game to use a lot of memory.
2018-03-03 22:05:33 +01:00
2023-02-07 15:26:02 +01:00
.. important :: The data inside ports are not saved! This means if you close and re-open the game, or reload the page then you will lose all of the data in the ports!
2018-03-03 22:05:33 +01:00
**Example Usage**
Here's a brief example of how ports work. For the sake of simplicity we'll only deal with port 1.
Let's assume Port 1 starts out empty (no data inside). We'll represent the port as such::
[]
2023-01-06 06:06:34 +01:00
Now assume we ran the following simple script
2023-01-06 13:43:09 +01:00
.. code :: javascript
export async function main(ns) {
2023-06-06 07:49:48 +02:00
for (var i = 0; i < 10; ++i) {
2023-01-06 13:43:09 +01:00
ns.writePort(1, i); //Writes the value of i to port 1
}
2018-03-03 22:05:33 +01:00
}
After this script executes, our script will contain every number from 0 through 9, as so::
[0, 1, 2, 3, 4, 5, 6, 7 , 8, 9]
2023-01-06 06:06:34 +01:00
Then, assume we run the following script
2023-01-06 13:43:09 +01:00
.. code :: javascript
export async function main(ns) {
2023-06-06 07:49:48 +02:00
for (var i = 0; i < 3; ++i) {
2023-01-06 13:43:09 +01:00
ns.print(ns.readPort(1)); //Reads a value from port 1 and then prints it
}
2018-03-03 22:05:33 +01:00
}
This script above will read the first three values from port 1 and then print them to the script's log. The log will end up looking like::
0
1
2
And the data in port 1 will look like::
[3, 4, 5, 6, 7, 8, 9]
2023-01-06 06:06:34 +01:00
.. warning :: In :ref: `netscriptjs` , do not try writing base
2019-07-14 05:55:58 +02:00
`Promises <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise> `_
2022-01-10 22:04:07 +01:00
to a port.
2019-07-14 05:55:58 +02:00
2018-03-03 22:05:33 +01:00
**Port Handles**
2023-01-06 06:06:34 +01:00
.. warning :: Port Handles only work in :ref: `netscriptjs` . They do not work in :ref: `netscript1`
2018-08-30 22:27:47 +02:00
2018-03-03 22:05:33 +01:00
The :js:func: `getPortHandle` Netscript function can be used to get a handle to a Netscript Port.
2022-01-10 22:04:07 +01:00
This handle allows you to access several new port-related functions. The functions are:
2018-03-03 22:05:33 +01:00
2021-11-27 00:04:33 +01:00
.. js:method :: NetscriptPort.writePort(data)
2018-03-03 22:05:33 +01:00
:param data: Data to write to the port
:returns: If the port is full, the item that is removed from the port is returned.
Otherwise, null is returned.
Writes `data` to the port. Works the same as the Netscript function `write` .
2021-11-27 00:04:33 +01:00
.. js:method :: NetscriptPort.tryWritePort(data)
2018-03-03 22:05:33 +01:00
:param data: Data to try to write to the port
:returns: True if the data is successfully written to the port, and false otherwise.
Attempts to write `data` to the Netscript port. If the port is full, the data will
not be written. Otherwise, the data will be written normally.
2021-11-27 00:04:33 +01:00
.. js :: method:: NetscriptPort.readPort()
2018-03-03 22:05:33 +01:00
:returns: The data read from the port. If the port is empty, "NULL PORT DATA" is returned
Removes and returns the first element from the port.
Works the same as the Netscript function `read`
.. js :: method:: NetscriptPort.peek()
:returns: The first element in the port, or "NULL PORT DATA" if the port is empty.
Returns the first element in the port, but does not remove it.
Works the same as the Netscript function `peek`
.. js:method :: NetscriptPort.full()
:returns: True if the Netscript Port is full, and false otherwise
.. js:method :: NetscriptPort.empty()
:returns: True if the Netscript Port is empty, and false otherwise
.. js:method :: NetscriptPort.clear()
Clears all data from the port. Works the same as the Netscript function `clear`
2023-01-06 06:06:34 +01:00
Port Handle Example
2023-01-06 13:43:09 +01:00
.. code :: javascript
2018-03-03 22:05:33 +01:00
2023-01-06 13:43:09 +01:00
export async function main(ns) {
port = ns.getPortHandle(5);
back = port.data.pop(); //Get and remove last element in port
2018-03-03 22:05:33 +01:00
2023-01-06 13:43:09 +01:00
//Wait for port data before reading
while(port.empty()) {
await ns.sleep(10000);
}
res = port.read();
2018-03-03 22:05:33 +01:00
2023-01-06 13:43:09 +01:00
//Wait for there to be room in a port before writing
while (!port.tryWrite(5)) {
await ns.sleep(5000);
}
2018-03-03 22:05:33 +01:00
2023-01-06 13:43:09 +01:00
//Successfully wrote to port!
}
2018-02-24 23:55:06 +01:00
Comments
--------
Netscript supports comments using the same syntax as `Javascript comments <https://www.w3schools.com/js/js_comments.asp> `_ .
Comments are not evaluated as code, and can be used to document and/or explain code::
//This is a comment and will not get executed even though its in the code
/* Multi
* line
* comment */
2023-01-06 13:43:09 +01:00
ns.print("This code will actually get executed");
2018-02-24 23:55:06 +01:00
2018-07-24 20:29:38 +02:00
.. _netscriptimporting:
2018-03-12 20:39:04 +01:00
Importing Functions
-------------------
In Netscript you can import functions that are declared in other scripts.
The script will incur the RAM usage of all imported functions.
There are two ways of doing this::
import * as namespace from "script filename"; //Import all functions from script
import {fn1, fn2, ...} from "script filename"; //Import specific functions from script
2023-01-06 13:43:09 +01:00
Suppose you have a library script called *testlibrary.js* ::
2023-01-06 06:06:34 +01:00
2018-03-12 20:39:04 +01:00
2023-01-06 13:43:09 +01:00
.. code :: javascript
export function foo1(args) {
2018-03-12 20:39:04 +01:00
//function definition...
}
2023-01-06 13:43:09 +01:00
export function foo2(args) {
2018-03-12 20:39:04 +01:00
//function definition...
}
2023-01-06 13:43:09 +01:00
export async function foo3(args) {
2018-03-12 20:39:04 +01:00
//function definition...
}
2023-01-06 13:43:09 +01:00
export function foo4(args) {
2018-03-12 20:39:04 +01:00
//function definition...
}
2023-01-06 13:43:09 +01:00
export async function main(ns) {
//main function definition, can be empty but must exist...
}
2023-01-06 06:06:34 +01:00
2018-03-12 20:39:04 +01:00
Then, if you wanted to use these functions in another script, you can import them like so::
2023-01-06 13:43:09 +01:00
.. code :: javascript
2018-03-12 20:39:04 +01:00
2023-01-06 13:43:09 +01:00
import * as testlib from "testlibrary.js";
2018-03-12 20:39:04 +01:00
2023-01-06 13:43:09 +01:00
export async function main(ns) {
const values = [1,2,3];
//The imported functions must be specified using the namespace
const someVal1 = await testlib.foo3(...values); //'...' separates the array into separate values
const someVal2 = testlib.foo1(values[0]);
if (someVal1 > someVal2) {
//...
} else {
//...
}
2018-03-12 20:39:04 +01:00
}
If you only wanted to import certain functions, you can do so without needing
2023-01-06 06:06:34 +01:00
to specify a namespace for the import
2023-01-06 13:43:09 +01:00
.. code :: javascript
import {foo1, foo3} from "testlibrary.js"; //Saves RAM since not all functions are imported!
2018-03-12 20:39:04 +01:00
2023-01-06 13:43:09 +01:00
export async function main(ns) {
const values = [1,2,3];
2018-03-12 20:39:04 +01:00
2023-01-06 13:43:09 +01:00
//No namespace needed
const someVal1 = await foo3(...values);
const someVal2 = foo1(values[1]);
if (someVal1 > someVal2) {
//...
} else {
//...
}
2018-03-12 20:39:04 +01:00
}
2023-01-06 13:43:09 +01:00
.. warning :: Note that the `export` keyword can **NOT** be used in :ref: `netscript1` as it's not supported.
2019-02-09 03:46:30 +01:00
It can, however, be used in :ref: `netscriptjs` (but it's not required).
2018-09-11 20:56:23 +02:00
Standard, Built-In JavaScript Objects
-------------------------------------
Standard built-in JavaScript objects such as
`Math <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math> `_ ,
`Date <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date> `_ ,
`Number <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number> `_ ,
and others are supported as expected based on which version
of Netscript you use (i.e. :ref: `netscript1` will support built-in objects that are
defined in ES5, and :ref: `netscriptjs` will support whatever your browser supports).