Tutorial: what can I use global code for? (now completed)

Learn how to make certain types of games and use gameEditor.

Tutorial: what can I use global code for? (now completed)

Postby lcl » Sat Aug 06, 2011 7:27 pm

GLOBAL CODE TUTORIAL BY LCL
Part 1 - variable definitions



Let's start

First off, let's get you to know what is Global Code.
you can find the Global Code from GE's main menu -> Script -> Global Code.
Or from any script event from script editor windows bottom:
Image
Global code as GE's documentation explains it:
Documentation http://game-editor.com/docs/scripting.htm wrote:Use the Global Code Editor to freely add any 'C' code (arrays, structs, and functions). The sky is the limit when you use this Editor but you should have your feet well grounded in the C Programming Language in order to use the Global Editor effectively. Your knowledge of the 'C' language will serve you well here.
Global Code can be accessed by any script.

Global code is something where you can code your own functions freely.
It is very good to learn using Global Code - it will save you a lot time if you get used
to using it!


Defining variables

You can define global variables and actor variables by clicking "variables" at the bottom of the window:
Image
Or
You can use Global Code to create global variables.
For creating an integer variable, write this:
Code: Select all
int myVar;

myVar is the name of the variable.
For creating a floating point variable, write:
Code: Select all
double myVar;

or
Code: Select all
float myVar;

For creating a string, write:
Code: Select all
char myString[255];

You can replace 255 with any number that is littler than 255. 255 is GE's limit.

So now you see that this [255] is affecting the size of the string, which actually is an array of chars.


Arrays

All other variable types can also have arrays, like this:
Code: Select all
int myVar[5];

That would create 5 variables,
myVar[0],
myVar[1],
myVar[2],
myVar[3],
and myVar[4].

You always have to remember that the arrays start with [0] and the highest usable array number is one less than what you've used when creating the array. So, if I had defined that array above and tried to use this code:
Code: Select all
myVar[5] = 10;

I'd get an error message, because there is no myVar[5]. Hope you understood this. :)


Multidimensional arrays

You can also define multidimensional arrays.
Code: Select all
int myArray[5][5];

That would create 5 * 5 integers, which means 25 integers!
To use them, do it like this:
Code: Select all
myArray[1][2] = 10;

So the first number means the level of array from which to get the 2nd integer.
This pic explains it quite well:
array.PNG
array.PNG (1.34 KiB) Viewed 15713 times

Also strings can have multiple dimensions, just write like this:
Code: Select all
char myStringArray[5][255];


And your variables can have even more dimensions, just add more "[]". :D


Giving variable a start value

You can give start values to variables. For integer, double or float do it this way:
Code: Select all
int myInteger = 5;
double myDouble = 5.5;
float myFloat = 5.25;

So just add "=" and what you want it to be. Then put the semicolon.

When you want to give string a start value, do it this way:
Code: Select all
char myString[255] = "Hello everybody";


That was about defining variables :D Let's get to function writing next!
Last edited by lcl on Mon Aug 08, 2011 8:38 pm, edited 1 time in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for?

Postby lcl » Sat Aug 06, 2011 7:44 pm

GLOBAL CODE TUTORIAL BY LCL
Part 2 - creating functions



Why should I create my own functions?

Functions can make your working so much easier,
if you have some thing you have to do often, make a function
so you can call it by 1 line.

Let's make a little variable limiting function for example.

If you want to limit some actors x to always be between - 100 and 100,
you'd need to write something like that:
Code: Select all
if (x < - 100)
{
    x = - 100;
}
else if (x > 100)
{
    x = 100;
}

And think of having many actors that need to have different limits.. :P

But what we can make with Global Code is a little function to limit a value!
(Don't worry if you don't understand the code, I will explain you about making functions later. :) )
Code: Select all
float limit(float value, float mi, float ma)
{
    return max(min(mi, ma), min(value, max(ma, mi)));
}

And then add this to the actors draw actor code:
Code: Select all
x = limit(x, - 100, 100);

And voilà, it will be limited to move only between -100 and 100.

