wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Tutorials > JASS/AI scripts tutorials
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 02-05-2005, 07:06 PM   #1
a thing
User
 
a thing's Avatar
 
Join Date: Mar 2004
Posts: 148

Submissions (1)

a thing has little to show at this moment (4)

Default Beginning JASS

NOTE: This tutorial if made for people who have been using World Editor's trigger GUI (Graphical User Interface) for a bit and know the basics of triggering. This is not a basic GUI trigger tutorial or variable tutorial, it's a basic JASS2 (Custom Script) tutorial.

JASS2, which is usually just called JASS, is the language used in Warcraft III. It's a rather limited language but I stick to it because of Bnet (Where else are you going to find a huge free game network?). Even though JASS is pretty limited, it's still gives you more control over the game than GUI triggers. Some reasons why JASS>GUI:

1. You have more control over conditions and loops.
2. You can use local variables.
3. You can use functions.
4. Not everything in JASS is in the GUI.
5. You can make the game run a lot smoother.
6. You can make your code take up less (digital) space.

To create a JASS trigger, simply make a new trigger in the Trigger Editor then go to Edit>Convert to Custom Text
Functions

Functions are the building blocks of programming. Basically, they're just a block of code that you can call (aka execute). Here's an example of a function that when called, will display the text "Hello world!" to all players for 30 seconds:
Collapse JASS:
function HelloWorld takes nothing returns nothing
   call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"Hello World!")
endfunction
A function always follows this format:
Collapse JASS:
function <function's name> takes *** returns ***
//{code that will be executed when the function is called}
endfunction
After "takes" is where you define what the function will "take in" when it's called. Anything taken in by a function can be used as a local variable (which will be explained later) inside of the function. To define what the function takes, put <type> <name> after "takes ". Example:
Collapse JASS:
function meh takes unit u returns nothing
To have a function take multiple values, just put a comma after the name of the taken value and then define the next.

If you don't want your function to take anything, simply put in "nothing" instead, like in the first example function I showed you.
The last part of the declaration is what type the function returns to the function it's called from. A function MUST return the type stated in the function declaration line as the last line of the function before "endfunction". Stumped? This example function might clear things up:
Collapse JASS:
function half takes integer x returns integer
return x/2
endfunction

The above function takes an integer, x, and returns half of x, sort of like a machine that needs a piece of wood to start, chops the wood in half, keeps one half, and spits out the other half on the other end. I know I could've just done x/2 instead of calling half but that wouldn't help you understand taking and returning, would it?
NOTE: Function names can't have spaces in them.

Calling

Unlike many other languages, in JASS2, you cannot just type a function name and the game will call it. You must put "call " in front of the function name, then parentheses around the parameters (what a function takes) of the function being called. Here's an example of calling a function called "meh" that takes nothing.
Collapse JASS:
call meh()
If you call a function that takes parameters, the passed parameters MUST be in the order they're defined when the function is defined. Here's an example for a function called ItakeAUnitThenAnInteger that takes a unit and then an integer:
Collapse JASS:
call ItakeAUnitThenAnInteger(gg_unit_hpea_0001,7)
A function can also be called as another's parameter, as long as it returns the type that the first function takes. When you do this, you do not need to put "call " in front of the function name but the parameters are still done in the same way. An example where IwantAunit takes a unit and HaveAUnit returns a unit:
Collapse JASS:
call IwantAunit(HaveAUnit())
Ifs

Ifs are pretty self-explanatory. If a condition is true, do actions. Wow that's really complicated! The syntax of an if statement is:
Collapse JASS:
if <condition> then
<actions>
endif
NOTE: When I say "<condition>" that can be just a boolean variable or a function that returns a boolean, not just a comparison.
Else

Else statements, like ifs are pretty self-explanatory. Else statements are used with in ifs. If the condition typed after "if " is false, do the actions under "else". Here's an example of an if with an else:
Collapse JASS:
if SomeInteger==1 then
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"SomeInteger is equal to 1")
else
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"SomeInteger is NOT equal to 1")
endif
Elseif

Elseifs are sort of like switch statements in advanced languages like C. Like else, elseifs are inside ifs. After "elseif " you put a condition then "then". If the condition for the if is false, then the game executes the code inside the first elseif found to be true. If neither the if or any elseifs are true then the game executes the code under the else if there is one. Here's an example of an if statement with elseifs and an else:
Collapse JASS:
if SomeInteger==1 then
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"SomeInteger is equal to 1")
elseif SomeInteger==2 then
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"SomeInteger is equal to 2")
elseif SomeInteger==3 then
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"SomeInteger is equal to 3")
else
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS,30,"SomeInteger is NOT equal to 1, 2, or 3.")
endif
WARNING: If you put an else before an elseifs, the elseifs will be ignored.
Loops

Loops are very useful and powerful. Loops keep on executing the code inside them until the condition after the exitwhen statement is met. The exitwhen statement must be inside the loop. I always put my exitwhen statements at the beginning of the loop for it's easier to read later. You begin a loop with a line that's just "loop" and end with "endloop". Here's an example from a little function that counts the playing players on a map that has two 6 player teams:
Collapse JASS:
    loop
      if TeamNumber==1 then
        exitwhen i>5
        set i=0
      else
        exitwhen i>11
        set i=6
      endif
        if GetPlayerSlotState(Player(i))==PLAYER_SLOT_STATE_PLAYING then
            set PlayerCount=PlayerCount+1
        endif
        set i=i+1
    endloop
WARNING: Loops without an exitwhen statement or an exitwhen statement that cannot be met will crash Warcraft III!
Local and global variables

