HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Jass for the programmer.

11-23-2003, 02:26 AM#1
weaaddar
This is for anyone whose ever programmed before.

Common.j and blizzard.j are the equivalant of the API of Jass you can extract them from your war3.mpq

Jass is a very odd langauge. Its partially object oriented, in that there are objects, but you can not create your OWN OBJECT TYPES.
This is a HUGE reason many people simply find jass not that useful. (Except for on the supreme optimization front).

They're wrong of course. Jass is much freer, and allows you to approach the problem from multiple angles. It allows cleaner and less clunky then the capal tunnel syndrome clicking yourself in to a comma triggering. They're many Jass only functions, but lets put them aside.

1
Lets first start off with the Units of Programming, Logic and Flow Operators.
Jass doesn't force you to use the hungarian standard of naming convention. This is not a good thing for readability. But I'll be using it in my demo.
Code:
function myFunction takes [i]datatype inname[/i], (use the comma operator for each thing the function is passed) returns [i]datatype[/i] 
return [i](value)[/i]
endfunction
Unlike java or other programming language you do not need to use the bracket operators {} each jass function is selfcontained between its start and end point, much like pascals Begin and End operators.
return will return a value and/or exits this function. This is useful for functions which may have results you don't want to reach...
The datatypes are all listed in the common.j file. Jass function CAN NOT BE PASSED OR OUTPUT AN ARRAY. Your inname variable acts like a local variable would.
And that brings up our next buddy the local variable.
Code:
local datatype localvar
[i]set localvar=new value[/i]
I'm seperating the defining and setting just so I can knock off two birds with one stone. Local variables are defined at the begining of your function and come before any other operators. You can assign a local right away by going local datatype localvar=new value.

Jass's assignment operator is the set varname= operator. Jass does not have the accumulators or operator assignment functions (++,--,+= etcetra).
You'll note that jass does not use semi-colons. Jass's delimiter is white space.
Lets move on:
Code:
loop
exitwhen boolean [i]true[/i]
endloop
Jass has one all purpose loop. It can be a top test or bottom test loop depending on where you put the exitwhen operator. Exitwhen takes a boolean either a logic statement (X==Y),the words true or false, a boolean variable, or a function which which returns a boolean.

Finally the last in the series of control:
Code:
if [i]boolean[/i] then
elseif boolean then
else
endif
then must proceed the boolean. You can have an infinite amount of statements in between it.
elseif acts just like if, but is only reached if the conditions of the if return false.
and else will run if no other conditions are met.

Finally to actually call your function is done like so
Code:
call myFunction(myvar)
set myvar=myFunction(myvar2)
call myFunction2(myFunction(myvar))
The call operator will simply call it.The Set operator can assign a variable to the value of the return. You can also use your function in place of the datatype it would return.


A note on arrays:
Arrays in jass are defined with a cap on amount of elements, This does not limit where you assign your elements in the array. You can assign your first value at 0 or 9999999 if you so chose...
Arrays in Jass are 1d, but you can make them N-dimensional the same way that C does them. In my case I ussually define a function called Index which will take the amount of values i want and use this formula
Where ELimit is the maxium value this dimension of the array will reach.
Code:
  
2d array: X*Ylimit+Y
3d array: X*Ylimit*Zlimit+Y*Zlimit+Z
Nd array: 1rstE*2ndElimit*3rdElimit...*NthELimit+2ndE*3rdELimit...*NthELimit+...+N-1thElimit*NthElimit+NthElimit