Global and Static Variables

Sure you have been in the situation where you declared the same variable in each and every script over and over again.
“Why is there no way to make them available in every script?” Is what went through your head at such times.

Be relieved now for there has been added a means of declaring and accessing global and scriptbound (static) values!
They cannot be changed, though.

Difference between statics and globals

Both, statics and globals are scriptbound and instantiated before the script is executed.
Both cannot be changed.
The difference is how they are called:
Globals can be called from everywhere simply by their name unless you already have a local variable that has the same name.
Statics on the other hand, need to use the cross-script reference to be accessed.

Globals are created using the global keyword.
Statics are created using the static keyword.

Aside from that, they are identical!

Declaring a global value

Declaring a global value is as simple as declaring a local value:

import crafttweaker.item.IItemStack;


global myGlobalValue as IItemStack = <minecraft:dirt>;
static myStaticValue as IItemStack = <minecraft:sand>;

Okay, let’s break it down, shall we?

  1. global keyword that indicates the declaration of a global value
  2. myGlobalValue the name of the value
  3. as IItemStack the type of the value (It is recommended to import the types before casting the variable)
  4. = <minecraft:dirt>; value initialization. As global values are final, you need to initialize them whilst declaring them!

Words of advice