In GUI, you can only use global variables, which can be set and used from any function. The only way to declare a global variable from WE is by getting up the variable dialog (Ctrl + B) in the Trigger Editor. Locals on the other hand, can only be used and set inside the function they're declared in. You declare a local variable by typing on a new line "local <type> <name>". If you want to set the local right away, you can put "=<value>" after "local <type> <name>". Locals must be declared before anything else in the function. Here's an example of a local unit called "u" that is set to gg_unit_hpea_0001 at declaration:
Collapse JASS:
local unit u= gg_unit_hpea_0001
The syntax for declaring a local array is "local <type> array <name>". You cannot set a default value for the array or anything like that. Here's an example for a local integer array called ia:
local integer array ia
NOTE: For some strange reason, you cannot have local code arrays.
Memory leaks

A memory leaks occur when a value is not set to a variable or a variable isn't destroyed so it just sits in the player's memory. Too many memory leaks can cause a lot of lag. Any type that extends a handle (any type declared in common.j, which is talked about next) can leak. The only types you really need to worry about are groups (unit groups), locations (points), forces (player groups), effects (special effects), and sounds, unless you have a trigger that runs a lot. In that case get rid of every single leak.

To get rid of leaks, you have to set returned values to variables, then destroy the variables. There's a nice list of functions to destroy variables at http://s91887676.onlinehome.us/World...p?showtopic=38.

After you destroy a variable, you should set it to "null".
Blizzard.j and common.j

I highly recommend that you extract Blizzard.j and common.j from war3.mpq in your Warcraft III directory with an MPQ archiver (MPQ2K and WinMPQ can be found at http://www.smempire.org/tutorials/mpq/). common.j contains all types and native functions for JASS. Blizzard.j is a bunch of functions that are used in the GUI. A lot of them leak or just return native values. When working with JASS you shouldn't use any BJ (Blizzard.j) functions that just return natives but use the natives themselves. If you use the functions that just return natives then they'll leak.
NOTE: JASS is case-sensitive.
NOTE: JASS is compiled from top to bottom so you cannot call a function that is defined below or you'll get a syntax error.

TIP: Putting "//" in front of something makes the rest of the line a comment.[/b]
__________________
I have left Warcraft and all other nonfree software.
(Have a nice life.)
Oh yeah, admins, learn to use &amp; isntead of & in XHTML source.
a thing is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 02-06-2005, 10:29 PM   #2
flamesforzuljin
back from the dead
 
flamesforzuljin's Avatar
 
Join Date: Aug 2003
Posts: 1,466

flamesforzuljin will become famous soon enough (31)flamesforzuljin will become famous soon enough (31)

Send a message via ICQ to flamesforzuljin Send a message via AIM to flamesforzuljin Send a message via MSN to flamesforzuljin
Default

cool, i was hoping someone would post a beginners jass tut.

i myself am looking to learn it...

good work

[MOVED]
__________________
Can't we find the minds to lead us
Closer to the heart?

flamesforzuljin is offline   Reply With Quote
Old 03-14-2005, 02:46 AM   #3
Erokke
 
Posts: n/a
Default

Interesting tutorial... but is there an easier way to get a list of functions?
  Reply With Quote
Old 03-14-2005, 02:20 PM   #4
ChoBo)H(StyleZ
User
 
Join Date: Mar 2004
Posts: 6

ChoBo)H(StyleZ has little to show at this moment (0)

Default

When you're working in GUI and want to define something as a specific unit that is already placed on the map, you can just click the unit...But when working in JASS, how do you find the correct unit ID to reference? ex: gg_unit_XXXX_0000 How do you find this name?
ChoBo)H(StyleZ is offline   Reply With Quote
Old 03-22-2005, 11:49 PM   #5
a thing
User
 
a thing's Avatar
 
Join Date: Mar 2004
Posts: 148

Submissions (1)

a thing has little to show at this moment (4)

Default

Set some unit variable to a unit selected from GUI then convert the trigger to JASS then copy and paste.
__________________
I have left Warcraft and all other nonfree software.
(Have a nice life.)
Oh yeah, admins, learn to use &amp; isntead of & in XHTML source.
a thing is offline   Reply With Quote
Old 04-22-2005, 05:35 PM   #6
Undead_Lives
Shades of Black
 
Undead_Lives's Avatar
 
Join Date: Jul 2004
Posts: 415

Undead_Lives will become famous soon enough (58)Undead_Lives will become famous soon enough (58)

Default

pretty cool tutorial. made quite simple.
__________________

Thanks to SkriK for making the image.
Give rep to people who help you.
Currently taking writing requests.
Undead_Lives is offline   Reply With Quote
Old 08-25-2007, 10:37 AM   #7
winterslayer666
User
 
Join Date: Aug 2007
Posts: 1

winterslayer666 has little to show at this moment (0)

Default Best toturial

Thank you athing, its the easierst and best Jass toturial ever made !!!
i made a account extra to say this^^
and: Is there a continue or have you made only beginnes?
winterslayer666 is offline   Reply With Quote
Old 10-04-2008, 07:11 AM   #8
k_soldier
User
 
Join Date: May 2008
Posts: 2

k_soldier has little to show at this moment (0)

Thumbs up

I'm new to jass very thanks to you. That explains a lot.
__________________
k_soldier is offline   Reply With Quote
Old 10-04-2008, 01:04 PM   #9
Vexorian
Free Software Terrorist
 
Vexorian's Avatar


Technical Director
 
Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)

Hero Contest #3 - 2nd Place

Default

Just next time try not to use more than one smiley per post.
__________________
Zoom (requires log in)Wc3 map optimizer 5.0
Someone should fix .wav sound in this thing.
Zoom (requires log in)JassHelper 0.A.2.A
Turns your simple code into something that is complicated enough to work.
Faster != more useful
Vexorian is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 02:10 AM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services