Purchases a new Hacknet Node. Returns a number with the index of the Hacknet Node. This index is equivalent to the number at the
end of the Hacknet Node's name (e.g The Hacknet Node named 'hacknet-node-4' will have an index of 4). If the player cannot afford
to purchase a new Hacknet Node then the function will return false.
purchaseServer
^^^^^^^^^^^^^^
..js:function:: purchaseServer(hostname, ram)
:param string hostname:Hostname of the purchased server
:param number ram:Amount of RAM of the purchased server. Must be a power of 2 (2, 4, 8, 16, etc.)
Purchased a server with the specified hostname and amount of RAM.
The *hostname* argument can be any data type, but it will be converted to a string and have whitespace removed. Anything that resolves to an empty string will
cause the function to fail. If there is already a server with the specified hostname, then the function will automatically append
a number at the end of the *hostname* argument value until it finds a unique hostname. For example, if the script calls
*purchaseServer("foo", 4)* but a server named "foo" already exists, the it will automatically change the hostname to "foo-0". If there is already
a server with the hostname "foo-0", then it will change the hostname to "foo-1", and so on.
Note that there is a maximum limit to the amount of servers you can purchase.
Returns the hostname of the newly purchased server as a string. If the function fails to purchase a server, then it will return an
empty string. The function will fail if the arguments passed in are invalid, if the player does not have enough money to purchase
the specified server, or if the player has exceeded the maximum amount of servers.
Example::
ram = 64;
hn = "pserv-";
for (i = 0; i < 5; ++i) {
purchaseServer(hn + i, ram);
}
deleteServer
^^^^^^^^^^^^
..js:function:: deleteServer(hostname)
:param string hostname:Hostname of the server to delete
Deletes one of your purchased servers, which is specified by its hostname.
The *hostname* argument can be any data type, but it will be converted to a string. Whitespace is automatically removed from
the string. This function will not delete a server that still has scripts running on it.
Specifies whether hostnames or IP addresses should be returned. If it's true then hostnames will be returned, and if false
then IPs will be returned. If this argument is omitted then it is true by default
Returns an array with either the hostnames or IPs of all of the servers you have purchased.
round
^^^^^
..js:function:: round(n)
:param number n:Number to round
Returns the argument *n* rounded to the nearest integer. If the argument passed in is not a number, then the function will return 0.
write
^^^^^
..js:function:: write(port/fn, data="", mode="a")
:param string/number port/fn:Port or text file that will be written to
:param string data:Data to write
:param string mode:Defines the write mode. Only valid when writing to text files.
This function can be used to either write data to a port or to a text file (.txt).
If the first argument is a number between 1 and 10, then it specifies a port and this function will write *data* to that port. Read
about how `Netscript Ports work here <http://bitburner.wikia.com/wiki/Netscript_Ports>`_. The third argument, *mode*, is not used
when writing to a port.
If the first argument is a string, then it specifies the name of a text file (.txt) and this function will write *data* to that text file. If the
specified text file does not exist, then it will be created. The third argument *mode, defines how the data will be written to the text file. If *mode*
is set to "w", then the data is written in "write" mode which means that it will overwrite all existing data on the text file. If *mode* is set to
any other value then the data will be written in "append" mode which means that the data will be added at the end of the text file.
read
^^^^
..js:function:: read(port/fn)
:param string/number port/fn:Port or text file to read from
This function is used to read data from a port or from a text file (.txt).
If the argument *port/fn* is a number between 1 and 10, then it specifies a port and it will read data from that port. Read
about how `Netscript Ports work here <http://bitburner.wikia.com/wiki/Netscript_Ports>`_. A port is a serialized queue. This function
will remove the first element from that queue and return it. If the queue is empty, then the string "NULL PORT DATA" will be returned.
If the argument *port/fn* is a string, then it specifies the name of a text file (.txt) and this function will return the data in the specified text file. If
the text file does not exist, an empty string will be returned.
clear
^^^^^
..js:function:: clear(port/fn)
:param string/number port/fn:Port or text file to clear
This function is used to clear data in a `Netscript Ports <http://bitburner.wikia.com/wiki/Netscript_Ports>`_ or a text file.
If the *port/fn* argument is a number between 1 and 10, then it specifies a port and will clear it (deleting all data from the underlying queue).
If the *port/fn* argument is a string, then it specifies the name of a text file (.txt) and will delete all data from that text file.
:param string hostname/ip:Server on which script is running
:param args...:Arguments that the script is running with
Returns the amount of hacking experience the specified script generates while online (when the game is open, does not apply for offline experience gains).
Remember that a script is uniquely identified by both its name and its arguments.
This function can also return the total experience gain rate of all of your active scripts by running the function with no arguments.
getTimeSinceLastAug
^^^^^^^^^^^^^^^^^^^
..js:function:: getTimeSinceLastAug()
Returns the amount of time in milliseconds that have passed since you last installed Augmentations
sprintf
^^^^^^^
..js:function:: sprintf()
See `this link <https://github.com/alexei/sprintf.js>`_ for details.
vsprintf
^^^^^^^^
..js:function:: vsprintf()
See `this link <https://github.com/alexei/sprintf.js>`_ for details.
prompt
^^^^^^
..js:function:: prompt(txt)
:param string txt:Text to appear in the prompt dialog box
Prompts the player with a dialog box with two options: "Yes" and "No". This function will return true if the player click "Yes" and
false if the player clicks "No". The script's execution is halted until the player selects one of the options.
Defining your own Functions
---------------------------
You can define your own functions in Netscript using the following syntax::
function name(args...) {
function code here...
return some_value
}
Functions should have some return value. Here is an example of defining and using a function::
function sum(values) {
res = 0;
for (i = 0; i < values.length; ++i) {
res += values[i];
}
return res;
}
print(sum([1, 2, 3, 4, 5]));
print(sum([1, 10]));
The example above prints the following in its log::
15
11
**Note about variable scope in functions:**
Functions can access "global" variables declared outside of the function's scope. However, they cannot change the value of any "global" variables.
Any changes to "global" variables will only be applied locally to the function. This also means that any variable that is first defined inside a
function will NOT be accessible outside of the function.
For example, the following code::
function sum(values) {
res = 0;
for (i = 0; i < values.length; ++i) {
res += values[i];
}
return res;
}
print(res);
results in the following runtime error::
Script runtime error:
Server Ip: 75.7.4.1
Script name: test.script
Args:[]
variable res not defined
The following example shows that any change to "global" variable inside a function only applies in the function's local scope::
function foo() {
i = 5;
return "foo";
}
i = 0;
print(i);
foo();
print(i);
Results in the following log::
0
0
**Other Notes about creating your own functions:**
Defining a function does not create a Javascript function object in the underlying game code. This means that you cannot use any function
you create in functions such as `Array.sort() <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort>`_ (not yet at least, I'll try to make it work in the future).