Pointers store references to objects, just like variables store numbers. Pointers allow, for instance, the same function to do the same with different objects, depending on what object is currently referenced through a certain pointer. Pointers can be used like synonyms or alternative names for objects. Like with variables, there are some predefined pointers; the most frequently used is my. For defining a pointer to an object, just add a '*' to the object keyword.
ENTITY* name; // define an uninitialized entity pointer ENTITY* name = otherpointer; // define an initialized entity pointer
In the second line the pointer references to another entity pointer previously defined in the script, for instance a view or sky entity. In functions you can use pointers like normal object. Pointers can be assigned to each other, and can be compared through the if instruction to check whether they reference to the same object. Example:
ENTITY* ent1 = NULL; // initialize the pointer to zero ENTITY* ent2; ... ent1 = my; ent2 = ent1; ent2.alpha = 75; // access an entity parameter if (ent1 == ent2) { ent_remove(ent2); } if (ent1 != NULL) { ent_remove{ent1); }
Some pointers are predefined. You'll find a list of them in a further chapter. The two most important predefined pointers are my for the entity that has started the current function; and you for a second entity that is set by some engine functions.
C-Script supports the following pointers: ENTITY*, STRING*, PANEL*, BMAP*, and MATERIAL*. Lite-C supports pointers for all predefined or user-defined variables and structs. The compiler autodetects whether an object is a pointer or the real object, so users need normally not care about the difference.
float myfunction(int a, float b); // define a function pointer named "myfunction" float fTest(int a, float b) { return (a*b); } ... myfunction = fTest; x = myfunction(y,z);For storing arrays of function pointers in lite-C, void* arrays can be used. Example:
float myfunction(int a, float b); // define a function pointer void* function_array[100]; // define a pointer array float fTest(int a, float b) { return (a*b); } ... function_array[n] = fTest; myfunction = function_array[n]; x = myfunction(y,z);