A little help on scripting if you will? :)

Just started out here trying to get my head around this whole scripting thing and i was wondering if this piece of script during an attack would work it's in the continue part of my attack script

getCPULoad();
if getCPULoad()= getCPULoad(97%){
cancelAttack();
if getCPULoad()< getCPULoad(97%){
emptyPettyCash()

Most likely it won't I'm not sure i'm a total noob when it comes to scripting :p

Help is free!

Welcome to scripting! This can be the most fun and the most frustrating part of the game! :) There are a couple things wrong with your code but you've got the general idea.

First of all, you've got a couple of syntax errors. Your IF conditions need to be in brackets, you forgot some close braces and a semi-colon, and you have to use two equals signs when you're comparing stuff (one equals sign means you're setting something to a value. Also, indenting the contents of IF blocks makes your code easier to read:

getCPULoad();
if ( getCPULoad() == getCPULoad(97%) ) {
cancelAttack();
}
if ( getCPULoad() < getCPULoad(97%) ) {
emptyPettyCash();
}

Next, the function GetCPULoad() returns a float (number with a decimal point) so you only need to compare it with 0.97 instead of 97%.

if ( getCPULoad() == 0.97 ) {
cancelAttack();
}
if ( getCPULoad() < 0.97 ) {
emptyPettyCash();
}

OK, I believe that will compile now, but it probably won't do what you want it to. I'll explain why in my next post.

From Wartt Hog

Help is free!

It costs money to compile functions so the fewer times you use them the better. Why not save your CPU load in a variable at the beginning of the function so that you only need to call getCPULoad() once?

float cpu = getCPULoad();
if ( cpu == 0.97 ) {
cancelAttack();
}
if ( cpu < 0.97 ) {
emptyPettyCash();
}

Also, it's generally considered bad practice to use == when you're working with decimals. What if your CPU usage is 97.000001%? Chances are this script will never cancel it's attack. I think what you really mean here is not 97% but everything 97% or greater, right? This means you can use the ELSE clause of the IF statement to do stuff if the CPU usage is not >= 97% (i.e. if it's less than 97%).

float cpu = getCPULoad();
if ( cpu >= 0.97 ) {
cancelAttack();
} else {
emptyPettyCash();
}

Now this is a pretty cool script except for one big flaw: You can only emptyPettyCash() when the port is weakened. As soon as the port is weakened, the finalize function is called INSTEAD of the continue function and by that point you don't have to worry about your CPU usage because the battle's over and your attack port will no longer take any CPU cycles. So what you really want is:

Initialize:

Continue:
float cpu = getCPULoad();
if ( cpu >= 0.97 ) {
cancelAttack();
}

Finalize:
emptyPettyCash();

If you want, you could get rid of the cpu variable and just call the function in the IF condition because we're only calling it once now, but it doesn't hurt anything to use the variable and you might add more code again later. More importantly you might want to consider lowering the 97% value because it's quite possible to jump straight from 96% to 103%. The value you choose to use depends on your risk factor and the level of difficulty of the opponents you plan on attacking.

OK, you should be set! Never be afraid to ask questions (just maybe not in the main chat channel. Use "Help-0" or this forum)

Happy coding!

From Wartt Hog

Wow, That Will Fail So Badly, It's Not Even Funny

First off, getCPULoad returns a float value with the amount of CPU you ARE CURRENTLY USING, not the % out of your max CPU. To get your maximum CPU, you need to take that value, and divide it by your maximum CPU points, like this:

//------------------
float percent = getCPULoad()/getMaximumCPULoad();
//------------------

and, in your context, call it like this:

//------------------
if(
percent>=0.97
){
cancelAttack();
}
//------------------

Now, like Warthog said, you need to call the emptyPetyCash(); function in 'Finalize', while the above function/variable set I've posted goes under 'Continue'...

Also, whenever you can set a function as a variable, do so. Just make sure you designate it right ('string' for text of IPs, 'float' if it could have a decimal, and 'int' if it will never have a decimal (use intValue(float to be made an int); if the value needs to be made into a whole number))

Next, whenever you're making a script for personal use, just put your IP directly into it, instead of using the costly getSourceIP(); and getMaliciousIP(); -- it cuts a massive amount of money off of the cost.

Lastly, remember you can put comments to yourself in the source file, by doing a double forward-slash (//). That is a single-line (as in no-returns) comment. for longer comments, and removing large sections of code that you may want later on, put a "/*" at the start, and a "*/" at the end. Basically, Comments work the same here as they do in CSS and Javascript.

Strike me, and I'll Chill You;
Near me, and I'll multiply;
Fear the Blue `j`

Wow

Wow thanks for the comments guys ^^

and also the pretty positive feedback on my main idea wit hthis script =D

I'll be sure to take it all into account off course, and I also recently started learning C# online just to try and understand this in-game language better haha :p i know i'm whacked :p

C# rox my sox

I love C# but know that it's significantly different than the hackwars C. The syntax is similar and it's always useful to learn another language, but if you're only learning it to understand hackwars, I recommend looking for some tutorials on just plain old C.

From Wartt Hog

Bleh7777's picture

HackScript is so watered down

HackScript is so watered down that it doesn't really matter. As long as you know how to use curly braces, you're fine.

Come visit BlehCo at 2.718.2.818 for free challenge answers!

But what about...?

Don't forget the extended order of operations, short-circuit operators, expression casting, scope, variable initialization, boolean logic, assignments in an expression, ... the list goes on. A simple programming language is still a language and there's a lot to learn that a veteran like yourself can forget, B7. I was trying to explain QBasic to a young friend the other day and I started to realize how much of the simple stuff people don't know.

...Unless you're talking about Scheme in which case you only need to know how to use brackets. :)

How about Whitespace? To use that language properly you only have to know how to use spaces, carriage returns and tabs because everything else is comments!

From Wartt Hog

now your talking semantics

now your talking semantics ;)

Nothing like a compiler chat just two plain ol syntax and semantics gaff, whilst figuring if a LR parser will do the trick ;)