- JASSCraft 1.1.3 - A nice JASS Editor with function lists and templates.
TABLE OF CONTENTS
[INDENT]Chapter one – functions
Chapter two - variables
Chapter three - operators
Chapter four - conditionals, loops
Chapter five - conclusion and suggestions
CHAPTER ONE - FUNCTIONS
New concept – function Function is a portion of code within a larger program, which performs a specific task.
Welcome to your very first function in JASS, called „HelloWorld“. As you've guessed already, the task of this function is to display a „Hello, world!“ text on the screen. Lets get started by taking a closer look at the example and study the syntax plus key elements of declaring a function.
A function in JASS must start with the keyword "function" and end with the keyword "endfunction". This tells Warcraft where the function starts and ends.
Once, you have started declaring the function you must add a name to it. It does not matter how you name your functions, but for the sake of better understanding it's wiser to give them explanatory names. I'll name my function "DisplayStatistics". NOTE #1: Two functions cannot have the same name!
TIP #1: Use explanatory function names, which relate to the function task.
TIP #2: Normally, JASS function naming convention says to capitalize the first letter of every new word. e.g MyTestFunction
TIP #3: To make a comment, use the double forward slashs. "//".
New concept - argument Argument is a reference or value that is passed to a function.
After naming your function, carry on by adding the argument list - the values or references needed by the function in order to complete its task. The argument list must start with the keyword "takes". The example function "HelloWorld" did not take any arguments, so it used "takes nothing". However, lets add a unit argument to our function.
The first keyword after "takes" denotes which type of parameter the function needs to perform its task, in our case "unit". The next word is the reference name of the argument, which is used inside the function. It does not matter how you name the references, "whichHero" could have been just "a". However, short and explanatory is the best way.
NOTE #2: Functions can take more than one argument. Multiple arguments must be separated with a comma!
As the function definition says, it's a piece of code with a specific task. After the task has been done, the function must return the outcome of it. For example, when you go to the store and buy something, the cashier's task is to charge (takes) you the money and return (returns) any change.
That means, once our done with the argument list, it's time to finish the function decleration altogether with a "returns" keyword and the variable type it returns.
To return a value or reference inside a function, you must use the "return" keyword. Do not mix that up with the keyword "returns", the latter is used in the function declaration process.
NOTE #3: A function ends its task, when it encounters a "return" keyword.
New concept - function call A function call is an expression containing a simple type name and a parenthesized argument list.
We're nearly done with learning the basic knowledge about JASS functions. The last vital information you need to know is how to make your function perform its task - how to call a function.
To make your function run its actions you must place the keyword "call", separated by a space, before your function name.
Next, you must add parathesis right after the function name and pass the arguments to the function (remember the "takes"?).
CHAPTER TWO - VARIABLES
New concept - variable An object of a specified type whose value can be changed.
New concept - local variable A variable that exists only inside a particular function, not affecting anything outside the function.
To declare a local variable, you must first add the "local" keyword, which is followed by the type of the variable. Lastly, you need to write a reference name to it.
Local variables can also be declared with a initial value, using the "=" sign. Note how the keyword "call" is missing, though, we are still running the function "GetRandomInt".
Lastly, if you don't need to declare a local variable with an initial value, but instead want to assign it later, you can use the "set" keyword.
NOTE #4: All local variables must be declared in the beginning of the function!
New concept - global variable A variable that can be accessed by all parts of the code.
To declare global variables, you must first create a globals block, which starts with the keyword "globals" and ends with the keyword "endglobals".
Global variables do not need a keyword in front of them, unlike local variables.
However, as same as with local variables, you can declare them with an initial value and change the content later with the "set" keyword.
TIP #4: JASS global variable naming convention prefers block letter names, to distinguish them from local variables.
New concept - variable array A set of sequentially indexed variables.
To declare a local variable array, you have to use the keyword "array" after the type of the variable.
To assign a value or reference, you must choose the index which will hold it, using the "" sign.
The same applies to global arrayed variables declaration.
NOTE #5: Array index values in JASS have a limit of 8190, any larger values will not work.
NOTE #6: Initial values are not possible when declaring arrayed variables.
CHAPTER THREE - OPERATORS
New concept - operator A symbol representing a operation.
New concept - assignment operator An operator which assigns a value to a variable.
Marker - "="
New concept - arithmetic operators Operators, which perform arithmetic operations.
Marker - "+"
Marker - "-"
Marker - "*"
Marker - "/"
NOTE #7: Multiplication and division operations have higher priority than addition and substraction.
NOTE #8: You can use parenthesis to give a group of operations a higher priority.
New concept - Relational and equality operators Operators, which give a boolean result after operation.
Relational operators are needed, when you want to compare two (or more) variables to determine your further actions.
expression equal to expression Marker - "=="
NOTE #9: Do not mix this up with assignment operator "="!
expression not equal to expression Marker - "!="
value greater than value Marker - ">"
value less than value Marker - "<"
value greater than or equal to value Marker - ">="
value less than or equal to value Marker - "<="
To join a group of operations together, you can use the keywords "and" and "or".
The function returns true if both of the comparisons evaluate to true.
The function return true, if either of the comparisons evaluate to true.
CHAPTER FOUR - CONDITIONALS, LOOPS
Now, we've come this far that we know how to declare functions and variables and know how to do some basic operations. The next step would be learning some basic conditional statements and loops.
New concept - conditionals A specific type of statement which performs something dependant upon the result of an expression.
The syntax for declaring an else-conditional is following: first comes the keyword "if", which is followed by the condition which needs evaluation. After that comes the keyword "then". All conditionals end with the keyword "endif". Between the keywords "then" and "endif" belong the actions which will be run depending on the evaluation.
The if-else-conditional is just an expanded if-conditional, with an added "else" keyword. The actions between "else" and "endif" will be run when the first condition evaluated differently than wanted.
The elseif-conditional is kind of like an expanded if-else. Instead of keyword "else" you must use "elseif" which is followed by a condition. After the condition, you must use "then" keyword, just like with the regural if-condition.
New concept - loop A piece of code that performs a series of instructions repeatedly until some specified condition is satisfied.
Loops are useful, when you need a function to do it's task multiple times.
To declare a loop, you must use the keyword "loop" to start and the keyword "endloop" to end it. To escape the loop, you must use the keyword "exitwhen" together with a condition. When the condition is satisfied, the loop is ended.
In the example loop, we use an enumerator variable, which is increased every loop iteration. Once the variable is greater than 5, the loop ends.
NOTE #10: A loop which conditions are never satisfied will crash Warcraft, so be careful.
CHAPTER FIVE - CONCLUSION AND SUGGESTIONS
This was the very basic of JASS. Without knowing the information this tutorial explained, it would be very difficult to learn more advanced things and develop your skills. If anything this tutorial explained is left unclear to you, take your time to read it again to graspe the idea.
If you feel like you're ready to move on, here's a list of little bit more advanced tutorials, which recap some of the things this tutorial explains and teach new knowledge.
The key to learning any programming language is simple: practise! It might sound dull and boring, but eventually you will find yourself writing more and more complex scripts.
In addition, when learning JASS, use the "Convert to Custom Script" action in World Editor to turn your GUI triggers to JASS. Though it's a very unefficient way to learn JASS, it helps you to learn Blizzard functions, used by every script out there.