JASS: Basic

Im_On_56k

Hm...
Reaction score
116
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.
Code:
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.
Code:
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.

Code:
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.
Code:
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

attachment.php


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.
Code:
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.
Code:
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
Code:
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.
Code:
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.
Code:
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.
Code:
local string s = "hello"
Make sure that is the very first line after your function.
It should now look like this.
Code:
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.
Code:
 call DisplayTimedTextToForce( GetPlayersAll(), 30, s )
Your trigger should now look like this.
Code:
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.

Code:
local integer i = 30
Now lets implant this into our trigger.
Code:
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.
Code:
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.

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

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

This is used when setting a variable.

Example
Code:
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.

Code:
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.

Code:
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
JASS:
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.
Code:
call DisplayTimedTextToForce( GetPlayersAll(), udg_MyInteger, "hello" )
This will display hello to all players for the value of MyInteger.


By Im_On_56k
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
Overall it is good, hopefully it will give a little push in the right direction for new jassers.

Just want to point out one thing:
Im_On_56k said:
Code:
Function GetPercent takes integer i, integer i2 returns integer
Return i/i2*100
Endfunction
Nothing wrong with the function as it is but the divesion will be calculated as a integer divesion, ie 1/3 = 0 and other error vaules.
If you want a integer return i suggest
R2I(i/I2R(i2))*100
if you change it to a real return
(i*100.)/i2

Also remember that jass is case sensetive, it is function, endfunction and return.
Thats the quind of errors you get when writing in Notepad ^^
 

PB_and_J

level 85 anti-spammer
Reaction score
41
one thing:

I still don't know what this is for //========== ==

and why some lines start a few saces after others

:banghead: (realy I don't know anything about anything about programing :(

However, the tut is nice, now I learned alot from it:).
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
// This is a comment. Whatever comes here is just "decoration" and will be ignored
// Some people strongly feel that comments are a must on any progam
// Others equally strongly feel that the source explains itself

As for the spaces, that's indentation, and greatly improves readability.
As an example, every now and then, someone posts some trigger code without "code" tags around.
Unfortunately, there's nested IFs... after some reading, you don't know anymore what action is in what then or else, or even what if.
Use it, do it, always.
 

SD_Ryoko

Ultra Cool Member
Reaction score
85
Like dis. Heh heh.

PHP:
function Display_Maps()
{
	global
	$script, $username, $userid, $administrators, $ptitle, $HTTP_REFERER,
	$mode, $genre, $page,  $view, $sort,
	$maptypes, $mapmodes, $mapsort;

	if(isset($view)) $sqlquery="SELECT * FROM maps WHERE mapid='$view'";
	else {
		$sqlsort="mapid DESC";
		if(isset($sort))
			{
			if($sort=="1") $sqlsort="mapid ASC";
			if($sort=="2") $sqlsort="downloads DESC";
			if($sort=="3") $sqlsort="author ASC";
		}
		if(!isset($mode)) $mode=0;
		if(!isset($page))  $page=1;
		$perpage=15;
 

XXXconanXXX

Cocktails anyone?
Reaction score
284
This looks alot like and works the same way as Vexorian's tutorial, but doesn't teach half as much, and doesn't grasp the whole concept of JASS.

Why did you make this?
 

PB_and_J

level 85 anti-spammer
Reaction score
41
AceHart said:
As for the spaces, that's indentation, and greatly improves readability.
1 final question: does it make a difference how many spaces, or indentations you have (1,2,3,4 etc), or does it have to be a certain number?
 

Sargon

New Member
Reaction score
83
PB_and_J said:
1 final question: does it make a difference how many spaces, or indentations you have (1,2,3,4 etc), or does it have to be a certain number?

It makes no difference.
 

Im_On_56k

Hm...
Reaction score
116
This looks alot like and works the same way as Vexorian's tutorial, but doesn't teach half as much, and doesn't grasp the whole concept of JASS.

Why did you make this?
Because I felt like it?
Take a look through this thread, there is more than 1 tutorial for some subjects.

I made this in 20 mins and I will be adding onto it.
 

XXXconanXXX

Cocktails anyone?
Reaction score
284
Yes, but you could've made it more original.

You explain it almost exactly as Vexorian did, but left out major portions and didn't grasp the whole concept, just took tidbits from Vexorian's explanation and through it together.
 

Darg

Administrator
Reaction score
49
conan stop throwing accusations around, he wants feedback not snide remarks.

read what he said in the first post:

This is the end of the tutorial, I made this in about 20 mins and I plan to hopefully add more to it later on.

my feedback:

its travelling along well so far, but there are some concerns on my part about the structure. It is vitally important that, when writing any tutorial, that you must show the reader what the end result of the tutorial is at the beginning of the tutorial. For example, if I were to create a similar tutorial to yours, I might say "This tutorial teaches you how to display a text message in the game using JASS". For this I would give an in-game screenshot of some text displaying on the screen and I might put a peasant on the map for good humour. Next, I would outline what JASS concepts I am going to teach in this tutorial, which for me would be something like:

- this tutorial will teach you how to write a basic JASS trigger using local variables and calling functions

After that I would start getting into the nitty-gritty detail.

- writing a basic trigger
- adding local variables
- calling functions

Finally, finish off the tutorial by linking to a map which contains the code that you have just been discussing.


Also, I would personally put the memory leaks section in a completely different tutorial. If you are trying to discuss the basics of JASS it is not the best idea to confuse the reader with memory leak discussions. Memory leaks occur regardless of whether you are GUI coding or JASS coding, so perhaps the better option is to make another tutorial discussing the impact of memory leaks and how you would combat them.
 

Darg

Administrator
Reaction score
49
hey I never saw you had an image attached to the first post, sorry about that! Guess I'm blind...

:)
 
W

Warnicro

Guest
I liked the tutorial better than vexorian's tutorial (no offense against vexorian) but this was easier somehow.

in my oppinion you did a good job, cause I actually pulled it off to make my first jass, I added some things and I even ended up making a cinematic with a few game texts (pre set, with variables :p) and made a small story and made it end again as well with a victory condition.
I thank you dearly for the tutorial, cause I really learned some from it, and I hope you continue, so I can learn some more.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top