Bitburner

Bitburner

View Stats:
Game freezes when running more than 1 "weaken" script?
Thanks to NiGhTMaRE for his help in getting a more complete default script up and running. But as I've been tweaking it and using it on several servers at once, a very strange thing is happening.

If a server has memory to run scripts, I would of course save it, and run it, from that server, instead of Home. Assume all my examples already have the necessary ports opened, and nuke.exe has been run, and hacking skill is currently 216.

So, I'm doing this simple troubleshooting only on the Level 2 servers (from Home). So those would be...

CSEC
zer0
nectar-net
max-hardware

Already unlocked and backdoored CSEC, and no money there anyway, so skip that. Going to zer0...

I set up a script to weaken it to the minimum.

/** @param {NS} ns */ export async function main(ns) { var target = "zer0"; var securityCurrent = ns.getServerSecurityLevel(target); var securityMinimum = ns.getServerMinSecurityLevel(target); while(true) { if (securityCurrent > securityMinimum) { await ns.weaken(target); } } }

And it has been running fine on several servers at once. (Saved on each server, and run from that server.) I had it running on 15 different servers at one point.

But now something weird is happening. Let me continue, and you'll understand. Next I move on to nectar-net, which has already been weakened to the minimum.

So instead of only weakening, I run a script to also grow and hack (Thanks to NiGhTMaRE), as follows...

/** @param {NS} ns */ export async function main(ns) { var target = "nectar-net"; var securityCurrent = ns.getServerSecurityLevel(target); var securityMinimum = ns.getServerMinSecurityLevel(target); var moneyAvailable = ns.getServerMoneyAvailable(target); var moneyMaximum = ns.getServerMaxMoney(target); while(true) { if (securityCurrent > securityMinimum) { await ns.weaken(target); } else if (moneyAvailable < moneyMaximum) { await ns.grow(target); } else { await ns.hack(target); } } }

And that one is running fine also. But the next one is the freeze point.

>>> The game started to gradually allow me less and less active scripts before freezing. It started at 15, then 12, then 7, now down to only 3 active scripts.

So let's move on to the 3rd server, which is max-hardware, and needs to be weakened. So, same script as the first one, except the variable...

var target = "max-hardware";

And POOF, the game freezes, after only running these three active scripts.

I don't see any typos or problems. Do you guys see anything in these simple scripts that would cause the game to freeze?

UPDATE: It seems to only be crashing on max-hardware. But I have no clue why, as the weaken script is the same as all the others, except the target variable.

export async function main(ns) { var target = "max-hardware"; var securityCurrent = ns.getServerSecurityLevel(target); var securityMinimum = ns.getServerMinSecurityLevel(target); while(true) { if (securityCurrent > securityMinimum) { await ns.weaken(target); } } }
Last edited by AlconburyBlues; Nov 5, 2024 @ 7:18pm
< >
Showing 1-14 of 14 comments
old_one_15 Nov 6, 2024 @ 7:57am 
The problem is that weaken-only script. It has nothing to do with the number of scripts running.

1) You're not updating securityCurrent. When you run ns.getServerSecurityLevel() it returns a number, which is what you assign to securityCurrent. So the if statement will either always run or never run, depending on the state of the server when you initialize the script.

2)The script doesn't sleep outside of that if block. If the condition is false (ie the server is already at minimum security), the script will keep checking the condition endlessly and never give control back to the game engine or other scripts. This is why the game freezes. The HGW script you posted doesn't have this problem, because it always executes an awaited function every loop.

If you were updating securityCurrent inside the loop, the weaken-only script would weaken the target to minimum then freeze the game. Because you're not, the script either never crashes or crashes immediately.
AlconburyBlues Nov 6, 2024 @ 5:59pm 
Got it. I think I messed it up by trying to assign a variable to everything. In the code that NiGhTMaRE originally gave me, the calls were directly in the loop, and not triggered only once and assigned a variable.
Last edited by AlconburyBlues; Nov 6, 2024 @ 6:04pm
AlconburyBlues Nov 6, 2024 @ 6:17pm 
@old_one_15, How does this look?

Hack (with weaken and grow) script:
/** @param {NS} ns */ export async function main(ns) { var target = "servername"; var securityMinimum = ns.getServerMinSecurityLevel(target) + 4; var moneyMaximum = ns.getServerMaxMoney(target) * 0.75; while(true) { if (ns.getServerSecurityLevel(target) > securityMinimum) { await ns.weaken(target); } else if (ns.getServerMoneyAvailable(target) < moneyMaximum) { await ns.grow(target); } else { await ns.hack(target); } } }

Weaken only script:
/** @param {NS} ns */ export async function main(ns) { var target = "servername"; var securityMinimum = ns.getServerMinSecurityLevel(target) + 4; while(true) { if (ns.getServerSecurityLevel(target) > securityMinimum) { await ns.weaken(target); } } }
Last edited by AlconburyBlues; Nov 6, 2024 @ 6:18pm
old_one_15 Nov 6, 2024 @ 7:38pm 
Originally posted by AlconburyBlues:
Got it. I think I messed it up by trying to assign a variable to everything.

I tend to do the same thing. The problem is that most of the in game functions don't return references to stats, they return the current value.

The HWG script looks good. It should actually grow and hack now.

