When Global Code is executed?

Game Editor comments and discussion.

When Global Code is executed?

Postby Clokio » Wed Jun 01, 2011 7:34 pm

When game start, on every frame?
Global Code can be accessed by any script. How?
:mrgreen:
Clokio
 
Posts: 229
Joined: Tue May 17, 2011 2:18 pm
Score: 32 Give a positive score

Re: When Global Code is executed?

Postby skydereign » Wed Jun 01, 2011 7:46 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: When Global Code is executed?

Postby savvy » Wed Jun 01, 2011 7:48 pm

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.
--> For my help, i ask for a simple +1 if it helps! ^-^
--> I dont code, I type art which you dont understand.
--> I keep winning the 3D model challenge at college, teacher says: "you keep winning im not giving you prizes".
User avatar
savvy
 
Posts: 494
Joined: Wed Jun 03, 2009 11:55 am
Location: England
Score: 44 Give a positive score

Re: When Global Code is executed?

Postby Clokio » Wed Jun 01, 2011 10:04 pm

Thanks, just another question, is variables declared are global?
Clokio
 
Posts: 229
Joined: Tue May 17, 2011 2:18 pm
Score: 32 Give a positive score

Re: When Global Code is executed?

Postby schnellboot » Wed Jun 01, 2011 10:19 pm

variable declarations in functions are not global
but just declarations are global yes
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest