Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17

Thread: Gs2 for the Noobs #2

  1. #1
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    812

    Gs2 for the Noobs #2

    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:
    PHP Code:
    function onCreated() {
      
    1;
      
    4;
      
    run "noob";
      
    hi "hello";
      
    john 1337;
      
    burgers 1.35;
      echo(
    run); // which will echo noob

    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() {
      
    cheese = {14"noob""hello"13371.35}; // (you don't need to have spaces, remember spaces does nothing in scripting, but I put them cause they look nice <3)

    Okay so what I did, is placed all these variable listed in the first example in one 'array'.
    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)

    PHP Code:
    function onCreated() {
      
    cheese = {14"noob""hello"13371.35};
      echo(
    cheese);

    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:
    1,4,"noob","hello",1337,1.35,
    How can I make it print the second element?
    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:
    PHP Code:
    function onCreated() {
      
    cheese = {14"noob""hello"13371.35};
      echo(
    cheese[4]);

    What this printed is the forth element which is 1337.
    '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
    PHP Code:
    cheese = {14"noob""hello"13371.35};
    //        0   1   2       3      4    5 
    So if you echo(cheese[3]); it will echo 'hello', if you do echo(cheese[5]); this will echo 1.35

    Consider array members/elements as variables too. So they can be used for calculations or whatever a variable does.
    PHP Code:
    function onCreated() {
      
    johnisnice = {1567"burgers"1357};
      echo(
    johnisnice[2] + johnisnice[0]); // which means 6 + 1 = 7 ... and this will echo 7

    functions and return:
    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:
    PHP Code:
    function onCreated() {
      echo(
    "PIE!");
    }

    function 
    fridge() {
      echo(
    "BURGERS");

    This will only echo 'PIE!', why? As I said, only the onCreated() function is being triggered.
    But if you want to trigger the fridge() function, all you have to do is add fridge(); to the script:
    PHP Code:
    function onCreated() {
      
    fridge(); // this will trigger fridge()
      
    echo("PIE!");
    }

    function 
    fridge() {
      echo(
    "BURGERS");

    So what RC echoes it:
    BURGERS
    PIE!
    Have you noted that order? Why is it in this order? Let me tell you how the Compiler reads it:

    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!
    PHP Code:
    function onCreated() {
      
    fridge(); 
    Oh, so you want me to run fridge()? Okay, let me see what's inside fridge.

    PHP Code:
    function fridge() {
      echo(
    "BURGERS"); 
    Okay, there you go, let me echo BURGERS... *echoes BURGERS to RC*

    Alright, the function has no more scripted stuff, so let me go back to where I was in the onCreated() function...

    PHP Code:
    echo("PIE!"); 
    Alright... *echoes 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.

    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;

    And this will echo 6...
    What is also happening is that it's stopping the function when the 'return;' parameter is read.
    so if I do:
    PHP Code:
    function onCreated() {
      echo(
    "HI");
      return; 
    // this will return 0; but you can type it return;
      
    echo("YOU THERE?");

    This will echo HI only... YOU THERE? will not be echoed because it has returned before it came to the function: 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:
    PHP Code:
    tuna
    burgers
    hi
    john
    sup 
    But from now on, you'll be using temp. and this. at the beginning:
    PHP Code:
    this.tuna
    temp
    .burgers
    temp
    .hi
    this
    .john
    this
    .sup 
    What does those do? Let me go deeper:
    If you have a variable, and want it to go on ALL the script, you need to use this. :
    PHP Code:
    function onCreated() {
      
    this.john 4;
      
    nub();
    }

    function 
    nub() {
      echo(
    this.john);

    And this will echo 4.
    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 :
    PHP Code:
    function onCreated() {
      
    temp.john 4;
      
    nub();
    }

    function 
    nub() {
      echo(
    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;
      echo(
    temp.john);

    And this will echo 4...

    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...
    PHP Code:
    earth.asia.japan.tokyo.city2.building3
    solarsystem
    .earth.asia.middle_east.lebanon.beirut.city5.building249 
    (this is just an example, none of this actually works unless you code them, lol)

    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.
    PHP Code:
    function echo(parameter1) { // you can call parameter1 whatever you want, but this function already exists so...
      
    print parameter1 to RC;

    So when you type echo("john"); , the compiler will do:
    PHP Code:
    parameter1 "john"
    and then print parameter1...

    For example, let's create a function that echoes all the text that we send to it...
    PHP Code:
    function printTextOnRC(txt) { // I called the first parameter txt
      
    echo(txt);

    So let's try to use it:
    PHP Code:
    function onCreated() {
      
    printTextOnRC("Hi my name is john");
    }

    function 
    printTextOnRC(txt) {
      echo(
    txt);

    This will actually echo 'Hi my name is john'.

    Still didn't understand? Here's how the compiler reads it:
    Hi, compiler at your service...
    Let's see what we have here...
    PHP Code:
    function onCreated() {
      
    printTextOnRC("Hi my name is john");

    Okay, let's see what's on printTextOnRC...

    PHP Code:
    function printTextOnRC(txt) { 
    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"...

    So what's next?
    PHP Code:
    echo(txt); 
    You want me to echo txt? What was txt again? Oh right, "Hi my name is john", fine... *echoes Hi my name is john*

    My job is done here...
    Hope you understood this time.
    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 :
    PHP Code:
    function addNumbers(number1number2) {
      
    temp.answer number1 number2;
      return 
    temp.answer// you can also use return number1 + number2;

    So if we use it :
    PHP Code:
    function onCreated() {
      
    temp.something addNumbers(14);
      echo(
    temp.something);
    }

    function 
    addNumbers(number1number2) {
      
    temp.answer number1 number2;
      return 
    temp.answer// you can also use return number1 + number2;

    This will make number1 = 1;
    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)
    Last edited by John; 07-16-2013 at 09:05 AM.
    -Johnaudi

  2. #2
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    812
    a = a + 1; ?!! :
    This world wide syntax is known for programming.
    Can you calculate a = a + 1 in math? No.
    In scripting, yes you can.
    It's really simple.
    PHP Code:
    function onCreated() {
      
    5;
      
    1;
      echo(
    a);

    Let's take a deep breath...
    a is equals to 5, got it.
    now a is equals to a + 1??
    think of it this way, since a is equal to 5
    then a is equals to 5 + 1
    and a is now equals to 6.
    And this indeed echoes 6.

    What this does, is it adds x number to a.
    (remember, it's better to use temp.a and this.a but in here I'm giving an example...)

    PHP Code:
    function onCreated() {
      
    5;
      
    1;
      echo(
    a);

    Same concept, will echo 4.
    Now, it becomes really tiring for a developer to type a = a + 1; all the time, so there's another syntax you can use which is the following:
    PHP Code:
    // you can replace the number 1 by anything you want
    += 1// Adds 1 to a
    -= 1// Substracts 1 from a
    /= 1// This means a = a / 1; which is a, this is done mostly by numbers other than 1
    *= 1// This means a = a * 1; which is a, this is done mostly by numbers other than 1
    %= 1// This one means a = a % 1; what % does it the remainder of the division. I'll explain this one later
    @= 1// This one means a = a @ 1; Let's say a is noob, it will become noob1
    a++; // smaller syntax for a = a + 1;
    a--; // smaller syntax for a = a - 1; 
    So here's a small example:
    PHP Code:
    function onCreated() {
      
    3;
      
    5;
      
    a++; // or a += 1;
      
    b--; // or b -= 1;
      
    if (== b) {
        echo(
    b);
      }

    And this will echo 8, because:
    At the beginning, a is 3, b is 5.
    We added 1 to a, it became 4
    We subtracted 1 from b, it became 4
    is a equals to b? Yes it is, then the script inside those curly brackets will run.
    And it echoed 4 + 4 which is 8.
    ---

    Congratulations, you now know the basics of Gs2!

    If you have understood everything from this point, then you're good to go to my next thread tutorial, where I will be covering actual fun stuff like player interactions, player objects, loops and clientside!
    If not, read it again and again and again, and ask all questions you have in this thread.
    I'd be happy to answer.
    Last edited by John; 07-16-2013 at 09:11 AM.
    -Johnaudi

  3. #3
    Veteran Striker's Avatar
    Join Date
    Jul 2013
    Location
    Britian
    Posts
    283
    Quote Originally Posted by John View Post
    a = a + 1; ?!! :
    This world wide syntax is known for programming.
    Can you calculate a = a + 1 in math? No.
    In scripting, yes you can.
    It's really simple.
    PHP Code:
    function onCreated() {
      
    5;
      
    1;
      echo(
    a);

    Let's take a deep breath...
    a is equals to 5, got it.
    now a is equals to a + 1??
    think of it this way, since a is equal to 5
    then a is equals to 5 + 1
    and a is now equals to 6.
    And this indeed echoes 6.

    What this does, is it adds x number to a.
    (remember, it's better to use temp.a and this.a but in here I'm giving an example...)

    PHP Code:
    function onCreated() {
      
    5;
      
    1;
      echo(
    a);

    Same concept, will echo 4.
    Now, it becomes really tiring for a developer to type a = a + 1; all the time, so there's another syntax you can use which is the following:
    PHP Code:
    // you can replace the number 1 by anything you want
    += 1// Adds 1 to a
    -= 1// Substracts 1 from a
    /= 1// This means a = a / 1; which is a, this is done mostly by numbers other than 1
    *= 1// This means a = a * 1; which is a, this is done mostly by numbers other than 1
    %= 1// This one means a = a % 1; what % does it the remainder of the division. I'll explain this one later
    @= 1// This one means a = a @ 1; Let's say a is noob, it will become noob1
    a++; // smaller syntax for a = a + 1;
    a--; // smaller syntax for a = a - 1; 
    So here's a small example:
    PHP Code:
    function onCreated() {
      
    3;
      
    5;
      
    a++; // or a += 1;
      
    b--; // or b -= 1;
      
    if (== b) {
        echo(
    b);
      }

    And this will echo 8, because:
    At the beginning, a is 3, b is 5.
    We added 1 to a, it became 4
    We subtracted 1 from b, it became 4
    is a equals to b? Yes it is, then the script inside those curly brackets will run.
    And it echoed 4 + 4 which is 8.
    ---

    Congratulations, you now know the basics of Gs2!

    If you have understood everything from this point, then you're good to go to my next thread tutorial, where I will be covering actual fun stuff like player interactions, player objects, loops and clientside!
    If not, read it again and again and again, and ask all questions you have in this thread.
    I'd be happy to answer.
    I understand that
    Striker*
    -Have any question, concerns or problems?
    Message me on forums, click on the link if you need help: http://era-go.com/forum/member.php?410-Striker
    Need help on iEra P.M my nick name is Striker*
    -Need further help? My email is:[email protected]
    ENjOY PLAYING IERA!!!!!
    -Gani Artist

  4. #4
    I just think of the = sign as "becomes," so that
    A becomes a's previous value plus one

  5. #5
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    812
    Quote Originally Posted by Poorface View Post
    I just think of the = sign as "becomes," so that
    A becomes a's previous value plus one
    Call it assign
    a is assigned (a's value + 1)
    -Johnaudi

  6. #6
    Yeah, we'll becomes is a smaller word! :P

  7. #7
    Quote Originally Posted by Poorface View Post
    Yeah, we'll becomes is a smaller word! :P
    becomes - become
    assigned - asign

    both got the same length the way you used it, else asign is shorter

  8. #8
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    812
    Quote Originally Posted by callimuc View Post

    becomes - become
    assigned - asign

    both got the same length the way you used it, else asign is shorter
    Assign*
    -Johnaudi

  9. #9
    Hi, I code c++ and obj-c, and I was wondering: in gs2, are the variable types assigned automatically somehow? Although this seems intuitive, it seems like it might cause some problems, I was just wondering..

  10. #10
    Veteran
    Join Date
    Jul 2013
    Location
    Hallandale, FL
    Posts
    159
    My dream was to learn gs2 and understand it. And john made it come true

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •