NETSCRIPT: allow getFunctionRamCost to get base RAM cost for scripts (#2771)

This commit is contained in:
Mathekatze
2026-05-17 03:37:58 +02:00
committed by GitHub
parent 6fcdb46888
commit 0257a37b9e
4 changed files with 33 additions and 6 deletions
+14 -2
View File
@@ -4,7 +4,9 @@
## NS.getFunctionRamCost() method
Get the ram cost of a netscript function.
Get the RAM cost of a netscript function.
The base RAM cost per script thread can also be retrieved by using `"baseCost"` as argument to this function.
**Signature:**
@@ -42,7 +44,7 @@ string
</td><td>
The fully-qualified function name, without the leading `ns`<!-- -->. Example inputs: `hack`<!-- -->, `tprint`<!-- -->, `stock.getPosition`<!-- -->.
The fully-qualified function name, without the leading `ns`<!-- -->.
</td></tr>
@@ -56,3 +58,13 @@ number
RAM cost: 0 GB
## Example
```js
const RAM_baseCost = ns.getFunctionRamCost('baseCost');
const RAM_for_hack = ns.getFunctionRamCost('hack');
const RAM_for_tprint = ns.getFunctionRamCost('tprint');
const RAM_for_stock_getPosition = ns.getFunctionRamCost('stock.getPosition');
```
+3 -1
View File
@@ -718,7 +718,9 @@ Get the metadata of a file.
</td><td>
Get the ram cost of a netscript function.
Get the RAM cost of a netscript function.
The base RAM cost per script thread can also be retrieved by using `"baseCost"` as argument to this function.
</td></tr>
+4 -1
View File
@@ -96,7 +96,7 @@ import { hasScriptExtension } from "./Paths/ScriptFilePath";
import { hasTextExtension } from "./Paths/TextFilePath";
import { ContentFilePath } from "./Paths/ContentFile";
import { hasContractExtension } from "./Paths/ContractFilePath";
import { getRamCost } from "./Netscript/RamCostGenerator";
import { getRamCost, RamCostConstants } from "./Netscript/RamCostGenerator";
import { getEnumHelper } from "./utils/EnumHelper";
import { ServerConstants } from "./Server/data/Constants";
import { assertFunctionWithNSContext } from "./Netscript/TypeAssertion";
@@ -1500,6 +1500,9 @@ export const ns: InternalAPI<NSFull> = {
}),
getFunctionRamCost: (ctx) => (_name) => {
const name = helpers.string(ctx, "name", _name);
if (name === "baseCost") {
return RamCostConstants.Base;
}
return getRamCost(name.split("."), true);
},
tprintRaw: () => (value) => {
+12 -2
View File
@@ -9002,12 +9002,22 @@ export interface NS {
getResetInfo(): ResetInfo;
/**
* Get the ram cost of a netscript function.
* Get the RAM cost of a netscript function.
*
* The base RAM cost per script thread can also be retrieved by using `"baseCost"` as argument to this function.
*
* @remarks
* RAM cost: 0 GB
*
* @param name - The fully-qualified function name, without the leading `ns`. Example inputs: `hack`, `tprint`, `stock.getPosition`.
* @param name - The fully-qualified function name, without the leading `ns`.
*
* @example
* ```js
* const RAM_baseCost = ns.getFunctionRamCost('baseCost');
* const RAM_for_hack = ns.getFunctionRamCost('hack');
* const RAM_for_tprint = ns.getFunctionRamCost('tprint');
* const RAM_for_stock_getPosition = ns.getFunctionRamCost('stock.getPosition');
* ```
*/
getFunctionRamCost(name: string): number;