Static variable or global variable?
Safer (not better)
int f() { OnStart(); return(5); } const int i = f(); const int j_global = i; #define PRINT(A) Print(#A + " = " + (string)(A)) void OnStart() { static const int j_static = i; PRINT(j_static); PRINT(j_global); }
Result
j_static = 0 j_global = 0 j_static = 0 j_global = 5
It is necessary to understand well what potential problems can be when using static variables.
fxsaber: It is necessary to understand well what potential problems can be when using static variables.
You didn't show any. You showed that the order of initialization of global/static variables is undefined.
By the documentation they should only be initialized with constants and "= i;" is not one. IMO the compiler should reject any such code.
Which is better and safer?
use static variable in OnTick() function or global variable?