| 01-05-2007, 04:42 PM | #1 |
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 |
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 |
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 |
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. JASS:constant function SomeRealValue takes integer level returns real return 100.+(50.*level) endfunction |
| 01-05-2007, 06:55 PM | #5 |
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 |
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 |
Oh! And for the memory? The memory space is the same? |
| 01-08-2007, 11:09 PM | #8 |
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. 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 To make it even better vexorian's optimizer changes the code above intro JASS:function SomeSpell_Actions takes nothing returns nothing call BJDebugMsg(R2S(50.)) endfunction 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 JASS:globals constant real SomeSpell_Damage = 50. endglobals function SomeSpell_Actions takes nothing returns nothing call BJDebugMsg(R2S(SomeSpell_Damage)) endfunction JASS:function SomeSpell_Actions takes nothing returns nothing call BJDebugMsg(R2S(50.)) endfunction |
