HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Understanding JASS

11-10-2008, 02:34 AM#1
toofless
Hi. I've recently started learning about map-making and as I want to include a particle system into my map I need to learn how to use JASS.
I was reading the tutorials while are mostly extremely helpful. Vexorians in particular helped me understand a lot and I feel I am starting to grasp it.

There is one point in his tutorial where I got lost. The part about function's taking and returning. I'm still unsure when it is necessary to take and return and what their usess are.

For example, mid way through the tutorial he started using a function 'Msg' to shortcut a function to message player 1 (or 0 in JASS xD).

EDIT: Oops it is later he specifies Player 1, still....

Collapse JASS:
function Msg takes string s returns nothing
    call DisplayTextToForce( GetPlayersAll(), s )
endfunction

function ShowMessage takes nothing returns nothing
    call Msg("This is a message!")
endfunction

function ShowSum takes integer a, integer b returns nothing
 local integer r=a+b
    call Msg("r= "+I2S(r))
endfunction

function Trig_JASS_test_Actions takes nothing returns nothing
   call ShowMessage()
   call ShowSum(33,45)
endfunction

Now why does function Msg take String s and where does String s go as I don't see it used anywhere else.

Would be good if someone could explain and also I apologise if this is the wrong place to post.

Thanks.
11-10-2008, 02:40 PM#2
Anitarf
Quote:
Now why does function Msg take String s and where does String s go as I don't see it used anywhere else.
A function argument is like a local variable except that you can define it's value when you call the function instead of in the function itself. As a local variable, it may only be used within the function.
11-12-2008, 08:42 AM#3
toofless
Ahh I think I understand. A sort of way to streamline your code?
Are there limits to when a function must/must not use arguments or is it solely for when the user needs to use them?

So a return is like the opposite, it sets a variable?
11-12-2008, 10:00 AM#4
Anitarf
Quote:
Originally Posted by toofless
Ahh I think I understand. A sort of way to streamline your code?
Are there limits to when a function must/must not use arguments or is it solely for when the user needs to use them?
Functions that are used as trigger actions must take nothing and return nothing. Functions that are used as trigger conditions or as filters must take nothing and return a boolean.

Quote:
So a return is like the opposite, it sets a variable?
Pretty much. Basically, the arguments and the return value are ways for a function to communicate with the code that calls it. Parameters are the input and the return value is the function's output, which you can either care about or not.
Collapse JASS:
globals
    region reg
    unit un
endglobals

function Foo takes unit u, real x, real y returns boolean
    call SetUnitX(u, x)
    call SetUnitY(u, y)
    return IsUnitInRegion(reg, u)
endfunction

function Bar takes nothing returns nothing
    local boolean b
    call Foo(un, GetUnitX(un)+100.0, GetUnitY(un)+100.0) //we just call the function, not caring about the return value
    set b = Foo(u, GetUnitX(unit)+100.0, GetUnitY(unit)+100.0) //we also get the function's return value and store it to b
    if b then
        call BJDebugMsg("Unit in region")
    endif
    //we can also use it directly wherever we could use a boolean
    if Foo(un, GetUnitX(un)+100.0, GetUnitY(un)+100.0) then
        call BJDebugMsg("Unit in region")
    endif
endfunction