Results 21 to 30 of 33

Thread: Gs2 for the Noobs #3

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Posting Permissions

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