Page 1 of 1

When Global Code is executed?

PostPosted: Wed Jun 01, 2011 7:34 pm
by Clokio
When game start, on every frame?
Global Code can be accessed by any script. How?
:mrgreen:

Re: When Global Code is executed?

PostPosted: Wed Jun 01, 2011 7:46 pm
by skydereign
Global code is not executed. In global space you declare functions, variables, and definitions. These can then be used in any script you want.

The below are examples of variable declarations. These variables can be accessed exactly like the variables you create the the script editor's [Variables] button.
Code: Select all
int variable;
int array[10];
char string[256];



You can also declare functions in global code, which are executed when you call them. Functions can be used to make your coding cleaner, and reusable. So if you find yourself repeating code between events or actors, you should make it a global function, that way you only have to call one line of code.
Code: Select all
// sets r g or b to 0 if it is 255
// otherwise set to 0
void
toggle_color ()
{
    r=(r!=255)*255;
    g=(g!=255)*255;
    b=(b!=255)*255;
}


If for some reason you wanted to use the above code a lot, you can call it like this.
Code: Select all
toggle_color();


Though the example wasn't all that practical, writing functions helps a lot. Even if you aren't writing the code a lot, it is helpful to have things written as functions as it makes searching for problems and fixing things up easier.

Re: When Global Code is executed?

PostPosted: Wed Jun 01, 2011 7:48 pm
by savvy
global code is in something called a 'void' it is there in the background until needed at all times.
think of a jar of sweets on the back shelf, only used when needed. otherwise it stays closed and concealed.

can be called on by what it defines, such as this:
void fill(int i){}
if you typed fill(0);
it would use whats in the brackets and the number 0 to do so, 0 could mean black for example.

Re: When Global Code is executed?

PostPosted: Wed Jun 01, 2011 10:04 pm
by Clokio
Thanks, just another question, is variables declared are global?

Re: When Global Code is executed?

PostPosted: Wed Jun 01, 2011 10:19 pm
by schnellboot
variable declarations in functions are not global
but just declarations are global yes