Hey guys, this is the second tutorial I make for Gs2, but make sure you've read and understood the #1, or else you will have no idea with what we're working with at the moment.
Arrays:
Remember we had examples with variables in the first tutorial? Well, an array is a group of variable, listed under one variable name.
'Hey john, what the heck are you talking about?'
Well, let me just show you:
See that above? As you can see, I've listed lots of variables and only used 'run'. But, there's another way of doing this and which is:PHP Code:
function onCreated() {
a = 1;
b = 4;
run = "noob";
hi = "hello";
john = 1337;
burgers = 1.35;
echo(run); // which will echo noob
}
Okay so what I did, is placed all these variable listed in the first example in one 'array'.PHP Code:
function onCreated() {
cheese = {1, 4, "noob", "hello", 1337, 1.35}; // (you don't need to have spaces, remember spaces does nothing in scripting, but I put them cause they look nice <3)
}
To create arrays, you need the following syntax :
variablename = { element0, element1, element2 ...};
You can have as many elements as you want, but remember to keep them between curly brackets! : { }
And are separated by a comma ','.
So now, you want it to echo the element.
(if you still haven't understood what an element is, it's a value/string or variable inside the array)
Let's see what RC printed for us... Oh no! John what have you done! I wanted it to echo the second element which is 4, but what RC printed was:PHP Code:
function onCreated() {
cheese = {1, 4, "noob", "hello", 1337, 1.35};
echo(cheese);
}
How can I make it print the second element?1,4,"noob","hello",1337,1.35,
In order to pick which element you want to print, you have to include square brackets [] after the array variable name, containing the element ID.
For example:
What this printed is the forth element which is 1337.PHP Code:
function onCreated() {
cheese = {1, 4, "noob", "hello", 1337, 1.35};
echo(cheese[4]);
}
'Hey john, what are you talking about? 1337 isn't the fourth element, it's the fifth!'
Well, back to kindergarten, you've learned to count numbers as 1 2 3 4 5 ...
But computers have been thought to count this way: 0 1 2 3 4 5 ...
So for short, the first element is 0, the second is 1 etc
So if you echo(cheese[3]); it will echo 'hello', if you do echo(cheese[5]); this will echo 1.35PHP Code:
cheese = {1, 4, "noob", "hello", 1337, 1.35};
// 0 1 2 3 4 5
Consider array members/elements as variables too. So they can be used for calculations or whatever a variable does.
functions and return:PHP Code:
function onCreated() {
johnisnice = {1, 5, 6, 7, "burgers", 13, 5, 7};
echo(johnisnice[2] + johnisnice[0]); // which means 6 + 1 = 7 ... and this will echo 7
}
This is very important, as well as the explanations after that.
In the script editor, you're not limited to the onCreated() function, you can have multiple ones, but the compiler will only trigger the onCreated() one... (trigger means run/give us the key to run the code in it)
So if you have:
This will only echo 'PIE!', why? As I said, only the onCreated() function is being triggered.PHP Code:
function onCreated() {
echo("PIE!");
}
function fridge() {
echo("BURGERS");
}
But if you want to trigger the fridge() function, all you have to do is add fridge(); to the script:
So what RC echoes it:PHP Code:
function onCreated() {
fridge(); // this will trigger fridge()
echo("PIE!");
}
function fridge() {
echo("BURGERS");
}
Have you noted that order? Why is it in this order? Let me tell you how the Compiler reads it:BURGERS
PIE!
Hello John, we meet again, I'm the compiler!
You have two functions in here, cool! But I am only gonna run the onCreated() function for you, sorry!
Oh, so you want me to run fridge()? Okay, let me see what's inside fridge.PHP Code:
function onCreated() {
fridge();
Okay, there you go, let me echo BURGERS... *echoes BURGERS to RC*PHP Code:
function fridge() {
echo("BURGERS");
Alright, the function has no more scripted stuff, so let me go back to where I was in the onCreated() function...
Alright... *echoes PIE!*PHP Code:
echo("PIE!");
What's also interesting about calling other functions, is that it can 'return' an object.
For example, when you 'ask someone' (trigger a function) for a pencil, it will give you (return) one.
And this will echo 6...PHP Code:
function onCreated() {
nubb = tuna(); // tuna() will be replaced by 6, because it has returned 6, so nubb = 6;
echo(nubb);
}
function tuna() {
return 6;
}
What is also happening is that it's stopping the function when the 'return;' parameter is read.
so if I do:
This will echo HI only... YOU THERE? will not be echoed because it has returned before it came to the function: echo("YOU THERE?");PHP Code:
function onCreated() {
echo("HI");
return; // this will return 0; but you can type it return;
echo("YOU THERE?");
}
this. temp. :
This is a really important lesson, as you will need to use it from now on.
All of our previous examples and tutorials were written like this:
But from now on, you'll be using temp. and this. at the beginning:PHP Code:
tuna
burgers
hi
john
sup
What does those do? Let me go deeper:PHP Code:
this.tuna
temp.burgers
temp.hi
this.john
this.sup
If you have a variable, and want it to go on ALL the script, you need to use this. :
And this will echo 4.PHP Code:
function onCreated() {
this.john = 4;
nub();
}
function nub() {
echo(this.john);
}
So this way, this.john will work on all functions...
BUT, if you are only using the variable 'john' in the function itself, you use temp.john :
And this will echo nothing... Because temp.john is only available in the onCreated() function, so to make it able to echo temp.john, you must use echo(temp.john); in the onCreated() function :PHP Code:
function onCreated() {
temp.john = 4;
nub();
}
function nub() {
echo(temp.john);
}
And this will echo 4...PHP Code:
function onCreated() {
temp.john = 4;
echo(temp.john);
}
This works for all types of variables, strings, integers (numbers), float (decimals), arrays etc...
What's with the dots ...?!:
Dots in scripting are used for reference, like an address...
(this is just an example, none of this actually works unless you code them, lol)PHP Code:
earth.asia.japan.tokyo.city2.building3
solarsystem.earth.asia.middle_east.lebanon.beirut.city5.building249
I'll explain why is it used for later on.
function parameters:
Okay, this one's a little bit confusing, but it's really easy to understand once you get the concept.
Parameters are inside of the parenthesis of the function...
As we learned echo(); has only one parameter which is the first...
What's actually inside echo()? It's a hidden function in Gs2.
So when you type echo("john"); , the compiler will do:PHP Code:
function echo(parameter1) { // you can call parameter1 whatever you want, but this function already exists so...
print parameter1 to RC;
}
and then print parameter1...PHP Code:
parameter1 = "john";
For example, let's create a function that echoes all the text that we send to it...
So let's try to use it:PHP Code:
function printTextOnRC(txt) { // I called the first parameter txt
echo(txt);
}
This will actually echo 'Hi my name is john'.PHP Code:
function onCreated() {
printTextOnRC("Hi my name is john");
}
function printTextOnRC(txt) {
echo(txt);
}
Still didn't understand? Here's how the compiler reads it:
Hope you understood this time.Hi, compiler at your service...
Let's see what we have here...
Okay, let's see what's on printTextOnRC...PHP Code:
function onCreated() {
printTextOnRC("Hi my name is john");
}
Oh, printTextOnRC takes one parameter called txt, and since in the onCreated() function there was printTextOnRC("Hi my name is john"); so all I have to do is make txt = "Hi my name is john"; *some magic* Done! now txt is equals to "Hi my name is john"...PHP Code:
function printTextOnRC(txt) {
So what's next?
You want me to echo txt? What was txt again? Oh right, "Hi my name is john", fine... *echoes Hi my name is john*PHP Code:
echo(txt);
My job is done here...
You can have multiple parameters, and separate them by a comma.
For example, having a function that adds two numbers and returns it's sum :
So if we use it :PHP Code:
function addNumbers(number1, number2) {
temp.answer = number1 + number2;
return temp.answer; // you can also use return number1 + number2;
}
This will make number1 = 1;PHP Code:
function onCreated() {
temp.something = addNumbers(1, 4);
echo(temp.something);
}
function addNumbers(number1, number2) {
temp.answer = number1 + number2;
return temp.answer; // you can also use return number1 + number2;
}
and number2 = 4;
and makes temp.answer = 1 + 4; which is 5, and it returns 5
temp.something now equals 5
and it echoes 5
simple enough!
---
(read next post)