And with this function you can limit any value you want, integer or floating point value, because of that all the values are defined as floats!
Now that's just a simple code and a simple function, but it can save a lot of time and work! :)


Return values of functions

First, what is return value?
Let's take a simple example of a function that has return value, GE's rand(); -function.
In case someone here isn't familiar with rand(); here's the documentations definition of it:
Documentation http://game-editor.com/docs/script_reference.htm wrote:rand: Generates a sequence of pseudorandom numbers. Each time it is called, an number between zero and max will be returned.

double rand (double max);

So, rand here returns double.
You input some value to rand() and it gives you a random double value between 0 and the number you've given.
So, if you write:
Code: Select all
textNumber = rand(5);

You can have your text showing any double value between 0 and 4.999999.
Notice that it won't give you 5 in any case, because rand() returns some value between 0 and the value you've given.

Function can return any types of variables, int, double, float, char. Or some functions don't return anything.


Defining the return value type of your function

For making you function return a value, write the return value's type before your functions name, like this:
Code: Select all
int myFunction()

That would be for a function that returns integer.
For float you'd do this:
Code: Select all
float myFunction()

This for double:
Code: Select all
double myFunction()

You can also return strings, just do it that way:
Code: Select all
char * myFunction()


And if you don't want your function to return any type of variable, write:
Code: Select all
void myFunction()


That's how you set the type of your return value. :)


Setting parameters for function

In GE's rand(); function you can input a value for limiting the scope that it will return the random number from.
You can do that also with your own functions.

When you define your function, put the parameters between the brackets "(" and ")".
Like this:
Code: Select all
int myFunction(int myVar)

As you see, you can add the parameters just like you would create a variable, but don't put a semicolon here!
And your function can have many parameters, like the limit function earlier in this post.
Code: Select all
float limit(float value, float mi, float ma)
{
    return max(min(mi, ma), min(value, max(ma, mi)));
}

As you see, just put a "," between parameters.
And you can add many types of parameters, like this:
Code: Select all
int myFunction(int myInt, double myDouble, float myFloat, char myString[255])

If you wanted to use that function, you'd then call it like this, for example:
Code: Select all
myFunction(5, 10.256, 2.75, "Hello");

So the first value is integer, second is double, third is float and fourth is string.


Returning the value

To return the value, use return.
Like this:
Code: Select all
int calculate(int one, int two)
{
    return one + two;
}

Now if you used this function like this:
Code: Select all
textNumber = calculate(5, 10);

The number shown would be 15.
Or a subtraction:
Code: Select all
textNumber = calculate(5, -10);

Would be -5, of course. :D


About using GE's Actor * pointer

First again, what it is?
It is a pointer which is used for storing actors.
Let's make a quick example:
Code: Select all
void moveToActor(char NAME[255])
{
    Actor * this = getclone(NAME);
    x = this->x;
    y = this->y;
}

That function would move event actor to the position of actor which name has been inputted to the function.
Like, if I wanted to move my actor named "Player" to the position of actor "Level2", I'd call this function in Player actors
script with this command:
Code: Select all
moveToActor("Level2");

Now that you know what the function does, let's go through its code, in little steps, to see how it works.

void moveToActor(char NAME[255])
{
Actor * this = getclone(NAME);
x = this->x;
y = this->y;
}

So,
void - define return type, which, in this case is void, so the function doesn't return anything.
moveToActor - the name of the function.
() - brackets that will have the parameters between them. (ATTENTION! no semicolon here!)
char NAME[255] - our functions only parameter, a string with max length of 255 chars.
{} - brackets that will have all the functions code between them.
Actor * this - creates an Actor *-pointer named this.
(Could also be written as:)
Code: Select all
Actor * this;
this = getclone(NAME);

= getclone(NAME); - will get an actor with the name inputted to the function and store it to "this". (getclone was used because it returns Actor * -pointer, so we can transform the actors name to an actor pointer!)
x = this->x; - moves the event actor to the inputted actors x position. (ATTENTION! remember to write "this->x" instead of "this.x", because we are now using pointer, not the actor itself!)
y = this->y; - moves the event actor to the inputted actors y position. (ATTENTION! remember to write "this->y" instead of "this.y", because we are now using pointer, not the actor itself!)




