Constant Function and Variable

nandosman

New Member
Reaction score
4
Whats the difference between constant functions and constant variables?
In a code, what's better? :

- having a bunch of constant globals with the parameters of my spell

or

- having a bunch of constant functions that returns the parameters of my spell
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Functions are generally useful for using in a formula, variables are generally useful for using as a generic value.

Both constant functions and globals will inline (functions only if they follow the inline rules as outlined in the JassHelper Manual), so there's no performance difference.
 

nandosman

New Member
Reaction score
4
and for code presentation, what do you recomend? (Spell Rawcodes and constants in functions or globals?)
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Globals ;)
If you want to do something like "damage-to-be-dealt-at-a-certain-time", then use a constant function like this (Example :)):
JASS:
constant function Damage takes integer spellvl returns real
    return I2R( spellvl ) * 100
endfunction
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Sure XD

Do this then ;)
JASS:
constant function Damage takes integer spellvl returns real
    return spellvl * 100.
endfunctio
 

HellCraft

Member
Reaction score
19
The difference is that in constant variables, the user can change the value(say you posted your spell and someone else is using it) but not put in a formula. For example, if I type 5*level in a constant variable then there will be a compile error saying that level isn't initialized.
But in constant functions, you pass level as a parameter, so you can do 5*level. Ok, so someone else is using your spell, he wants to make the spell deal damage depending on the hero's kills. So he changes the formula to 5*level*kills instead.

Where to use what?
You can use functions when you need to get a value which depends on another value. Like damage, which depends on the level, so that you can pass level as a parameter.
You can use variables when that field will probably be constant and never change, like an animation path or the order string of the dummy spell.
 
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