• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[General] Difference between Constant Function and Function

Status
Not open for further replies.
Level 9
Joined
Apr 23, 2011
Messages
460
What is the real difference between say:
JASS:
constant function test takes nothing returns integer
return 50
endfunction

and

JASS:
function test takes nothing returns integer
return 50
endfunction

does the application of the "constant" apply some different method to return the value or something? I just don't get why or when I should use either of the two.
 
Constant functions imply that the return value will not be a non-constant value.

But really, there is no actual difference. (as far as anyone can tell) We used to use "constant" before all the functions that were considered configurable. However, Jass NewGen brought out freeform global declaration, so we don't need that anymore.

You should just use normal functions instead of constant functions unless you have organizational issues. Normal functions do not have the limits of return values that constant functions have.
 
Jass NewGen Pack features a tool known as JassHelper, it will basically provide a neat interface for you to code in. (with some nice features) For example, you don't have to go to the variable editor to create globals. You can simply declare a globals block:
JASS:
globals
    unit someUnit //creates a global variable of the type "unit" with the name "someUnit"
endglobals

This is the link to Jass NewGen Pack:
http://www.wc3c.net/showthread.php?t=90999

This is the link to the latest published JassHelper (aside from Cohadar's):
http://www.wc3c.net/showthread.php?t=88142

This is the link to the readme for JassHelper, which should explain some things:
http://www.wc3c.net/vexorian/jasshelpermanual.html

---

Anyway, basically you really don't need the keyword "constant" before functions except for organizational purposes or if you don't want to allow a function return a non-constant value. (for whatever reason)

The reason why I mentioned freeform globals is because people used to use constant functions for configurable values for spells (like amount of damage). Example:
JASS:
constant function DmgAmount_LightningSpell takes nothing returns real
    return 250. //edit this to change the damage of the spell...
endfunction

//... then they refer to it later on...
    call UnitDamageTarget( u, tar, DmgAmount_LightningSpell(), true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, null )

That is how configurables for spells used to be done. (so that people could easily modify values without looking too far)

However, with JassHelper, spell configurables are usually just done like this:
JASS:
globals
    constant real DamageAmount_LightningSpell = 250.
endglobals

//... then they refer to it later on...
    call UnitDamageTarget( u, tar, DamageAmount_LightningSpell, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, null )

Now this may present the question, why use the constant keyword before a configurable? It really depends on the spell, but usually you don't want configurables to have different values throughout the game. Some stuff like damage are meant to be fixed for certain spells, so you can add the constant keyword so that someone can't re-set the variable to a different value. In the example with the global DamageAmount_LightningSpell, adding the "constant" keyword makes it so that you can't reassign the value to something else later on. For example:
set DamageAmount_LightningSpell = 125.
Will throw an error since the variable has the keyword constant before it.

TL;DR: The keyword constant is mostly an organizational tool. It is up to you whether you want to use it for your code, especially if you are not submitting the code for public use.
 
Status
Not open for further replies.
Top