JASS: Basic

Tutorial By Im_On_56k

This tutorial includes:
Calling Functions
Local Variables
Making Basic Functions
Making and Using Comments
Using Global Variables


So lets start from the beginning.

Basic look at JASS

Make a new trigger with the event Map Initalization.
Convert it to custom text(JASS) by going to Edit-Convert To Custom Text.

It should now look like this.
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing endfunction //=========================================================================== function InitTrig_Untitled_Trigger_001 takes nothing returns nothing set gg_trg_Untitled_Trigger_001 = CreateTrigger( ) call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions ) endfunction
It may vary depending on what you named your trigger.
My trigger name was Untitled Trigger 001

We are now going to add an action to it that will display text to all players in
the game.
call DisplayTimedTextToForce( GetPlayersAll(), 30, "hello" )
An action is a function that calls a group of code to do something in the game.
So when we call our function we must put call before the function name as seen above.

call DisplayTimedTextToForce
The function DisplayTimedTextToForce takes 3 things, so we must give it 3 things.

GetPlayersAll() (playergroup)
30 (integer)
"hello" (string)

GetPlayersAll() is who it will be displayed to in this case it will display to all players.

30 is how long it will display the text in this case it will display it for 30 seconds.

"hello" is the string you will display in this case it will display hello.

All strings must have " around them to declare them as a string.

Now our trigger should look like.
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing call DisplayTimedTextToForce( GetPlayersAll(), 30, "hello" ) endfunction //=========================================================================== function InitTrig_Untitled_Trigger_001 takes nothing returns nothing set gg_trg_Untitled_Trigger_001 = CreateTrigger( ) call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions ) endfunction

(Image)

Local Variables

Now we are going to get into local variables.

Local variables are variables that are only used in the function they are declared in.

To declare a local variable it must be at the very top of the function.

To declare a local string we would put.
local string mystring
Local is what tells the game that you are declaring a local variable.

string is the type of variable you are declaring.

mystring is the name of the string you are declaring.

There are many different types of variables but I will list only the most common ones.
local string s local integer i local real r local boolean b local location l local group g

Those are the most commonly used variables.
You can also set a local variable when declaring it.

Example
local integer i = 1
This will declare i as an integer that equals 1.

Here is another example of setting a string when declaring it.
local string s = "hello"
Now lets use what we have learned in our trigger.

To refresh our memory this is what the trigger looked like.
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing call DisplayTimedTextToForce( GetPlayersAll(), 30, "hello" ) endfunction //=========================================================================== function InitTrig_Untitled_Trigger_001 takes nothing returns nothing set gg_trg_Untitled_Trigger_001 = CreateTrigger( ) call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions ) endfunction
Lets make a local string variable that equals hello.
local string s = "hello"
Make sure that is the very first line after your function.
It should now look like this.
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing local string s = "hello" call DisplayTimedTextToForce( GetPlayersAll(), 30, "hello" ) endfunction //=========================================================================== function InitTrig_Untitled_Trigger_001 takes nothing returns nothing set gg_trg_Untitled_Trigger_001 = CreateTrigger( ) call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions ) endfunction
Now lets set our DisplayTimedTextToForce to our local variable.
call DisplayTimedTextToForce( GetPlayersAll(), 30, s )
Your trigger should now look like this.
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing local string s = "hello" call DisplayTimedTextToForce( GetPlayersAll(), 30, s ) endfunction
Now lets use what we learned again and make a local variable that is an integer.

local integer i = 30
Now lets implant this into our trigger.
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing local string s = "hello" local integer i = 30 call DisplayTimedTextToForce( GetPlayersAll(), i, s ) endfunction //=========================================================================== function InitTrig_Untitled_Trigger_001 takes nothing returns nothing set gg_trg_Untitled_Trigger_001 = CreateTrigger( ) call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions ) endfunction

Now I will explain to you what our trigger does.

At Map Initalization it will display the text hello to all players for 30 seconds.

Functions

Now lets get into making functions.
A function is a group of code that processes something.

We will put our function into the map script.(the very first thing in the trigger
editor)

We will call our function GetPercent
Our function will take 2 integers and get the percent of them.
So it will divide them by each other and multiply by 100.

Heres how it will start.
function GetPercent takes integer i, integer i2 returns integer
Function is telling the game that GetPercent is a function.

GetPercent is the name of the function.

Takes is what the function is taking from you.

Here is an example.
call DisplayTimedTextToForce( GetPlayersAll(), i, s )

The function DisplayTimedTextToForce takes a playergroup, an integer and a string.
Which is this part.
( GetPlayersAll(), i, s )

Returns is what it is giving to you, the oposite of take.

This is used when setting a variable.

Example
local integer i = GetPercent(1,100)
This will set i to 1.

Now back to finishing our function. Note the above will not work yet.

function GetPercent takes integer i, integer i2 returns integer return i/i2*100

Return is what the code is returning as explained above.

* means multiply.
/ means divide.
+ means addition.
- means subtraction.


Now we must finish our function.

function GetPercent takes integer i, integer i2 returns integer return i/i2*100 endfunction
endfunction tells the game that the code for this function ends here.

Comments
// defines a comment in JASS, aswel as many other languages but lets just stick to jass.

Example
function GetPercent takes integer i, integer i2 returns integer
//This is a comment and will not have any effect on anything in the function.
return i/i2*100
endfunction



Using Global Variables
You have probally used Global Variables if you are experienced enough in the World Editor.
They are like local variables in jass but can be accessed in any trigger.

To use global variables in jass you must place udg_ before the global variable name.

Say we have a global variable named MyVar
To use it in jass we would make it this: udg_MyVar

Here is an example of using a integer global variable in jass.
call DisplayTimedTextToForce( GetPlayersAll(), udg_MyInteger, "hello" )
This will display hello to all players for the value of MyInteger.


By Im_On_56k

Click here to comment on this tutorial.
 
 
Blizzard Entertainment, Inc.
Silkroad Online Forums
Team Griffonrawl Trains Muay Thai and MMA fighters in Ohio.
Apex Steel Pipe - Buys and sells Steel Pipe.