HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Jass spell help

09-10-2007, 09:45 PM#1
~Void~
I know most of the syntax (partially from a basic knowledge of C++) and I know how JASS relates to GUI, but what I want to know is that I've seen lots of spells where each line almost is a function, e.g.

Collapse JASS:
function blarg takes ... returns ...
(line goes here, usually has to do with a constant)
endfunction

Just repeated a lot. Why is this there and what exactly is it for? Is there some part of JASS that makes it easier to do that? I want to understand because it will help me make spells entirely in JASS, not just leakless GUI code that has its globals removed and locals added to make MUI. Please somebody explain.
09-10-2007, 10:20 PM#2
The Elite
well

Collapse JASS:
function Kill2 takes unit a, unit breturns nothing
    call KillUnit(a)
    call KillUnit(b)
endfunction


Well if i were to put that into the custom script part of my map i could do this:
Collapse JASS:
function gg_trg_KillUnit takes nothing returns nothing
    call Kill2(udg_Thrall, udg_Jaina)
endfunction
see, in jass you can have functions like that that you declair in the custom script section and then you can use them when ever you want in other functions. If you need a better description just look through the JASS tutorials on this website
09-10-2007, 10:23 PM#3
moyack
Something like this??

Collapse JASS:
constant function Spell_SpellID takes nohting returns integer
    return 'A000'
endfunction

This is used to set constant values, like the spell rawcodes, AOE values, etc.

I suggest to check some tutorials about introduction to JASS, it will give you better lights about this topic.

This is a good one: http://www.wc3campaigns.net/showthread.php?t=74894
09-10-2007, 10:24 PM#4
TaintedReality
It lets later users easily change the values of your spell. It's mostly used for spells that are submitted to the public.
09-11-2007, 06:05 AM#5
Captain Griffen
constant functions like that can be inlined by Vex's optimiser, so at run time they are as fast as not having functions, but have the ease of editing in the WE.
09-11-2007, 06:19 AM#6
botanic
Basically it is just so that the spell can conform to the JESP spell standard so it makes it easier for ppl to import and use the spell.
09-11-2007, 09:23 PM#7
~Void~
Quote:
Originally Posted by moyack
Something like this??

Collapse JASS:
constant function Spell_SpellID takes nohting returns integer
    return 'A000'
endfunction

This is used to set constant values, like the spell rawcodes, AOE values, etc.

I suggest to check some tutorials about introduction to JASS, it will give you better lights about this topic.

This is a good one: http://www.wc3campaigns.net/showthread.php?t=74894

I think I get it, but I know I really don't.
09-11-2007, 09:41 PM#8
botanic
ok ill sum it up for ya

constant functions are used for the following reasons

1) they are ever so slightly faster normally
2) with vex's optimizer they can become much faster then a non constant
3) because it helps for the distribution of spells to others (look for the constant blocks and change that stuff to modify)

HOWEVER

constants have some downsides:

1) unmodifiable return values, for example you cant return any value that MIGHT change (even if it doesn't) like strength of a unit level ect
2) are just generally harder to work with due to reason 1
3) constants are mostly useless are parts of a spell such as the creation of special effects, damage ect due to there nature of being a "constant"

So basically if you are returning anything but the same number value or w/e don't use them, and honestly it is sometimes better to avoid them altogether if you wont be making a spell public.
09-12-2007, 12:37 AM#9
Pyrogasm
This is all false:
Quote:
Originally Posted by botanic
1) they are ever so slightly faster normally
2) with vex's optimizer they can become much faster then a non constant

1) unmodifiable return values, for example you cant return any value that MIGHT change (even if it doesn't) like strength of a unit level ect
2) are just generally harder to work with due to reason 1
3) constants are mostly useless are parts of a spell such as the creation of special effects, damage ect due to there nature of being a "constant"

So basically if you are returning anything but the same number value or w/e don't use them, and honestly it is sometimes better to avoid them altogether if you wont be making a spell public.
They are no faster. And Vexorian's optimizer does not make them faster, it simply inlines them, meaning this:
Collapse JASS:
//This
constant function Int takes nothing returns integer
    return 5
endfunction
function SomeFunc takes nothing returns integer
    return Int()+15
endfunction

//Becomes this:
constant function Int takes nothing returns integer
    return 5
endfunction
function SomeFunc takes nothing returns integer
    return 5+15 //It might go directly to 20, I'm not sure.
endfunction

The only thing you can't do with a constant function is call another non-constant function. The following functions are valid:
Collapse JASS:
constant function StrengthBonus takes integer HeroStrength, integer Level returns integer
    return 25*HeroStrength+5*Level
endfunction

constant function Duration takes integer HeroStrength, integer Level, real RandomDur returns real
    return StrengthBonus(HeroStrength, Level)/5.00+RandomDur
endfunction
While these are not:
Collapse JASS:
function StrengthBonus takes unit U, integer Level returns integer
    return 25*GetHeroStr(U)+5*Level
endfunction

constant function Duration takes integer HeroStrength, integer Level returns real
    local real RandomDur = GetRandomReal(0.00, -5.00)
    return StrengthBonus(HeroStrength, Level)/5.00+RandomDur
endfunction
09-12-2007, 01:39 AM#10
botanic
actually they are faster because once they are called the value is not rederived. So they are the same speed the first time however subsequent times their are a microsecond faster... not that .00001 seconds of extra speed really matters ne ways :/

And vex's optimizer does make the first time faster because calling pure basic values as a return is faster then anything else
09-12-2007, 05:36 AM#11
Pyrogasm
The function doesn't become faster when optimized. It becomes inlined, which makes it faster, but the function no longer exists.