That's all for now! If you feel you still need some clarification with these things, ask me!
And if this tutorial needs something more, tell about that as well!


I hope this was helpful!

- lcl -
Last edited by lcl on Sun Feb 26, 2012 1:57 pm, edited 8 times in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for?

Postby Hblade » Sun Aug 07, 2011 4:30 pm

Thank you LCL :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Tutorial: what can I use global code for?

Postby lcl » Sun Aug 07, 2011 4:39 pm

Hblade wrote:Thank you LCL :)

Was that helpful for you? :D
Anyway, I'm going to continue this soon.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for?

Postby Jagmaster » Sun Aug 07, 2011 5:07 pm

This is very helpful! I have one question; can you define an actor variable in global code or do you have to use the variable tab? Same with save groups?
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: Tutorial: what can I use global code for?

Postby lcl » Sun Aug 07, 2011 5:13 pm

Jagmaster wrote:This is very helpful! I have one question; can you define an actor variable in global code or do you have to use the variable tab? Same with save groups?

Glad you find it helpful. :) remember to check this topic, because I will continue this. :)
You can't create actor variables or save groups with global code. But you can write your own save functions and then you can create those variables by global code and save them to a file. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for?

Postby lcl » Sun Aug 07, 2011 7:09 pm

Updated now the 2nd post a little, going to continue tomorrow or so. :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for?

Postby schnellboot » Sun Aug 07, 2011 7:16 pm

nice work, I think this will help many newcomers
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Tutorial: what can I use global code for?

Postby Camper1995 » Sun Aug 07, 2011 11:21 pm

Awesome work LCL!

This helped me pretty much to be more sure what I've learned until now.

Thank you for this and keep doing great tutorials as this one is!!

++ :P
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

Re: Tutorial: what can I use global code for?

Postby Game A Gogo » Mon Aug 08, 2011 3:24 pm

nice 100 point LCL, you're a great contributor :D
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Tutorial: what can I use global code for?

Postby Hblade » Mon Aug 08, 2011 4:01 pm

lcl wrote:
Hblade wrote:Thank you LCL :)

Was that helpful for you? :D
Anyway, I'm going to continue this soon.


It's very helpful for beginners, and even most moderate users ^^
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Tutorial: what can I use global code for?

Postby lcl » Mon Aug 08, 2011 5:23 pm

Game A Gogo wrote:nice 100 point LCL, you're a great contributor :D

Thanks!
Hblade wrote:
lcl wrote:
Hblade wrote:Thank you LCL :)

Was that helpful for you? :D
Anyway, I'm going to continue this soon.


It's very helpful for beginners, and even most moderate users ^^

So, it seems that the tutorial gave something to you as well..? :)

Thank you all. This is still under work, I'm going to explain a few more things. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for?

Postby lcl » Mon Aug 08, 2011 8:37 pm

This is now completed tutorial! :D
- lcl -
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Tutorial: what can I use global code for? (now completed

Postby schnellboot » Mon Aug 08, 2011 8:59 pm

just one more thing to functions
if you use a non-event actor in a function you have to comment its name
example:
Global Script
Code: Select all
void move()
{
player2.x+=10;
}


player1 -> Script Editor
Code: Select all
//player2
move();
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Tutorial: what can I use global code for? (now completed

Postby ikarus » Wed Aug 17, 2011 11:57 pm

Is there anything special I have to do to make global code work? Like for instance I put the code:
Code: Select all
int tom = 55;

And I save it in global.c, whenever I try to access it from the script editor it says it's undeclared???
>>>>>>>>>>>>>>>>>>>>> http://www.code1011.com <<<<<<<<<<<<<<<<<<<<<<<
ikarus
 
Posts: 143
Joined: Fri Aug 12, 2011 3:06 am
Score: 11 Give a positive score

Next

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest