Andrewgosu
The Silent Pandaren Helper
- Reaction score
- 716
Basic JASS Tutorial
So, you want to learn the JASS magic? Just stick around and read this tutorial then. Hopefully, it will give a reboost to your already damaged braincells. I was just kidding. Maybe.
Anyway, I tried to make this as informatory yet still, fun, as I could. Proceed.
Before we get started, you must understand the basics. That's why I have listed a somewhat boring, but very important list of definitions, which you have to know in order to learn JASS. Do not skip reading that (Well, for your own sake, of course).
After you have read it, proceed to step 1.
Definition: function
- In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more statement blocks.
JASS:
function AddSpecialEffectLocBJ takes location where, string modelName returns effect
set bj_lastCreatedEffect = AddSpecialEffectLoc(modelName, where)
return bj_lastCreatedEffect
endfunction
"AddSpecialEffectLocBJ" - the name of the function.
"(takes) location where, string modelName" - the parameter list.
"(returns) effect" - the value it returns.
A function can take nothing and return a value.
A function can take a parameter and return nothing.
A function can take multiple parameters and return a value (Not multiple values!).
A function can take multiple parameters and return nothing.
"call AddSpecialEffectLocBJ(where, modelName)" - how to call a function. A function call.
Function is immediately ended, when it meets a "return". ("Skip Remaining Actions" in GUI)
"(takes) location where, string modelName" - the parameter list.
"(returns) effect" - the value it returns.
A function can take nothing and return a value.
A function can take a parameter and return nothing.
A function can take multiple parameters and return a value (Not multiple values!).
A function can take multiple parameters and return nothing.
"call AddSpecialEffectLocBJ(where, modelName)" - how to call a function. A function call.
Function is immediately ended, when it meets a "return". ("Skip Remaining Actions" in GUI)
Definition: native- Belonging to one by birth; "my native land"; "one's native language".
- Characteristic of or relating to people inhabiting a region from the beginning; "native Americans"; "the aboriginal peoples of Australia".
- A person who was born in a particular place; an indigenous person.
- As found in nature in the elemental form; "native copper".
Example:
JASS:
native AddSpecialEffectLoc takes string modelName, location where returns effect
Definition: constant
- Changeless: persistent in occurrence and unvarying in nature; "maintained
a constant temperature"; "principles of unvarying validity". - A quantity that does not vary.
- In mathematics and the mathematical sciences, a constant is a fixed, but possibly unspecified, value. This is in contrast to a variable, which is not fixed.
Example:
JASS:
constant integer bj_MAX_PLAYERS = 12
Definition: variable
- In computer science and mathematics, a variable is a symbol denoting a quantity or symbolic representation. In mathematics, a variable often represents an unknown quantity; in computer science, it represents a place where a quantity can be stored. Variables are often contrasted with constants, which are known and unchanging.
Definition: local
- Local means "in effect only in a particular context"; the relevant kind of context is a particular function execution, a particular buffer, or a particular major mode. It is the opposite of 'global'.
Example:
JASS:
function AddRandomInt takes integer i returns integer
local integer x = GetRandomInt(10, 20)
return i + x
endfunction
Whereas "x" is being a local variable of type integer.
Syntax for declaring local variables: local /variable type/ /variable name/ = expression (optional)
Syntax for declaring local variables: local /variable type/ /variable name/ = expression (optional)
Definition: global
- Values (or Parameters) that apply to the whole system.
- A variable designation that means the entire script can read that variable. Opposite of a local variable.
Example:
JASS:
function SetRandomReal takes real r returns nothing
set udg_tmpReal = GetRandomReal(10., 20.)
endfunction
Whereas "udg_tmpReal" is a global variable of type real, which is given a new value.
Note: Global variables have an "udg_" prefix!
Note: Global variables have an "udg_" prefix!
Step 1 - Preparing to learn JASS
Now, I assume(*) you have read the definitions list. Lets proceed.
Firstly, we need a professional JASS editing program, although, one can do JASS in notepad, too. But since, I assume(*) you're a total beginner in JASS, we need to have function search option and syntax highlighting.
The best(**) tool for that is JassCraft. Download it.
http://www.wc3campaigns.net/showthread.php?t=80105
Credits where due.
If you have searched for other tutorials, threads or posts about learning JASS and seen a "convert GUI into JASS and examine" suggestion, then I, I do not recommend that. It's wrong. You want to learn pure, fluid JASS, right? Forget converting GUI to JASS, then.
Open JassCraft.
The function search is a nice feature, which helps to learn faster. I highly suggest to learn the most common functions by heart.
The syntax check is a very useful tool, which helps to prevent us from copy pasting faulty code into World Editor.
And finally, the indent option. Learn to indent your code the correct way, for the sake of readability.
Proceed to step 2.
(*) Never assume anything.
(**) In my honest opinion.
Step 2 - Basic JASS
Well, no - I am not going to teach how to convert a GUI trigger and display a message to players. Lets start with something practical without being too hasty. Lets make a basic creep reviving function.
Start by clicking Ctrl+U or selecting Templates->New function. A template function without a name and parameter list should appear. Give the function a name.
TIP #1: Use descriptive names! Doesn't matter whether you're naming variables, function or something else.
Use capital letter to separate words. Avoid using numbers, slashes etc.
Use capital letter to separate words. Avoid using numbers, slashes etc.
I named my function "UnitReviveCreep", Unit - takes an unit, ReviveCreep - revives the taken unit. Simple, uh? Descriptive naming isn't that hard. Remember it!
After naming the function, we have to think what is the purpose of the function. My function will take an unit, x and y coordinates where to "revive" it. Lets do it! (Damn, it sounds so cliché)
... takes unit whichUnit - unit is the type of the variable the function takes, "whichUnit" is the name of it. One can name "whichUnit" whatever he wants, but I try to be as descriptive as I can be.
As you can see, parameters are separated with commas.
As you can see, parameters are separated with commas.
Now, you ask, why does it return a boolean. Well, because the unit is not a hero, we have to check, whether the unit is dead or alive in order to "revive" it. I mean recreating the same unit with revive.
Lets make an if/then condition to make sure the unit is not a hero and it's dead.
JASS:
function UnitReviveCreep takes unit whichUnit, real x, real y returns boolean
if (IsUnitType(whichUnit, UNIT_TYPE_HERO) == true) or (IsUnitType(whichUnit, UNIT_TYPE_DEAD) != true) then
return false
// The unit is a hero OR it is alive. Lets end the function.
endif
endfunction
Note #1: JASS uses double signs to do comparisons.
a == b is 'a is equal to b'
a != b is 'a is not equal to b'
a > b is 'a greater than b'
a < b is 'a is less than b'
a <= b is 'a is less than or equal to b'
a >=b is 'a is greater than or equal to b'
Note #2: When doing a "IsUnitType" check, you always have to compare it with a boolean. Otherwise, it will bug.
Note #3: JASS is case-sensitive. "or" does not equal to "Or", "and" does not equal to "And".
Tip #2: To view all the unit types, type "UNIT_TYPE" into the function finder.
a == b is 'a is equal to b'
a != b is 'a is not equal to b'
a > b is 'a greater than b'
a < b is 'a is less than b'
a <= b is 'a is less than or equal to b'
a >=b is 'a is greater than or equal to b'
Note #2: When doing a "IsUnitType" check, you always have to compare it with a boolean. Otherwise, it will bug.
Note #3: JASS is case-sensitive. "or" does not equal to "Or", "and" does not equal to "And".
Tip #2: To view all the unit types, type "UNIT_TYPE" into the function finder.
Now, having made the check, we need to create the same type of unit for the same player at given coordinates.
JASS:
function UnitReviveCreep takes unit whichUnit, real x, real y returns boolean
if (IsUnitType(whichUnit, UNIT_TYPE_HERO) == true) or (IsUnitType(whichUnit, UNIT_TYPE_DEAD) != true) then
return false
// The unit is a hero OR it is alive. Lets end the function.
endif
call CreateUnit(GetOwningPlayer(whichUnit), GetUnitTypeId(whichUnit), x, y, GetUnitFacing(whichUnit))
The function names are quite self-explanatory. "GetOwningPlayer" gets the owner of the unit, "GetUnitType" gets the units type, e.g, a footman. No need to explain "GetUnitFacing".
But something is a miss. The function returns a boolean, but, if the function passes the conditions, it will return nothing.
JASS:
function UnitReviveCreep takes unit whichUnit, real x, real y returns boolean
if (IsUnitType(whichUnit, UNIT_TYPE_HERO) == true) or (IsUnitType(whichUnit, UNIT_TYPE_DEAD) != true) then
return false
// The unit is a hero OR it is alive. Lets end the function.
endif
call CreateUnit(GetOwningPlayer(whichUnit), GetUnitTypeId(whichUnit), x, y, GetUnitFacing(whichUnit))
return true
// The unit was not a hero nor was it alive. The function was a success.
endfunction
Now it's complete. Your very own "custom script" ready to be inserted into your maps header.
How to call it? Just like any other function. Allow me to do some pseudo-code.
"call UnitReviveCreep(GetTriggerUnit(), xCoord, yCoord)". Just like that.
Proceed to step 3.
Step 3 - examples
A picture is worth more than a thousand words. Well, lets replace "picture" with "example". Aren't I clever, am I?
Example: if/then, if/then/else, elseif
JASS:
JASS:
JASS:
Example: Loop
JASS:
function Loop_Example takes nothing returns nothing
local integer index = 0
// "loop" to mark the beginning of the loop.
loop
// exitwhen (condition(s)) to exit a loop.
exitwhen (index > 50)
...
set index = index + 1
endloop
// "endloop" to mark the end of the loop.
endfunction
JASS:
function Loop_Example takes unit whichUnit returns nothing
loop
// A exitwhen condition, which deals with an unit's health.
exitwhen (GetUnitState(whichUnit, UNIT_STATE_LIFE) > 100)
call TriggerSleepAction(.25)
// A wait, to ensure the loop doesn't meet the execution limit. REQUIRED.
endloop
endfunction
That's enough for today, folks.
Thank you for reading this tutorial. Feedback is appreciated.