Q: Help! My global variables are not being initialized correctly, or ZC crashes on quest start!

There are two likely causes for this "bug." Both are caused by misunderstanding how global variables are initialized.

When you start a new quest, then, and only then, are the global variables initialized, and the order in which they are initialized is only partially specified, as described here.

Consider the following code:
Code:
ffc script foo {
   void run() {}
   int a = 0;
   int b = a+10;
}

item script bar {
   int c = a+1;
}
What's wrong here? The foo script is perfectly fine; a is guaranteed to be initialized before b, so b will be correctly initialized to 10. However, the problem occurs in script bar: even though it comes later in the file than foo's declaration, it is NOT guaranteed that c will be intialized after a, so there's no telling what value c will assume.

The other possible problem is illustrated in the following snippet:
Code:
item script foo {
   void run() {}
   int a = this->X;
}
Why is this snippet buggy? Well, recall that a is initialized immediately after quest start, so foo's "this" pointer is not yet pointing to anything meaningful. To correct the code, move the initialization of a inside the run method:
Code:
item script foo {
   void run() {a=this->X;}
   int a;
}