Netscript Miscellaneous¶
Netscript Ports¶
Netscript ports are endpoints that can be used to communicate between scripts. A port is implemented as a sort of serialized queue, where you can only write and read one element at a time from the port. When you read data from a port, the element that is read is removed from the port.
The read()
, write()
, clear()
, and peek()
Netscript functions can be used to interact with ports.
Right now, there are only 20 ports for Netscript, denoted by the number 1 through 20. When using the functions above, the ports are specified by passing the number as the first argument.
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!
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:
[]
Now assume we ran the following simple script:
for (i = 0; i < 10; ++i) {
write(1, i); //Writes the value of i to port 1
}
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]
Then, assume we run the following script:
for (i = 0; i < 3; ++i) {
print(read(1)); //Reads a value from port 1 and then prints it
}
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]
Port Handles
The getPortHandle()
Netscript function can be used to get a handle to a Netscript Port.
This handle allows you to access several new port-related functions and the
port's underlying data structure, which is just a Javascript array. The functions are:
-
NetscriptPort.
write
(data)¶ Arguments: - 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.
-
NetscriptPort.
tryWrite
(data)¶ Arguments: - 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.
-
NetscriptPort.
full
()¶ Returns: True if the Netscript Port is full, and false otherwise
-
NetscriptPort.
empty
()¶ Returns: True if the Netscript Port is empty, and false otherwise
-
NetscriptPort.
clear
()¶ Clears all data from the port. Works the same as the Netscript function clear
-
NetscriptPort.
data
¶ The Netscript port underlying data structure, which is just a Javascript array. All valid Javascript Array methods can be called on this.
Port Handle Example:
port = getPortHandle(5);
back = port.data.pop(); //Get and remove last element in port
//Remove an element from the port
i = port.data.findIndex("foo");
if (i != -1) {
port.data.slice(i, 1);
}
//Wait for port data before reading
while(port.empty()) {
sleep(10000);
}
res = port.read();
//Wait for there to be room in a port before writing
while (!port.tryWrite(5)) {
sleep(5000);
}
//Successfully wrote to port!
Comments¶
Netscript supports comments using the same syntax as Javascript comments. 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 */
print("This code will actually get executed");
Javascript Math Module¶
The Javascript Math Module is supported in Netscript and is used in the same way:
numThreads = Math.floor(getServerRam("foodnstuff")[1] / 3.4);
Javascript Date Module¶
The Javascript Date Module is supported in Netscript. However, since the 'new' operator does not work in Netscript, only the Date module's static functions can be used:
- now()
- UTC()
- Parse()
- Maybe some others I don't know about
Example:
time = Date.now();
Javascript Number Module¶
The Javascript Number module is supported in Netscript.
Example:
tprint(Number.isInteger(1)); //True
tprint(Number.isInteger(1.534059)); //False