JASS - JASS Crash Course - A Basic JASS Tutorial

Andrewgosu

The Silent Pandaren Helper
Reaction score
715
JASS CRASH COURSE

The purpouse of this tutorial is to teach the very basics of JASS, which gives you a solid foundation from where to develop your JASS skills and knowledge.

REQUIREMENTS:
- JASS NewGen Package (used to help to declare globals)

RECOMMENDATIONS:
- JASSCraft 1.1.3 - A nice JASS Editor with function lists and templates.

TABLE OF CONTENTS

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.


funcs.jpg


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. "//".

func_0021.jpg

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.


func_0001.jpg


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!


func_0002.jpg


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.


func_0003.jpg


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.


func_0005.jpg



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.


func_0006.jpg


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.


func_0007.jpg


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.


func_0008.jpg


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".


func_0009.jpg


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.


func_0010.jpg


To declare global variables, you must first create a globals block, which starts with the keyword "globals" and ends with the keyword "endglobals".


func_0011.jpg


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.


func_0012.jpg


To declare a local variable array, you have to use the keyword "array" after the type of the variable.


func_0013.jpg


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.

Addition
Marker
- "+"

Subtraction
Marker -
"-"

Multiplication
Marker -
"*"

Division
Marker -
"/"


func_0014.jpg



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".


func_0015.jpg


The function returns true if both of the comparisons evaluate to true.


func_0016.jpg


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.


func_0017.jpg


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.


func_0018.jpg


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.


func_0019-1.jpg


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.


func_0020.jpg


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.

Moving from GUI to JASS by Rheias
Writing Efficient Code by phyrex1an
JASS Introduction (a lenghty tutorial) by Vexorian
So You Want to Learn JASS by emjlr3

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.

Anyway, this ends our little tutorial!


Happy mapping!

Andrewgosu, your favorite Pandaren
 

cr4xzZz

Also known as azwraith_ftL.
Reaction score
51
Very useful but please do not use photobucket for image uploading. As you can see one picture was already removed (bellow Division Marker - "/") so it's better to be tinypic.com or something else. Good job on the tutorial.
 

BRUTAL

I'm working
Reaction score
118
i liked this tut
it was easy to understand for someone who is just learning jass like myself :thup:
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
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"?).

You should also mention that functions that don't take any arguments have "nothing" as a keyword, and they still have to have an empty set of parenthesis.

NOTE #4: All local variables must be declared in the beginning of the function!

... before any actions are run. (just to be clear)

NOTE #5: Array index values in JASS have a limit of 8192, any larger values will not work.

... variable[0] to variable[8191], though the index 8191 is known to have bugs related to it, making 8190 the highest, safest index.

In the loops and conditionals section, you should also put that you can have nested if-then-elseif-else blocks, nested loops, and multiple "exitwhen"s.

The key to learning any programming language is simple: [del]just[/del] practic[del]s[/del]e! It might sound dull and boring, but eventually you will find yourself [del]from[/del] writing more and more complex scripts.

Overall, a very basic and clear introduction to JASS. It addresses all the basic concepts and gives new JASSers tips on how to use each one, as well as teaching them proper ways to write their code. I especially like the formatting, as it makes it very readable and easy to look at. There's always room for more JASS tutorials!
 

CaptDeath

New Member
Reaction score
103
ima read this stat cause Andrewgosu wrote it so that means its overflowing in levels of pew
and i think im ready for Jass
 

Andrewgosu

The Silent Pandaren Helper
Reaction score
715
i think we also need a vJASS Crash Course..
just the basics..

like:
libraries / scopes
private/public functions
global config blocks.

Maybe, just maybe.

Though, intermediate JASSers don't really need that much guidance.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top