That weaken-only script will eventually freeze the game if you run it. Once security gets down below securityMinimum it will keep running the while loop without doing anything/giving up control. If you want it to keep weakening forever, you could just remove the if statement,

while(true){
weaken
}

If you want it to exit gracefully there are a few things you could do. Easiest would probably be to move the condition into the while statement,

while(sec > secMin){
weaken
}
AlconburyBlues Nov 7, 2024 @ 12:13pm 
Thanks for all your incredibly helpful tips. I try them out.
TheAncientOne Nov 8, 2024 @ 5:36pm 
Good tips from what I can see, to add, if you still have an issue, try adding an 'await ns.sleep(500); in your while loop, just to verify something is not bogging things down. I ran the script here without issue though..
/** @param {NS} ns */ export async function main(ns) { var target = "zer0"; var securityCurrent = ns.getServerSecurityLevel(target); var securityMinimum = ns.getServerMinSecurityLevel(target); while(true) { if (securityCurrent > securityMinimum) { await ns.weaken(target); } await ns.sleep(500); } }
and yes, I realize the issue seems to be with a specific in-game server, but, anything is possible - never have doubt at a problem. You could even put code in just for that specific host to only delay it some if that is the case...
Last edited by TheAncientOne; Nov 8, 2024 @ 5:39pm
AlconburyBlues Nov 10, 2024 @ 7:32am 
I actually found out later that the script was locking up the game once the security level for that server was at the minimum. I didn’t have anything in there to graciously end the script once it got to that point.
TheAncientOne Nov 10, 2024 @ 10:08am 
Originally posted by AlconburyBlues:
I actually found out later that the script was locking up the game once the security level for that server was at the minimum. I didn’t have anything in there to graciously end the script once it got to that point.
Nice, essentially skipping the code blocks and running at full cpu/loop throttle I imagine.
AlconburyBlues Nov 10, 2024 @ 10:24am 
LOL maybe. But each server has its own time limit between weakens. I think that was purposely done to prevent that. If it wasn’t a simulation, I’m sure the CPU would indeed be at 100%.
TheAncientOne Nov 10, 2024 @ 3:16pm 
Originally posted by AlconburyBlues:
LOL maybe. But each server has its own time limit between weakens. I think that was purposely done to prevent that. If it wasn’t a simulation, I’m sure the CPU would indeed be at 100%.
well, the scripts in-game can inherently run loose - that is why infinite loop can lock the game. Difference is the simulation is sand boxed and it limited to what resources are assigned (otherwise, we would all have bsod'd a time or 8) :lunar2019crylaughingpig:

I just meant that If the script is skipping the if block and no pause/sleep is there, because the checks are not what it is looking for (such as no longer above min level) then a sleep would need to be in place (or as you did, a script kill) to resolve it (especially in a while true block). I use sleep on a lot of mine just for that reason so they can run until requirements are met again. Depending on my script, I have some I run from multiple servers that do 1 job while others are watch dogs running from another keeping the security down and cash flow up. Then I have one I run from my home system that is essentially a worm that does them all but installs scripts to each target to run.

Anyhow, glad you have it sorted either way.
Last edited by TheAncientOne; Nov 10, 2024 @ 3:22pm
AlconburyBlues Nov 11, 2024 @ 9:44pm 
Very insightful thinking.:steamthumbsup: I will definitely have those ideas in mind as get further in the game and add stuff in. But for the time being, I'm just glad I discovered this gem of a game.:steamhappy:

(Taking a project management position several years ago might have been a nice bump in pay at the time. But I would easily take a pay cut if it means I can enjoy doing brain-work and improve my scripting/programming skills again.)
AlconburyBlues Nov 15, 2024 @ 8:24pm 
Originally posted by TheAncientOne:
...try adding an 'await ns.sleep(500); in your while loop, just to verify something is not bogging things down.

This might be a silly question. But I'm comparing this sleep method to other languages. Is that value (500) in milliseconds?
ƝƖƓӇƬMƛƦЄ Nov 16, 2024 @ 5:08pm 
Originally posted by AlconburyBlues:
Originally posted by TheAncientOne:
...try adding an 'await ns.sleep(500); in your while loop, just to verify something is not bogging things down.

This might be a silly question. But I'm comparing this sleep method to other languages. Is that value (500) in milliseconds?
Hello!

Yes, sleep is run in milliseconds in this game, but here's some quick tricks/references to help if you ever want to sleep for longer intervals like a minute or more:
const intervalMin = 60 * 1000; // 1 Minute const intervalTenMinutes = 10 * 60 * 1000; // 10 Minutes await ns.sleep(intervalMin); // Sleep for 1 minute

Hope this helps! :steamhappy:
TheAncientOne Nov 20, 2024 @ 3:58pm 
Originally posted by AlconburyBlues:
Originally posted by TheAncientOne:
...try adding an 'await ns.sleep(500); in your while loop, just to verify something is not bogging things down.

This might be a silly question. But I'm comparing this sleep method to other languages. Is that value (500) in milliseconds?
Sorry been letting my game idle, have been working a lot past few days. NiGhTMaRE I believe took care of it though, indeed is in milliseconds.
< >
Showing 1-14 of 14 comments
Per page: 1530 50