Jass Questions

werasd

New Member
Reaction score
14
Well, for first hi!

I'm having troubles in the beginning of a function in Jass.

The line i'm having questions is:
Code:
function [function name] [B]takes[/B] nothing [B]returns[/B] nothing

I don't even understand what does the "take" and the "return" thing do or makes sensitive in the whole function.

What does it do? Can someone tell me?
 

Pyrogasm

There are some who would use any excuse to ban me.
Reaction score
134
"Takes" signifies what arguments must be passed to a function when it is called. For example, let's say you had the following function:
JASS:
function SomeFunc takes nothing returns nothing
    call SetUnitExplode(udg_YourUnit, true)
    call KillUnit(udg_YourUnit)
endfunction

To utilize this function, you'd probably do something like this:

JASS:
local unit U = GetTriggerUnit()
//...
set udg_YourUnit = U
call SomeFunc()

But you can use a new argument to pass that unit to the function:
JASS:
function SomeFunc takes unit ToKill returns nothing
    call SetUnitExplode(ToKill, true)
    call KillUnit(ToKill)
endfunction

To utilize this newfunction, you'd do something like this:

JASS:
local unit U = GetTriggerUnit()
//...
call SomeFunc(U)
 

Prometheus

Everything is mutable; nothing is sacred
Reaction score
589
When a function takes something, it is using a value.
This kind of works like a global.

JASS:
function test1 takes unit a returns nothing
    call KillUnit(a)
endfunction

function actions takes nothing returns nothing
    local unit whounit = GetTriggerUnit()
    call test1(whounit)
endfunction


If a function doesn't take anything, you don't but anything between the ( and ).
When you use takes unit a, its like calling a variable.
The variable is a which is directly connected to whounit which is a reference to the triggering unit, from the next function you can do things to a, which is basically doing stuff to whounit, which in turn is doing stuff to the triggering unit.

When you return something, your calling a function to get a value.

JASS:
function test2 takes nothing returns integer
    return GetLevelOfAbilityForUnit(GetTriggerUnit())
endfunction

function actions2 takes nothing returns nothing
    local integer level = test2()
endfunction


Since this function takes nothing, there is no need to put anything into the ( and ). When a function returns something, its like using a variable, it holds a value.

Advanced~

Constant functions are like functions but they hold a non-changing value. They are commonly used to make implementing spells easier. I believe Constant Functions always return something.

JASS:
constant function test3 takes nothing returns string
    return "Hello"
endfunction

function actions3 takes nothing returns nothing
    call DisplayTextToForce(GetPlayersAll(), test3()
endfunction


This is a simple and not very useful example but with constants, people don't have to go looking for the value to change, just create them at the beginning of the code and comment them, then instead of maybe setting the radius of your spell to 45.00, use a constant function that returns a real, 45.00 and say that, that is what that constant function does.

Functions and constant functions can take and return at the same time, and take multiple values but they can only return 1 value.

JASS:
function test4 takes real a, real b, returns real
    return a * b
endfunction

function actions4 takes nothing returns nothing
    local real gh = 56.9656
    local real hg = 78.4563
    call DisplayTextToForce(GetPlayersAll(), R2S(test4(gh, hg)))
endfunction


In the actions trigger we set 2 variables and then when we display the text, the function test4 takes both our variables and multiplies them for us. Then it returns and answer and our trigger displays the answer to all the players.

I hope this covered what you need to know :)
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
JASS:
function Multiply2 takes real daReal returns real
    return daReal*2
endfunction


Functions are used to perform a specific action you want ( a formula usually) on a particular argument (in this case, the argument is daReal). So, whatever number you put into Multiply2 as an argument, Multiply2 will return that number*2.

JASS:
function blah takes nothing returns nothing
    local real a=Multiply2(2)
    local real b=Multiply2(a)
endfunction


As you can see, the argument must match up. You can pass a function a value, or a variable, so long as the value/variable type is correct.

THIS WOULD NOT WORK:

JASS:
function blah takes nothing returns nothing
    local string a=Multiply2(8)
endfunction


Because Multiply2 will always return a real (that's why we put 'returns real' at the end). A string is not a real. Therefore, it will not work.

Anyway, if a function doesn't use any parameters, you say 'takes nothing'.
If the function doesn't return anything, you say 'returns nothing'.

Makes sense, right?
 

Pyrogasm

There are some who would use any excuse to ban me.
Reaction score
134
kc102 said:
Constant functions are like functions but they hold a non-changing value. They are commonly used to make implementing spells easier. I believe Constant Functions always return something.
Not true. A constant function can call any other constant function:
JASS:
constant function GetRandomMultiplier takes integer Level returns real
    if Level == 1 then
        return 0.20
    elseif Level == 2 then
        return 0.35
    elseif Level == 3 then
        return 1.27
    endif
endfunction

constant function GetLevelDamage takes integer Level, real Damage returns real
    local integer RandomInt = RandomInt(1, 3)
    return Damage*((GetRandomMultiplier(RandomInt)+2.05)*Level)
endfunction

Both are acceptable constant functions.

The other difference is that Vexorian's Map Optimizer inlines constant functions like so:
JASS:
constant function KillUnit takes nothing returns boolean
    return true
endfunction

function SomeFunc takes nothing returns nothing
    if KillUnit() then
        call KillUnit(GetTriggerUnit())
        call BJDebugMsg("Killed")
    endif
endfunction

//Becomes this when optimized:

function SomeFunc takes nothing returns nothing
    if true then
        call KillUnit(GetTriggerUnit())
        call BJDebugMsg("Killed")
    endif
endfunction
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Lol.. You forgot a default return:
JASS:
constant function GetRandomMultiplier takes integer Level returns real
    if Level == 1 then
        return 0.20
    elseif Level == 2 then
        return 0.35
    elseif Level == 3 then
        return 1.27
    endif
    //ELSE RETURN!!!//
endfunction


:D
 

Pyrogasm

There are some who would use any excuse to ban me.
Reaction score
134
Whatever.


Oh, and your user title lies! (1262 > 2000) == false
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top