Basics of Hackscript
From ElectronicWar
Everything you need to start scripting in Hack Wars.
Contents |
[edit] Variables
You will find variables in every programming languages. See variables as a box that contains data. There are 4 kinds of variables in Hack script:
- Integers
- Floats
- Strings
- Booleans
Making (declaring) variables.
Making (aka declaring) variables is very simple. To declare a variable, type the variable type, name and the value you want it to contain.
Example of declaring an integer.
int myInt = 5;
Make sure you always add a ; at the end of every line, with a couple of exclusions ("if" and "while Basics_of_Hackscript#If_Statements_and_While_loops).
You can now dynamically change myInt by using functions and maths. An example of changing it:
int myInt = 5; myInt = myInt * 2;
myInt now contains the number 10.
Integers
An integer can only contain a whole number, for example 53. Don't try to assign a decimal number to an integer, because it just won't work. Please note that the integer in hackscript is shortened to int.
Floats
A float can contain decimal numbers, for example 10.325. Floats are used with, for example, money.
Strings
Strings can only contain text. You can add other variables to a line of text by using +.
An example:
string var = "World"; string anotherVar = "Hello " +var;
The string anotherVar now contains the text: "Hello World". You can also put variables in between the text by doing for example:
int apples = 6; string howManyApples = "There are " +apples+ " apples in the basket.";
The string howManyApples here will say: "There are 6 apples in the basket."
Booleans
A boolean can contain either true or false. Booleans are not used very often, but they can prove useful in some situations.
Declaring/using booleans.
To check if something is true or false, you can use the following so-called "Logical Operators".
http://www.electronicwar.net/w/images/6/65/Logical_operators.png
Example:boolean trueOrFalse = 756 =< 34;
This boolean would be false, because 756 is not less nor equal to 34.
[edit] If Statements and While loops
If Statements
"If" is used to do something when the boolean between the () is true. When it's true it will execute whatever you have between the brackets { }
An example:
int myVar = 4; int varTwo = 5; string yesOrNoes = ""; if(myVar!=varTwo){ yesOrNoes = "yes"; }else{ yesOrNoes = "noe"; }
In this case, the string yesOrNoes will say "yes", because myVar and varTwo are not equal.
While Loops
"While" is used in exactly the same way as If, but it keeps executing whatever is between the {} until the value turns false.
Example use of While:
int timer = 3; while(timer>0){ timer = timer - 1; }
This loop will keep counting down until the timer reaches zero, because then it will turn false and the loop stops.
[edit] Functions
Functions can be used to calculate something, get a value, and more.
Using Functions in Hack Script
This is an example of a bank script.
float money = getAmount(); string myIP = getSourceIP(); lowerDeposit(myIP,money); message(myIP,"$" +money+ " has been deposited on your bank.");
Whoa, what's all this? Let me break it up for you.
The function getAmount() can be used in exactly the same way as a variable. It return the float of how much the player is trying to deposit.
The function getSourceIP() returns the IP of the player using the script as a string.
The function lowerDeposit() is different, because it's not used to get a value, but to do something. This function requires two values: the IP of the player you want to deposit it to, and the amount of money. These values are separated by commas (,).
message() is of the same type of lowerDeposit(). It will put the string in the ~GAME MESSAGES tab of the player with the given IP.
Tip: Whenever you need to use the same function that returns a value twice, declare it to a variable just like I did with "money" and myIP". This will make the script more efficient, and you will save compiling money.
To get an understanding of what more of the functions do, visit: Functions and http://www.hackwars.net/manual/hackscript-api
[edit] Syntax Error?
Take the following steps:
- Make sure that you've added ; after every line, except "If"s and "While"s
- Make sure that you've added enough closing brackets when you have a lot of ifs or whiles inside of each other.
- When putting a function in a function, make sure you've added enough )s at the end.
message("123.456.7.890","Greetings from "+getSourceIP();should bemessage("123.456.7.890","Greetings from "+getSourceIP()); - Make sure that you've declared the variables correctly.
float rofl = "Funny message!"is impossible, because you can't put text into a float. - Make sure that your strings (not string variables) are in quotes ("text here").
- When declaring a float, make sure that you always add a decimal number after the comma, even if there isn't any (
float var = 100.0).
Happy scripting!

