Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 33

Thread: Gs2 for the Noobs #3

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

    Gs2 for the Noobs #3

    Hey again guys, today I'll be covering most of the important parts in Gs2 under this tutorial.
    Please make sure you have read and understood all the previous tutorials, or else you'll get confused and stuck, I'm not going to re-explain what stuff like trigger, array, parameters, and stuff like that means in this tutorial, since I have so much stuff to get into details with.

    The known Types:
    A type can either be a string (words, letters, sentences, array etc), a number.
    Why do you need to learn them? Just because if you're reading a documentation in graal.net you will see something like:
    savevars(str, int)
    You don't need to know what the savevars() function does now, but what I'm focusing on is it's parameters.
    When it says str, it means string (str is short for string), what this means it wants a string in this parameter.
    int means integer, which is a number with NO decimals (not 2.352363 but simply 2)

    Here's a small table:
    prefix - type - example - what is it about

    str - string - "Hello World" - letters/words/sentence everything in between " " or a variable that contains a string
    int - integer - 34 - any number with no decimals (1 - 4 - 7 - 6 - 32 - 54 - 19 etc...) if you put 3.67 it will think you put 3.
    float - float - 2.74845685484 - a number with decimals (2.573485642 or pi etc...)
    bool - boolean - true/false - I'll go more into this one, it's basically a true/false statement (if you're wondering, true = 1 and false = 0)
    obj - object - player - can be anything of the above... A player is an object.
    So what is the parameter of echo(parameter) ? It's an Object / string. Since Graal is an object oriented game, a string can be an int/float and vice versa...

    You might've not understood correctly, but I'll explain and go deeper into this later.

    sleep():
    Just out of curiosity, did you ever want RC to echo() something after 2 or 3 seconds? Did you ever want to add some kind of time limit?
    Well, sleep(float) is your friend! What sleep does, is make the script 'wait' x seconds before continuing.
    Here's an example:
    PHP Code:
    function onCreated() {
      echo(
    "I'll tell you a secret after 5 seconds!");
      
    sleep(5);
      echo(
    "I'm not a boy!");

    So what RC printed is:
    I'll tell you a secret after 5 seconds!
    (and after you wait 5 seconds, RC printed)
    I'm not a boy!

    Simple enough! Now, what is this useful for? I'll tell you later with loops!

    Player Objects:
    Graal has a built-in function to find players, consider this function as a GPS. It's name is findplayer(str)...
    The string parameter is the player's GraalID.
    So my GraalID is Graal804226. So if I want to find my player, all I have to do it findplayer("Graal804226");
    But john, what's the point of finding a player?!
    Here's the fun part you've all been waiting for, you can actually modify your player!
    Remember what I explained to you about the does? ( ... ) ? That's it's an address.
    So what you have like echo(johnaudi.phonenumber); this will echo my phone number... (Don't try that girls, contact me if you're interested )
    What a player actually is, is a main address of something.
    WARNING: findplayer(GraalID) only works if the player is ONLINE, if you find an offline player nothing can be read in his addresses.

    Let's say I want to echo my name! :
    PHP Code:
    function onCreated() {
      
    temp.johniscool findplayer("Graal804226"); // temp.johniscool contains all the data that Graal804226 has (which is me)
      
    echo(temp.johniscool.nick); // player.nick is a built-in variable that shows the player's nick

    And this will echo my nick which is John.
    Another way to do that is:
    PHP Code:
    function onCreated() {
      echo(
    findplayer("Graal804226").nick);

    This will echo John too...

    So let's say I want to change my nick to Jeremy.
    What should I do?
    Here's the method:
    PHP Code:
    function onCreated() {
      
    temp.johnny findplayer("Graal804226");
      
    temp.johnny.nick "Jeremy";

    Then I check in game, and my nick has been changed to Jeremy! Sweet!
    Another way to do that:
    PHP Code:
    function onCreated() {
      
    findplayer("Graal804226").nick "Jeremy";

    Cool huh?

    Okay so nicknames are starting to get boring, don't you like it when you have a gold name and act like you're an admin?
    the built-in variable for the name color is 'ap' and when it's 100 the name will be gold.
    PHP Code:
    function onCreated() {
      
    findplayer("Graal804226").ap 100// you can change 100 to anything. but 100 is the Gold name.

    And my name color is now gold, wohooo!! I'm admin!
    Remember that you can use the different method, storing findplayer() in a variable and use the variable.

    Let's say I want my guild tag to be Events Team, and my name color to be gold (ap = 100) and my name to be "Cool Guy", what we do is:
    PHP Code:
    function onCreated() {
      
    findplayer("Graal804226").guild "Events Team";
      
    findplayer("Graal804226").ap 100;
      
    findplayer("Graal804226").nick "Cool Guy";

    But as you can see, it's a bit ugly to keep writing findplayer at this point, so let's store it in a variable instead.
    PHP Code:
    function onCreated() {
      
    temp.pl findplayer("Graal804226"); // I named it temp.pl because it's short for player
      
    temp.pl.guild "Events Team";
      
    temp.pl.ap 100;
      
    temp.pl.nick "Cool Guy";

    And yes, all this works perfectly fine...

    There's also player.chat (you known that overhead chat we use all the time)
    More player objects like player.head
    player.body
    player.shield
    player.attr[1] (this is the player's hat)

    Let's match it all, shall we?
    PHP Code:
    function onCreated() {
      
    temp.john findplayer("Graal804226");
      
    temp.john.nick "JOHNN!!";
      
    temp.john.guild "Noobs For Life";
      
    temp.john.ap 99// 99 means blue
      
    temp.john.chat "Hey guys!";
      
    temp.john.head "head2.png";
      
    temp.john.body "body5.png";
      
    temp.john.attr[1] = "hat7.png";
      
    temp.john.shield "shield5.png";



    You can also warp the player to a specific levels, using player.setlevel2("levelname.nw", 50, 30);
    I'll be covering the setlevel2(levelname, x, y); maybe later in another tutorial...

    Loops:
    This is where some people get confused, but it's really easy to use, and makes your program's script a bunch easier to debug/read.

    What's a loop? It's when you trigger something that will go over and over and over and over again until reached a point to stop.
    Alright, didn't understand, right?
    Let me teach you what a timeout loop is.
    In Graal, there's a function called setTimer(float);
    What this hidden function does:
    PHP Code:
    function setTimer(num) {
      
    sleep(num);
      
    onTimeOut();

    onTimeOut() is a function you should create, this will get called after a certain amount of seconds...

    PHP Code:
    function onCreated() {
      echo(
    "I'll call the timeout function in 3 seconds");
      
    setTimer(3);
    }

    function 
    onTimeOut() {
      echo(
    "onTimeOut() has been triggered!");

    Here's how the compiler reads it:
    Hey John! I miss you! Where have you been?

    Let me trigger the onCreated() function for you...
    PHP Code:
    echo("I'll call the timeout function in 3 seconds"); 
    You want me to echo that? sure! *sends the text to RC*
    What's next...
    PHP Code:
    setTimer(3); 
    What's setTimer(3) ? Let me check in my hidden functions... oh, it says that I should trigger onTimeOut() after 3 seconds, okay, I'll wait 3 seconds, I got nothing to do, right...?

    *3 seconds has passed*
    Okay, time to go to work, what was I going to do? Oh yes, trigger onTimeOut(). Let's do that!
    Let's see what's inside onTimeout()
    PHP Code:
    echo("onTimeOut() has been triggered!"); 
    You want me to echo that? Okay.... *echoes it*

    My job is done, see ya later john!
    That's a loop john? That seems easy... No, no that's not a loop.
    What will happen if I do the following:
    PHP Code:
    function onCreated() {
      
    setTimer(1);
    }

    function 
    onTimeOut() {
      echo(
    "Hi!");
      
    setTimer(1);

    What will happen is...
    It will wait 1 second, then read the onTimeOut() function
    In there, there is echo("Hi!"); - so it will echo Hi! on RC.
    After that, there's a setTimer(1), so it will call onTimeOut() after 1 second.
    Once it calls it back, it will echo("Hi!")
    Then it will read setTimer(1) and calls onTimeOut() again after 1 second.
    Once it calls it back, it will echo("Hi!")
    Then it will read setTimer(1) and calls onTimeOut() again after 1 second.
    Once it calls it back, it will echo("Hi!")
    Then it will read setTimer(1) and calls onTimeOut() again after 1 second.
    Once it calls it back, it will echo("Hi!")
    Then it will read setTimer(1) and calls onTimeOut() again after 1 second.
    etc...

    It will keep happening every one second... Got how this works?
    I call this the timer loop.

    Another type of loop is called the while() {} loop.
    What this will do, is run a the loop IF the condition in the while() loop is true.
    For example...
    PHP Code:
    function onCreated() {
      
    temp.something 1// temp.something is now equals to 1
      
    while(temp.something 10) { // while temp.something is less than 10, keep recalling the script inside the curly brackets 
        
    echo(temp.something); // this will echo how much temp.something is
        
    temp.something += 1// or temp.something++; and this will add one to temp.something, so when it reachs something more than 9, the while loop will stop running
      
    }

    This will echo :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    CAUTION: I'd prefer if you add a sleep() in your while loops, because this might crash the server if it's too fast... I take no responsibility for the server's death.
    I know this loop thing is kinda new to you, but try to understand it by actually trying it...
    (check next post)
    Last edited by John; 07-17-2013 at 11:20 AM.
    -Johnaudi

  2. #2
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Another loop is the for() loop.
    PHP Code:
    function onCreated() {
      echo(
    "Starting loop!");
      for (
    temp.1temp.10temp.i++) {
        echo(
    temp.i);
        
    sleep(1); 
      }
      echo(
    "loop ended!");

    What this will do is the same as having:
    PHP Code:
    function onCreated() {
      echo(
    "Starting loop!");
      
      
    temp.1;
      
      if (
    temp.10) {
        echo(
    temp.i);
        
    sleep(1);
        
        
    temp.i++;
      }
      
      if (
    temp.10) {
        echo(
    temp.i);
        
    sleep(1);
        
        
    temp.i++;
      }

      if (
    temp.10) {
        echo(
    temp.i);
        
    sleep(1);
        
        
    temp.i++;
      }

      if (
    temp.10) {
        echo(
    temp.i);
        
    sleep(1);
        
        
    temp.i++;
      }

      if (
    temp.10) {
        echo(
    temp.i);
        
    sleep(1);
        
        
    temp.i++;
      }

      if (
    temp.10) {
        echo(
    temp.i);
        
    sleep(1);
        
        
    temp.i++;
      }
     
    // etc...

      
    echo("loop ended!");

    Loops are really important in scripting, makes your lines shorter and easier to read!

    ---

    Clientside:
    In all online games, there is a client, and a server, right?
    So what's a client? That's basically what you can see. What only you can see (unless you're setting a certain data for both, client and server)

    You won't understand at this point.

    Can a player talk with the server? Or should he be able to talk with the client?
    A player talks to the client, and the client sends data to the server...
    Okay, confused, I know...
    In clientside, the only player is YOU.
    So basically, the default player variable is called 'player', you can do player.chat and stuff like that on clientside.

    To introduce CLIENTSIDE to the compiler, you should have //#CLIENTSIDE
    BTW, //# is not a comment, # means include...
    So soon once Gs3 is out, you're going to include it in the script using //#GS3

    PHP Code:
    // The script on the top is for serverside
    //#CLIENTSIDE
    // The script on the bottom is clientside 
    You must know that not everything that works on serverside, will work the same on clientside...
    So if you have echo() on serverside, it will echo to RC, right?
    but in clientside, it will echo to the f2 window under script tab, not RC!
    But first, you must add the weapon to your character using the function addweapon().
    So all you have to do is add this script in the onCreated() function :
    PHP Code:
    function onCreated() {
      
    findplayer("Graal804226").addweapon(name); // name is the name of the weapon, it will automatically put it
    }
    //#CLIENTSIDE
    function onCreated() {
      echo(
    "Hello World!");



    ---

    Once the weapon has been added to your player, there is a built in function that gets recalled everytime YOUR player speaks.
    The function name is called onPlayerChats().
    For example:
    PHP Code:
    //#CLIENTSIDE
    function onPlayerChats() {
      echo(
    "Player said something!");

    And every time you say something, the function will get triggered.

    Let's try to print what the player have said:
    PHP Code:
    //#CLIENTSIDE
    function onPlayerChats() {
      echo(
    "Player has said: "player.chat);

    player.chat is the variable for the player's overhead saying's...

    You can also use it as siri!

    PHP Code:
    //#CLIENTSIDE
    function onPlayerChats() {
      if (
    player.chat == "Hi graal") {
        
    player.chat "hello";
      }

    So every time you say Hi Graal, your chat will turn into hello... Fun isn't it?

    Alright time to go a little more advanced, we're going to make a better looking siri, shall we?
    PHP Code:
    //#CLIENTSIDE
    function onPlayerChats() {
      if (
    player.chat == "hi") {
        
    player.chat "Hello!";
      } else if (
    player.chat == "how are you?") {
        
    player.chat "I'm fine, how are you?";
      } else if (
    player.chat == "what is 1 + 1") {
        
    player.chat "1 + 1 is "1;
      } else {
        
    player.chat "Sorry I don't know what you're saying!";
      }

    Here's a smarter Siri, but it looks ugly with all the if() and stuff like that, what can we use? Let's use the switch() command!
    Not sure if I already explained how it works, but I'm going through this again.
    PHP Code:
    function onCreated() {
      
    temp.number 3;
      switch(
    temp.number) { // switch(variable) is the enclosure for if (variable ==
        
    case 6// case 6: means 6) , so basically what we have now is if (variable == 6)
          
    echo("COREECT"); // this code between the case and the break will run if the statement is true, if not, iw I'll check the one after that
        
    break;
        case 
    7:
          echo(
    "why is it 7?");
        break;
        default: 
    // this is the IF NONE OF THE ABOVE IT TRUE, RUN THIS CODE
          
    echo("No answer!");
        break;
      }

    Just a quick overview.
    Now let's apply this to the example given above:
    PHP Code:
    //#CLIENTSIDE
    function onPlayerChats() {
      switch(
    player.chat) {
        case 
    "hi":
          
    player.chat "Hello!";
        break;
        case 
    "how are you?":
          
    player.chat "I'm fine, how are you?"
        break;
        case 
    "what is 1 + 1":
          
    player.chat "1 + 1 is "1;
        break;
        default:
          
    player.chat "Sorry I don't know what you're saying!";
        break;
      }

    That's pretty much it...
    If the next tutorial I will be covering some more player interactions and GUI building, so stay tuned!
    Last edited by John; 07-28-2013 at 03:49 PM.
    -Johnaudi

  3. #3
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Answering any questions, because I'm sure you guys got some!
    -Johnaudi

  4. #4
    Well I'm receiving some questions due to players not understanding anything... lol

  5. #5
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Quote Originally Posted by callimuc View Post
    Well I'm receiving some questions due to players not understanding anything... lol
    lol, let them pm me.
    -Johnaudi

  6. #6
    i never understood a thing... imma nub :c

  7. #7
    I think others would follow better if you would put it in one thread.

  8. #8
    Quote Originally Posted by The Doctor View Post
    I think others would follow better if you would put it in one thread.
    or he could put links to the other threads in each new one or in his signature

  9. #9
    Veteran Iceman's Avatar
    Join Date
    Jun 2013
    Location
    Chiraq
    Posts
    245
    This is basic Java or C programming. The only hard part would be to memorize the functions and specific objects you have created. Can you define your own objects/functions or does someone have to choose from some sort of library?

  10. #10
    Yes, you can write your own

Posting Permissions

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