Wc3C.net

Wc3C.net (http://www.wc3c.net/forums.php)
-   JASS/AI scripts tutorials (http://www.wc3c.net/forumdisplay.php?f=650)
-   -   Beginning JASS (http://www.wc3c.net/showthread.php?t=71484)

a thing 02-05-2005 08:06 PM

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]

flamesforzuljin 02-06-2005 11:29 PM

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

i myself am looking to learn it...

good work

[MOVED]

03-14-2005 03:46 AM

Interesting tutorial... but is there an easier way to get a list of functions?

ChoBo)H(StyleZ 03-14-2005 03:20 PM

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?

a thing 03-23-2005 12:49 AM

Set some unit variable to a unit selected from GUI then convert the trigger to JASS then copy and paste.

Undead_Lives 04-22-2005 06:35 PM

pretty cool tutorial. made quite simple.

winterslayer666 08-25-2007 11:37 AM

Best toturial
 
Thank you athing, its the easierst and best Jass toturial ever made !!! :emote_thumbup2: :emote_thumbup2: :emote_thumbup2:
i made a account extra to say this^^
and: Is there a continue or have you made only beginnes?

k_soldier 10-04-2008 08:11 AM

I'm new to jass very thanks to you. That explains a lot.:emote_thumbup: :emote_grin: :emote_cool: :panda:

Vexorian 10-04-2008 02:04 PM

Just next time try not to use more than one smiley per post.


All times are GMT. The time now is 07:42 AM.

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