HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Which is the best?

01-05-2007, 04:42 PM#1
GALLED
Hello,

I'm begin writing spells in JASS and for some ocasions i can choose between a constant function and a global variable.

My question is: In general ¿which is better, constants functions or global variables?

thanks in advance
01-05-2007, 04:54 PM#2
Rising_Dusk
Constant functions can not be changed on the fly.
Globals can be.

It entirely depends upon what you need.
If you need just a value that will NEVER change, then a constant is the way to go.
But if you need some value that can be changed, then go with a global.
01-05-2007, 05:58 PM#3
Anopob
In the other thread I thought someone said that constants can SOMETIMES be changed. And what do you mean "on the fly"?
01-05-2007, 06:45 PM#4
Rising_Dusk
On the fly means in game.
You can set a global variable in-game, but not a constant.

Also, constants can give variable results, but only if given an argument.
You can't change the value of a constant function in-game.

Collapse JASS:
constant function SomeRealValue takes integer level returns real
    return 100.+(50.*level)
endfunction
That would be an example of a constant that can return different numbers.
01-05-2007, 06:55 PM#5
moyack
Other advantage is the speed. Vex has proben (I don't remeber where was posted that) that constant functions are faster than global.
01-05-2007, 07:11 PM#6
Vexorian
constant functions are faster than global?

It seems that constant functions are as fast as normal function.

When I tested them constants did have an smaller time, but it seems that it was caused by measure problems, the time wasn't significant smaller.

But pipedream has checked the engine and constant functions don't behave different than normal functions, the same happens with variables.

The advantage right now is that the optimizer can inline constant "variables" or functions some times , and then they are much faster than the normal ones after the optimizer
01-08-2007, 10:47 PM#7
GALLED
Oh!

And for the memory? The memory space is the same?
01-08-2007, 11:09 PM#8
SFilip
Considering constants are inlined by the optimizer they shouldn't take any memory...globals on the other hand do. But if a couple of bytes really bothers you...

Anyway constants are usually used to allow the public to easily modify your spell/system.

Collapse JASS:
constant function SomeSpell_Damage takes nothing returns real
    return 50.
endfunction

function SomeSpell_Actions takes nothing returns nothing
    call BJDebugMsg(R2S(SomeSpell_Damage()))
endfunction
Now lets pretend that SomeSpell_Damage is used multiple times in the actions...it would require the user to look through the code and replace every 50. with 100. for example...but this way he can only change the function in the top without even looking at the actual code.
To make it even better vexorian's optimizer changes the code above intro
Collapse JASS:
function SomeSpell_Actions takes nothing returns nothing
    call BJDebugMsg(R2S(50.))
endfunction
which is clearly the fastest way to do it.

However with the development of preprocessors globals easily beat functions, but require the user to actually have the preprocessor and an editor hack to use it. The code above could be done this way if you use JassHelper
Collapse JASS:
globals
    constant real SomeSpell_Damage = 50.
endglobals

function SomeSpell_Actions takes nothing returns nothing
    call BJDebugMsg(R2S(SomeSpell_Damage))
endfunction
which once again turns intro this after you use vexorian's optimizer
Collapse JASS:
function SomeSpell_Actions takes nothing returns nothing
    call BJDebugMsg(R2S(50.))
endfunction