Page 1 of 9 1 2 3 ... LastLast
Results 1 to 10 of 88

Thread: Gs2 for the Noobs #1 - The Beginner's Guide!

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

    Post Gs2 for the Noobs #1 - The Beginner's Guide!

    Hey,
    I like writing a bunch of tutorials about stuff nobody likes, making them look more fun/easier.
    So let's get started with GS2.

    Small FAQ (YOU MUST KNOW, I KNOW THIS IS THE BORING PART):

    Q.What's Gs2?
    A.GraalScript 2, it's the language you type in, just like English, French and there's Gs2.

    Q.What can I do using Gs2?
    A.Create baddies, make calculations, create cool stuff you'll think it's boring!

    Q.Is Gs2 free?
    A.Sadly, no... You need to have a 'Gold Membership' and have to get hands on a server with the correct rights. (Can't find any servers? Post your GraalID on this thread)

    S.I've got a question!
    A.POST IT! I'd be happy to reply

    ---

    Okay, let's start.
    (if you already know how to setup your script editor, skip this step)
    Setting Up:
    • Get hands on a server's RC with weapon rights, press F6 in game, and a window like this one will popup:
    • Second, press the Script Editor button :
    • After that, select the Weapons folder, and press on (new weapon) :
    • Choose a weapon name, then press OK. (out of preference, name the weapon YourName/Test or something like that, e.g: John/TutorialTest)
    • After that, you'll have a text field on your right, that's where you can write text in! (for future references, this field is called a MultiLineEdit)


    The Coding part:

    You know the RC Chat, right? That's where the staff talks to each other... Well did you know, Graal can talk too? Yes it can, but you should make it send the text to the RC to be able to talk to you! For example:


    How did I do that?
    Scripted it!

    Okay so let me drive you into the steps one by one...
    The script that I used is:
    PHP Code:
    function onCreated() {
      echo(
    "Hey john, I'm Graal!");

    So, when you first see the stuff above you'll be like 'omg so many signs wth is the stuff I give up'.
    Let me explain it step by step.
    function is telling the compiler (the thing making your code into reality) that this script is called when this function is being triggered.
    Didn't understand? Good, cause I didn't either... But I'll be covering that after teaching you what the stuff inside that function means.
    For short, a function is a box, that has a to-do list in it...

    onCreated() is the 'key' of this box... Graal gives us a key called onCreated() as soon as you create the weapon/press Apply.
    For example, if you had 2 functions (boxes) called onCreated() and the other is called iLikeTunaYesIdo(), when you press Apply, Graal will give us a free key called onCreated(), then the script will automatically run the code in the onCreated() function/box. Yet again, I'll be covering this in the future...

    The { and } are called the curly brackets of the function, which is in fact, the parameter of the box/function. (look guys, I'm gonna call it function, just remember it's a box)... So whatever is INSIDE the brackets, belongs to the function / is inside the function. For example, when you open your fridge and see an apple, grape, beef and a murderer in your fridge, the function we will call 'fridge()' will have the stuff - and let's say you have another function called burger which contains ham, lettuce and tomatoes...
    Example:
    PHP Code:
    function fridge() {
      
    apple
      grape
      beef
      murderer
    }
    function 
    burger() {
      
    ham
      lettuce
      tomatoes

    (hope you got the concept)

    what's echo("something") ? It's a built-in graal function, which makes the thing you write (in this example, 'something') will be printed/said on RC. So basically if I change 'something' to 'HEY IT'S PIE DAY!' you'll have RC print out HEY IT'S PIE DAY! (remember to press apply)

    The ; is really important, it marks when the line is going to get read, for example, in order to make a burger correctly, you need to make it in this order: Bun, Lettuce, Ham, Other Bun.
    So the correct script for it is :
    PHP Code:
    function makeAYummyBurger() {
      
    Bun;
      
    Lettuce;
      
    Ham;
      
    Other Bun;

    (remember those are examples, these scripts will not actually work)
    This will place the Bun first, then lettuce, then the ham, then the other bun in that order.
    But if you forgot a semicolomn (;) there will be a bunch of errors, quick example :
    PHP Code:
    function makeAYummyBurger() {
      
    Bun;
      
    Lettuce
      Ham
    ;
      
    Other Bun;

    I forgot the semicolomn on the lettuce so it will be read as this:
    PHP Code:
    function makeAYummyBurger() {
      
    Bun;
      
    LettuceHam;
      
    Other Bun;

    And since Lettuce and ham cannot be mixed together, this will not work.

    All of the above was conceptualizing, if you want to do some actual coding, read the next step...

    Actual Coding:
    ** Small note:
    you can name your functions and variables (I'll talk about that) whatever you want.
    spaces do not matter, it's just for a nice coding style, so having:
    PHP Code:
    function onCreated()         {
    echo(
    "HIII");
                                                                      echo(
    "ELLO");






    Is the same as having :
    PHP Code:
    function onCreated(){echo("HIII");echo("ELLO");} 
    end of small note**

    Let's say we want to make Graal tell us what 1 + 3 is equal to, so what we have to do is:
    PHP Code:
    function onCreated() {
      echo(
    3);

    And this will print out 4 on the RC Chat.

    WARNING, if you use " " it will print out the text inside the brackets, for example echo("1 + 3"); will echo 1 + 3 not 4.

    Let's try another one, shall we? I want to know what 8 - 5 does, so:
    PHP Code:
    function onCreated() {
      echo(
    5);

    And this will print out 3...

    2 x 5 ? In scripting, times is not used by an 'x', it's used by that star you get by pressing Shift + 8 (*).
    Most likely echo(2 * 5); which will give 10;
    8 divided by 2, in scripting, the division sign is expressed by a slash (/).
    so echo(8 / 2); which is 4...

    But let's say I want to mix text with calculations, how can I do that?
    Here's an example:
    PHP Code:
    function onCreated() {
      echo(
    "Your number is "1);

    This will echo Your number is 2
    Don't panic!! The sign @ is used to connect strings (or chat if you want to call it like that)! For example, if I had echo("John" @ "Audi"); it will print out JohnAudi. There is also another sign called SPC, this is used for SPACES instead of @, take this as an example: echo("John" SPC "Audi"); will echo John Audi not JohnAudi (if this does not work, stick with having a space like echo("John Audi"); cause it doesn't matter really...).


    Remember in math class when they thought you about variables? For example, a variable called a = 5 so 1 + a = 6... That's what basically the whole idea of scripting is all about (well most of it)!
    Let's say I have a = 5; and I want it to echo a, what do to?
    PHP Code:
    function onCreated() {
      
    5;
      echo(
    a);

    WARNING, if you echo("a"); it will send the text 'a', if you echo(a); it will send the number 5 which is the value given to a.

    **Small Note:
    you can place comments inside your script to be able to find what you want easier, or for other developers to know what your doing.
    for example:
    PHP Code:
    function onCreated() {
      echo(
    "HI!"); // this will echo hi!

    the stuff on the right of these double slashes '//' will not be compiled, so only the stuff on the left.

    Another type of comments is the /* and */. What this does, is a 'Comments Brackets'... So everything in between those brackets will not be read by the compiler/the thing that does our script work.
    for example:
    PHP Code:
    function onCreated() {
      echo(
    "PIE PIE PIE!");
      echo(
    5);
    /*
      echo(4 - 2);
      echo("beef");

    the stuff in here will not be read, so the only things being echoed are PIE PIE PIE! and the number 8.
    */

    end small note**


    Did you know that a variable can contain calculations too?
    Let me name this variable 'i' and make it equal to the calculation of 3 + 2.

    PHP Code:
    function onCreated() {
      
    2;
      echo(
    i);

    Having i = 5, will echo 5.
    You can also have something like this:
    PHP Code:
    function onCreated() {
      
    1;
      echo(
    1);

    And this will echo 4... BUT, 'i' still have the value of 3, we only echoed i + 1, but we did not say i = i + 1;
    Confused? Cool, let me dive you trough that.

    Let me start by a basic example :
    PHP Code:
    function onCreated() {
      
    abc 4;
      
    abc 2;
      echo(
    abc);

    What will it echo? 4 or 2?
    It will echo 2, why? Here's how the compiler thinks:
    Hi, I'm the compiler.
    Let's see what I got here...
    PHP Code:
    function onCreated() { 
    Okay, so there's a function called onCreated(), I will give you a free trigger (key) for it! There you go...

    PHP Code:
    abc 4
    Alright, you want me to assign the number 4 to the variable abc, sure, *magic trick*... boom, now abc is 4, have a nice day!

    PHP Code:
    abc 2
    Hm, you already told me abc is 4, but if you want me to change that, I'm good with that, *more magic trick* and bam, abc is now 2.


    PHP Code:
    echo(abc); 
    You want me to print the value of abc? What was abc again? Oh right, 2, there you go... *prints the number 2 on RC*
    ---

    You can use more variables in the same script, you're not limited to a few!
    PHP Code:
    function onCreated() { // the name of the function
      
    tuna 35// tuna is now 35
      
    beef 4// beef has the number 5 assigned to it
      
    pork 1// and pork is 1 ...

      
    echo(tuna / (beef pork)); // omg what's this sorcery! It printed 7!
    }
    /*
    Let me explain what happened above
    In math class, you know that multiplications and divisions happens before the additions and subtractions.
    Like 2 * 3 + 4 / 2 does 2 * 3 first, then 4 /2 , the it adds them together, more of an example: (2 * 3) + (4 / 2)...
    (remember parenthesis's are met to be calculated first)

    so why did I have (beef + pork) in parenthesis? Because if I do not add them, it will divide tuna with beef, then add pork to it.
    So, echo(tuna / (beef + pork)); is like echo(35 / (4 + 1));

    */ 
    (check next post)
    Last edited by John; 06-05-2014 at 11:43 AM.
    -Johnaudi

  2. #2
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    **small tip: Whenever a bracket starts, it must end, same thing with the parenthesis...
    So { must have a }
    " must have another "
    ( and ) etc.
    end small tip**

    I told you guys you can name your variables whatever you want, well, not really.
    Try to avoid having space in your variable names, you can't have john audi = 2; but johnaudi = 2; will work.
    Try not to name your variable a number, you can't do 4 = "bacon"; (I'll go wider into strings in another tutorial).
    Some variables have already assigned numbers, like x, y, z, zoom, image, timevar2 etc...
    You can use them, but make sure you're not the only one that can manipulate them... (timevar2 is read only, you can't set timevar2 = 3; cause timevar2 is the amount of seconds when UNIX first started, will teach you that later)

    The if statement:
    The if statement is one of the crucial statements in scripting, you MUST know this one to finish learning your noob stage.
    What the if statement is, it's a small box, in your big function box, that has a to-do list in it...
    So some of you will think it's a function inside a function ... ? No, we're not in Inception where there's a dream inside the dream, not that complicated... What do you use 'If' for in your daily routine?
    Here's an example...
    Hi john, if 2 is less then 3, I'll give you a cookie...
    The script for that will be:
    PHP Code:
    function onCreated() {
      if (
    3) { // the if statement is written like this : if (test, checks if the test is true, if yes, it will run the code inside the box/brackets
    // You'll find new operators in here, in math class we all know what < and > is, right? If you do not, I'm sorry to tell you that this tutorial is not made for you...
    //So is 2 less then 3? Yes, yes it is, okay, since it is less than 3, let's run the code inside the brackets which is echo("Here's your cookie!");
        
    echo("Here's your cookie!");
      }

    This will print out Here's your cookie! since 2 is in fact less than 3.
    But is 2 greater than 3? No it's not!
    PHP Code:
    function onCreated() {
      if (
    3) { // this test is false, that means it is not true, and since we 'lied' to the compiler, he will not run the script inside the brackets for us! bad compiler!
        
    echo("COOKIE!");
      }

    And this will echo nothing...

    So, I've been told I'll get another cookie if 3 is equal to 6 divided by 2, so you guys would be like 'Hey john, I know this one! you just have to put if (3 = 6 / 2) right?'
    Well, WRONG, = is used to assign a variable to a number, such as x = 4; t = -3; nub = 0; etc, but if you're checking 'if' something is 'equals' to another, you have to use '==' not '='... Yes this is confusing, but play with it at first so you can understand.

    So basically:
    PHP Code:
    function onCreated() {
      if (
    == 6/2) {
        echo(
    "stuff");
      }

    ---
    As you can see, there are some exceptions in the if() statement, but it's easy to use!
    == is used to check if two things are equal
    < is used to check if the first var is smaller than the second var
    > if first var is bigger than second var
    <= if first var is less or equal to second var
    >= if first var is bigger or equal to second var
    != if first var is NOT equal to second var
    remember that ! means NOT.
    Why isn't it !==? I don't know, take this out with most of the scripts used these days
    Logical Operators:
    Yes, It's beginning to get boring, but in my next threads I'll teach you guys how to have some player interactions once you understand these important stuff, and your programming will become a whole lot easier.

    What are these logical operators?
    Let's say for example, I want something to echo if 1 + 1 equals 2, and 2 is less than 4.
    Two ways to do that, you can place an if statement inside an if statement, or use a logical connector.
    Example of 2 if statements:
    PHP Code:
    function onCreated() {
      if (
    == 2) {
        if (
    4) {
          echo(
    "test");
        }
      }

    And here's how the compiler thinks:
    Oh, john, we meet again! So what do you have for me?

    PHP Code:
    function onCreated() { 
    Oh yes, the onCreated() function, we should call this now since I can trigger it for free at the beginning!

    PHP Code:
    if (== 2) { 
    Wait, doing some math, does 1 + 1 actually equals 2? Hmmmm, Yes, since 2 == 2 so, there, you can access the code inside the if statement, have fun!

    PHP Code:
    if (4) { 
    Another if statement? Cool! if the last result was false, I wouldn't have seen that... Is 2 < 4? yes, it's true. So what's inside these brackets?

    PHP Code:
    echo("test"); 
    Okay john, you drove me all the way to simply echo 'test'? as you wish... *sends the text 'test' to RC*
    Consider the if statements as a Guru, if the answer is correct, you shall see what's inside the box!

    So, what's a logical operator? Without having to put 2 if statements, you can easily put the logical operator '&&' which means and.
    so you'd have:
    PHP Code:
    function onCreated() {
      if (
    == && 4) {
        echo(
    "test");

    So what it did, is check the operation on the left, and check the operation on the right. If the operation on the left AND the operation of the right is TRUE, the compiler will read the script in the box/brackets. But if one/ or two of them is false, the compiler will not read the stuff inside this statement.

    Another logical operator is called '||' or simply OR. This will check if one of them is 'true'.
    so
    PHP Code:
    function onCreated() {
      if (
    == || 3) { // 1 + 1 is not equal to 3, so this is false, but 2 is in fact less than 3, so that's true, since there's 1 true, let's let the nice compiler read the script
        
    echo("test"); // and it will echo test
      
    }

    ---

    else:

    You guys remember that an if statement needs to return 'true' if it's correct? But what if it's not?
    Let's talk this out... John, I'll give you a cookie if 1 + 2 is 4, but if not, you're going to cry...

    The script of that would be:
    PHP Code:
    function onCreated() {
      if (
    == 4) {
        echo(
    "Here's your cookie!");
      } else {
        echo(
    "*cries* :(");
      }

    And this will only echo '*cries* '... so what it does is, check if the if() statement is true, if it is, it will read the stuff inside if without reading the stuff inside else, but if it's not, it will read the stuff inside else.

    Maybe reading that isn't as practical as trying it, go ahead, copy some of those scripts, tweak with them a bit, and check out how it works.

    Using if's with variables:

    Programmers and developers almost never use two numbers in an if() statement, since it will cause it to calculate them for no reason, but they use the if() statement for variables, for example...
    PHP Code:
    function onCreated() {
      
    john 3;
      if (
    john == 3) {
        echo(
    "yay!");
      } else {
        echo(
    "nub");
      }

    Which will of course echo 'yay!'...
    Remember that we can put two or more variables in an if statement:
    PHP Code:
    function onCreated() {
      
    john 5;
      
    tuna 2;
      
    burger 2;
      if (
    john != && tuna == burger) {
        echo(
    "wow cool!");
      } else echo(
    "that didn't work x.x");

    and this will echo 'wow cool!'...
    If you're wondering while I didn't add the {} to the else, is because everything that needs brackets (function something(), if(), else, others you'll learn later) can have it's bracket removed and replace in one line, for example:
    PHP Code:
    function onCreated() 
      if (
    == 2) echo("hi"); 
    The compiler will automatically place brackets for the next semicolomn, so if you have:
    PHP Code:
    function onCreated() {
      if (
    1== 3) echo("hi"); echo("sup");

    the compiler will read it as:
    PHP Code:
    function onCreated() {
      if (
    == 3) {
        echo(
    "hi");
      }
      echo(
    "sup");

    But this is more of a styling code, if you didn't get that last part, it's not that important, you can keep using the {} which is better to keep track of your code...

    So, basically, you know the major basics of the if statement, I'll see you guys later on my next tutorial!

    Any questions are welcome!
    Last edited by John; 07-15-2013 at 07:13 AM.
    -Johnaudi

  3. #3
    Veteran Ek CM Bolivar's Avatar
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    423
    I have no server access, Graal1009627.
    How to contact me?

    In-game name currently: ZombiEk" Ciprioni
    Current gang owned: Mayhem
    Skype: ek_graal
    Facebook: Shannon Gallagher
    Email: [email protected]
    Push notifications: enabled

    Ask me any questions/concerns!

  4. #4
    Interesting to see new coders in the GraalOnline community! I'd love to assist, so ask me any questions as well as John.

  5. #5
    This looks nice.
    Also, do you need gold to get on your server?

  6. #6
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Quote Originally Posted by Poorface View Post
    This looks nice.
    Also, do you need gold to get on your server?
    Yes you do, but that's like free if you work for an iOS server / only 5$...
    -Johnaudi

  7. #7
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Quote Originally Posted by Ek CM Bolivar View Post
    I have no server access, Graal1009627.
    Added to server called Fyse.
    Added you with Weapon rights of weaponname EK/*.
    So name your weapon EK/Test or EK/Noob stuff like that.
    YOU NEED GOLD.
    -Johnaudi

  8. #8
    Veteran Ek CM Bolivar's Avatar
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    423
    ok thanks getting gold.
    How to contact me?

    In-game name currently: ZombiEk" Ciprioni
    Current gang owned: Mayhem
    Skype: ek_graal
    Facebook: Shannon Gallagher
    Email: [email protected]
    Push notifications: enabled

    Ask me any questions/concerns!

  9. #9
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Updated thread.
    -Johnaudi

  10. #10
    Ex. Player Relations Admi RenoDorvay's Avatar
    Join Date
    Jun 2013
    Location
    Melboune, Australia
    Posts
    866
    This is awesome, good stuff John. This is the kind of thing the community needs

Posting Permissions

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