Galaxy (language)

From MoMM Wiki

Jump to: navigation, search

Galaxy is the C-like scripting language used behind the scenes in Starcraft II. All map triggers and computer player AI are powered by Galaxy. Galaxy scripts have the .galaxy extension. Information on this page is believed to be correct, but as no official documentation is available correctness is not guaranteed.

Contents

Language features

General

All language elements must be declared before they are used. Preprocessor definitions are not supported, but scripts can include each other with the include keyword (the .galaxy extension may be omitted):

include "TriggerLibs/NativeLib"

Note that include paths must be absolute, not relative.

Comments are C++-style only:

// this is a Galaxy comment

Control structures

If-, while- and do-while-statements are legal, but must always be used over a block of code enclosed in curly braces - not just an individual line of code, as is possible in C:

if ( a > b ) {
    // action
}
else if ( a < b ) {
    // action
}
else {
    // a == b
}
 
while ( i < 10 ) {
    i = i + 1;
}
 
do {
    i = i + 1;
}
while ( i < 10 );
 
// illegal
// if ( i < 10 )
//     i = 10;

Continue- and break-statements are also allowed within the body of a loop. It appears that any type of expression (rather than only boolean) can appear within the conditional part of these control structures. The logical operators are the usual &&, || and !.

Functions

Function syntax is identical to that of C, except in that return-types must be explicit:

int Add( int a, int b )
{
    return a + b;
}

Functions may be declared and defined separately:

int Add( int a, int b );
 
void UseAdd()
{
    Add( 1, 2 );
}
 
int Add( int a, int b )
{
    return a + b;
}

In particular, functions may be declared and defined in different files, but both declaration and definition must be included in any file in which the function is used.

Recursion is legal:

int Factorial( int n )
{
    if ( n == 0 ) {
        return 1;
    }
    return n * Factorial( n - 1 );
}

Variables and modifiers

Global variables may be declared anywhere. Local variables must be declared at the beginning of a function, before any other statements or control structures. Local variables may shadow global variables, but not function arguments. Single- and multi-dimensional arrays are supported. Several modifiers are available: variables declared with const keyword must be initialised on declaration and may not be modified later on; the static keyword may be used with both variables and functions and indicates that the element may only be used within the file in which it is defined. There is also a native modifier, which presumably allows a function to be defined within the game engine itself. The following code demonstrates some legal variable declarations and uses:

static const int global = 0;
const fixed      pi     = 3.14;
string           name   = "Fred"
int[ 16 ][ 32 ]  globalArray;
 
static void SomeFunction()
{
    int i = 0;
    i = 1;
    // illegal
    // int j = 0;
    globalArray[ 0 ][ 0 ] = 1;
}

Types

Known primitive types are int (integer), fixed (fixed-point real), bool (boolean value which may be either true or false), byte (integer type with 256 possible values), char, string, and void (function return types only). EssaysExperts.Com is the company which first and main priority was, is and will be customers� satisfaction with the essays online. If you still have no idea where to buy your writing tasks, this company is the best option for you. Many built-in handle types exist as well. At this point, the following are known to exist:

  • abilcmd
  • actor
  • actorscope
  • aifilter
  • animtarget
  • bank
  • camerainfo
  • color
  • doodad
  • handle
  • marker
  • order
  • playergroup
  • point
  • region
  • revealer
  • sound
  • soundlink
  • text
  • timer
  • transmissionsource
  • trigger
  • unit
  • unitfilter
  • unitgroup
  • unitref
  • wave
  • waveinfo
  • wavetarget

Uninitialised variables default to a type-specific value. Int values are automatically promoted to fixed values as needed; other type conversions must be done explicitly, though it is not known how.

Pointers are supported:

int   i  = 4;
int*  p  = &i;
int** pp = &p;
// illegal - what is the correct syntax?
// int j = *p;

As the above example shows, it is currently unknown how to dereference a pointer. The usual * operator is not valid. 0 is not a legal value - an unassigned pointer should be set to null instead.

Structures are supported:

struct Map {
    string name;
    int    numPlayers;
};
 
void ChangeMap( Map* p )
{
    p->name = "Lost Temple";
    p->numPlayers = 4;
}
 
void Foo()
{
    Map map;
    map.name = "Scrap Station";
    map.numPlayers = 2;
    ChangeMap( &map );
}

Empty structures are not allowed. Structures may be copied by assigning one instance of a structure type to another. Structures may not be passed by value; only pointers to structures can be used as function arguments. Returning a pointer to a temporary structure appears to pass the static checker, but causes the script to fail. Structures may also be recursive, as in the following example:

struct Node {
    Node* next;
    int   value;
};

Structures may also contain raw instances (rather than pointers to instances) of different structure types. Unlike functions, structures do not support forward-declaration and thus must be defined before they (or their pointer types) are used.

Lastly, Galaxy allows typedefs:

typedef int Useless;

Other operators

Other new operators which are known at this point are:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left and right bitshifts (<<, >>)
  • Shortcut operators (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=)

Garbage collection

Galaxy is a garbage-collected language. No more information is known on this subject at this point.

Unsupported constructs

Galaxy does not support any of the following C constructs:

  • Preprocessor definitions
  • C-style /* */ comments
  • For-loops (at least, not with the usual C syntax)
  • Switch-statements
  • Goto-statements
  • Dynamically-sized arrays
  • Dynamic memory allocation (new, malloc)
  • Increment/decrement operators (++, --)
  • Signed/unsigned type distinctions
  • Forward-declarations (of structures or globals)
  • The ternary operator (? :)
Personal tools