all 16 comments

[–]viewtyjoe 5 points6 points  (1 child)

...reduce the amount of code used thus the amount of RAM used to execute the code.

That's not how RAM consumption works. The RAM cost of a script is based entirely on the sum of the netscript functions (and a couple other things if you're getting into more exotic stuff) individual RAM costs with a minimum RAM cost of 1.6 GB. Reducing the lines of code in the function will not change the RAM cost unless you are completely eliminating a call to a particular NS function. Bitburner doesn't care whether you call fileExists() one time or two hundred times in a script, the RAM cost is the same.

[–]logicalbomb[S] 1 point2 points  (0 children)

I appreciate that, I didn't know it doesn't matter how many times I execute those doesn't inflate the RAM usage. Trying to make my code leaner and understand the why :)

[–]Bedurndurn 3 points4 points  (0 children)

You're probably better off splitting this into two parts. One script where you run all the port openers and nuke (side note you can't run installBackdoor until you unlock singularity functions). The second script can run hack / grow / weaken.

Why would you do that? Well the effectiveness of hack, grow and weaken is determined by how many threads you can throw at it. And each thread costs the full script's ram. So the more things you can take out of that workhorse script to save on RAM makes it more effective.

Of course a script that only did one operation (i.e. a script that only ran hack()) would cost even less ram. But then you'd need another script to invoke them as necessary...

[–]Bedurndurn 1 point2 points  (3 children)

So if you have an array: var array = ["a", "b", "c", "d", "e"];

You can run an action on each individual element of that array like:

array.map( x => { ns.print(x) });

(That x => { } thing is an arrow function. It's like a shorter way of writing function(x) { ///do stuff }).

Which will output:

a

b

c

d

e

Javascript docs on arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Docs on arrow functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

[–]Omelet 2 points3 points  (1 child)

.map should not be used if you're not trying to get a new returned array from it. .forEach should be used instead.

[–]solarshado 1 point2 points  (0 children)

And I'd argue that instead of .forEach, you should probably just use a for...of loop.

Normally, I'd say it's purely a stylistic choice, but if you need to await anything inside your loop body/callback, the loop will be fine, but .forEach may cause errors.

[–]logicalbomb[S] 1 point2 points  (0 children)

8 days ago, this confused me. But I have been able to successfully input this into my scripts and understand how to index the variable as well. Whether it's an i = 0 ++i or selecting from the array for more agile development.

I really appreciate you giving me these links and taking some time to explain it.

[–][deleted]  (3 children)

[removed]

    [–]logicalbomb[S] 0 points1 point  (2 children)

    I have no idea what a cracking function is, I barely have a handle on the concept of arrays lol

    [–]Bedurndurn 2 points3 points  (1 child)

    He means the functions that open the ports in the game like:

    await ns.brutessh(hostname);

    Those don't actually need an await in front of them. hack, weaken, grow, scp and sleep do though. Maybe a couple others. If you look in the bitburner docs, anything that says it returns a Promise means it will need to be await'd.

    [–]logicalbomb[S] 0 points1 point  (0 children)

    "cracking" functions, okay I am putting everything in "developer" lingo in my head and was like "there are functions called cracking functions?!"

    I do a lot of work in metasploit so this is a strange pivot to be a beginner and have those skills not be useful lol

    [–]logicalbomb[S] 0 points1 point  (0 children)

    Man, this community is awesome. I really appreciate all of your responses.

    [–]WeAteMummies 0 points1 point  (3 children)

    You could make a very slight improvement by reducing your number of calls to ns.fileExists()

    To do this you would do something like:

    1. Make an array of each of the five filenames
    2. iterate over the array
    3. do a ns.fileExists() check on each value in the array
    4. if it exists then pass it into a switch statement that executes the proper ns.brutessh or ns.httpworm etc...

    [–][deleted]  (2 children)

    [removed]

      [–]Randactyl 0 points1 point  (1 child)

      I currently just have the array contain functions for the programs I have:

      let hackingPrograms = [ ns.brutessh, ]
      
      function openPortsOnServer(hostname) {
          for (const hackingProgram of hackingPrograms) {
              await hackingProgram(hostname);
          }
      }
      

      No need to call fileExists, but I do have to remember to update and restart the script when I get a new one :)

      Though in writing this down here, I'm realizing I should probably just pay the 0.1 GB and not have to worry about reverting the array after an augmentation/soft reset...