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
590
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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