Using Global Code

From Game Editor

Revision as of 22:28, 10 July 2011 by Dst (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Global Code Basics

The global code (script>global code) allows you add infinite scripts to your game. This is useful for pretty much anything.

If you are having trouble making something happen in global code, this page may provide an answer to your issue.

When using the global code, you will notice that some things may or may not work in it. Basically, there is a set of rules that must be followed for functions to work inside global code.

All global functions must start with a declaration;

 void dothis(){
 }
 int enemyhp;

The first one creates a function that you can run from any actor or script window.

The second creates an integer named enemy hp. Declared values will be global variables. You cannot declare an actor variable. Only primitive types will be saved.


Order and Object Rules

Global code allows nesting of functions. You can write as many as you like and call them from others, provided you don't write them in reverse order.

 void doRun(){
 }
 void mainDraw(){
 doRun();
 }

Will work, because the compiler will find the definition of doRun() before it's called in mainDraw. If mainDraw is added first, you will get an error.

You cannot use global code to declare complex global objects; If you try to use Struct or Actor* you will encounter problems. Game Editor handles all the interaction, listing, and running of complex Objects. It is not designed for you to add your own in this manner. Use the 'Add Actor' button instead.

You may use Actor* as a local variable for use in a script, if you like.


Actor and Variable Rules

A script may use abstract actor references as long as the actor is referenced in the function call itself;

What this means is:

Enemy>Draw Actor>

 doRun();

Global Script>

 void doRun(){
 x++;
 }

This will work because the global code can reference the actor that the script was called in. There must be a reference to the actor calling the script, or in the script declarations in order to use Event Actor variables. Event Actor, Collide Actor, Parent Actor - these will be accessible inside a 1st level function. (note that collide actor would require the script was called from a collision in order to exist).

If you try to run one global function inside another, the actor reference will be lost.

Enemy>Draw Actor>

 doRun();

Global Script>

 void doRun(){
 doMove();
 }

Global Script>

 void doMove(){
 x++;
 }

Will NOT work because the actor calling doMove is a function itself; There is no actor reference passed and it will not know what actor x is referring to.


Some functions and values cannot be passed inside global script, and some can. For instance, ChangeAnimation, xvelocity, DestroyActor....these can be run inside a function and will work if the function is called from the original actor.

Directional_velocity, ChangePath()...these will NOT work. This is one of the quirks of the program that you will have to work around. They will require you to create your own function in order to get the actor in question. A simple workaround involves Actor * and the getclone (preferrably getclone2) function.

 void cPath(char anim[32], int cindex, char pname[32]){
 Actor *this=getclone2(anim, cindex);
 ChangePath(this->clonename, pname, BOTH_AXIS);
                                    }
 cPath("Enemy", cloneindex, "path1");

Many functions will require you to use Actor * and pass the function using Actor->clonename;

If you have a function or variable that is not working inside the global script, try this custom function method. Remember that cloneindex will be accessible inside the actor's 1st level script, provided it is called from the actor itself, from some event in the Actor Control Panel.