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

Thread: Advanced Gs2 for the Noobs #1

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

    Advanced Gs2 for the Noobs #1

    Hey guys,

    I hope you have read my previous tutorials and understood everything from it, I do not advise reading this tutorial if you still did not perfect the stuff taught in my previous tutorials.

    We will be covering in this series, advanced Gs2 functions, how bytes/bits work, more interactivity with the player, more about DBs, weapons, classes, and covering crucial stuff to know when using loops. We will also focus on maintaining the least lag possible.

    So let's get started!

    bits and bytes:

    (If you think this is difficult, you can skip it, it's un-needed for Graal)

    This is a core concept in all computer programming languages, you have to know this, it might not be THAT important for Gs2, but quite important for the real world!

    It's a bit boring though? Who cares! You get bored at school, don't you?

    Now, how do you think temp.something can be equal to 10? If I had temp.a = 10; our main question would be, who taught the computer what the number '10' is? Nobody cares, but what is important, is what is hidden under these numbers.

    These numbers, letters, and everything else, is written under binary numbers.

    You know in these hacking movies, you see the numbers 0101010100101011011100 etc? These, are called binary numbers...
    How to understand this is the following (this might be wrong the way I say it, but I'm trying to make it more understandable):

    1 Byte, contains 8 bits. (not always)

    A representation of a bit is used by the numbers 0 and 1.

    ... (27)(26)(25)(24)(23)(22)(21)(20)
    In other words:
    ... 128 64 32 16 8 4 2 1


    Let's say we have the binary number 00001011, what will this become in normal decimal numbers?

    128 64 32 16 8 4 2 1
    0 0 0 0 1 0 1 1

    You do : 128x0 + 64x0 + 32x0 + 16x0 + 8x1 + 4x0 + 2x1 + 1x1 = 8 + 2 + 1 = 11

    So 00001011 is equivalent to the number 11.

    Another example with 1010001.

    128 64 32 16 8 4 2 1
    0 1 0 1 0 0 0 1

    You do: 64 + 16 + 1 = 81.

    So 1010001 is equivalent to 81.

    Just Google about this, it will help you a bunch.

    Now what's the point of all this? You'll see.

    bit-wise operators:

    Till now, we've learned about the operators +, -, *, /, @, ^(logical) and %... And <, >, >=, <=, &&, ||, !=, ==.
    I'm here now to introduce you guys to 4 new operators!

    xor (Bitwise XOR operator, it's mainly used as ^ but graal has it as logical)
    ~ (Bitwise NOT operator)
    | (Bitwise OR operator)
    & (Bitwise AND operator)
    << (Bitwise Left Shift)
    >> (Bitwise Right Shift)
    So, what do these operators do? I'm going to explain all of them, let's start with the easiest ones:

    <<: What is this? It looks like two less-than signs, it's pointing to the left.
    What << will do is the following:
    PHP Code:
    function onCreated() { 
      
    temp.<< 1// 2 means 00000010
      
    echo(temp.a); // and this will echo '4'.

    Here's an explanation. At the beginning, temp.a is equals to 2, which is 0010 in binary numbers, then we made 2 << 1, which means we will need to move all the numbers one time to the left, meaning we need to add a zero to the end: 0100
    Having the binary numbers 0100, when you turn it to decimal it will end into: 8x0 + 4x1 + 2x0 + 1x0 = 4.

    Let's have another example, the number 10 (which is 00001010), if we do 10 << 2, we need to add 2 zeros or move everything to times to the left, so it will become 00101000.

    Same concept goes with >>, which is to the right (where you remove zeros or ones): 10 >> 1 will become 00000101. And 10 >> 2 will become 00000010.

    Now we're done with the shift operators!

    The NOT (~) operator, is more like the ! one.
    But what this one does, it will change all 0s into 1s, and all 1s into 0s...

    For example:
    PHP Code:
    function onCreated() {
      echo(~
    2); // Will echo -4

    Why? Okay, here we go into negative numbers... Negative numbers do not start by zeroes, they start by ones, and end by -1.

    Example, -4 is : ...11111101

    It's a hard way to explain how negative numbers work, but here's how the ~ work: All numbers are inverted.

    For example:
    0010101101010110010101010101001110101101010011 will become:
    1101010010101001101010101010110001010010101100

    and
    11110111 will become
    00001000

    See what I mean?
    I'll give you an example on how negative binary numbers work when I explain the bitwise OR operator.

    Now time to explain the bitwise OR ( | ), what this does is the following.

    You put two numbers, 2 and 3 for example, and use the OR statement on them:

    PHP Code:
    echo(3); 
    2 is 0010 and 3 is 0011
    What will happen is add anything that is 2 | 3 =
    For example:
    0010 with
    0011 will give you:
    0011

    Another example, with 8 (1000) and 6 (0110) so 8 | 6 =
    1000
    0110 will give you:
    1110

    It's quite easy, now for the AND (&) operator, it will check if there is the same, if the number 1 is not available in both, it will not add it.
    Let's take for example 2 and 3, so 2 & 3 =
    0010
    0011 will give:
    0010

    Another example with 4 (0100) and 7 (0111) so 4 & 7 =
    0100
    0111 will give:
    0100


    For the XOR element, well, it only outputs IF they are NOT the same.
    Example: 2 xor 3:
    0010
    0011 gives:
    0001

    Another example with 4 (0100) and 7 (0111) so 4 xor 7 =
    0100
    0111 gives:
    0011


    If you're interested in negative binary numbers, Google them!

    Now, if you have understood NOTHING in this section, it's okay! You won't need it for Graal anyways, Graal's variables are equals to the max int value so it's not a good language to use for such stuff.

    You can also use >>=, <<=, |=, &= etc...

    If you're up to understand this, then it would be a piece of cake understanding Gs2!

    To help you guys out with finding the binary numbers or integers, I've made you two functions that can help you:
    PHP Code:
    public function bin2num(str) {
      
    temp.nm 0;
      
    temp.bts str.length();
      
      for (
    temp.ch 0temp.ch str.length(); temp.ch++) {
        
    temp.bts--;
        
        
    temp.nm += (str.charat(temp.ch) == "0" temp.bts);
      } return 
    temp.nm;
    }

    public function 
    num2bin(nmbits) {
      
    temp.num_lft nm;
      
      if (
    bits == ""bits 8;
      
      
    bits min(128bits);
      
      
    temp.bin_str "";
      for(
    temp.bits 1temp.>= 0temp.i--) {
        
    temp.calc temp.num_lft - (temp.i);
        
        if (
    temp.calc.pos(".") == -&& temp.calc >= 0) {
          
    temp.bin_str @= "1";
          
          
    temp.num_lft temp.calc;
        } else 
    temp.bin_str @= "0";
      } return 
    temp.bin_str;

    Copy these functions to your script, and try echo(bin2num("0010")); and it will echo the number 2. You can do echo(num2bin(3, 4)); (this means get the number 3 in binary numbers using 4 bits => equivalent to 0000 or 0011 etc, having 4 digits.) and this will echo 0011, if you had 5 bits it will echo 00011.

    public and private functions:

    There are several type of functions, Graal uses public and private, other programming languages use 'protected' but since Gs3 isn't out yet, I'm not sure if they will include 'protected' in a class.

    A private function, is a function available for the weapon itself, only the weapon can trigger it. BUT, in some cases, another weapon can trigger it, though, that's not good training. (for example, onActionFunction() can be triggered from triggeraction()).

    Here's an example of a private function called 'IAmNumberOne()':
    PHP Code:
    function onCreated() {
      
    temp.something IAmNumberOne();
      echo(
    temp.something); // Echoes 1.
    }

    function 
    IAmNumberOne() {
      return 
    1// It just returns the number '1'

    Private functions are being used like the way we have used them before, by the following syntax: function name(parameters) {...}
    So a private function, is technically a normal function.

    What's a public function? A public function, is quite self explanatory, it's public! Anyone can access it, even the weapon itself.

    Let's make a class called 'toes'.
    I've made a script inside this 'toes' class called 'IAmNumberOne()':

    PHP Code:
    function IAmNumberOne() {
      echo(
    "The number one!");

    Then I created a weapon, and inputted this script in it:

    PHP Code:
    function onCreated() {
      
    temp.something.join("toes"); // Joined the variable to the class, now it contains EVERYTHING the class has!

      
    temp.something.IAmNumberOne(); // Now it's supposed to call the IAmNumberOne() function

    Wait! There's a problem here! We've received this error: Script: Function something.IAmNumberOne not accessible at line # in script of SomeWeapon

    It doesn't have enough accessibility, so what we're going to do, is go to the class and add the 'public' keyword before the word 'function':

    PHP Code:
    public function IAmNumberOne() {
      echo(
    "The number one!");

    And run the weapon again, and you can see it has echoed.

    The syntax would be: public function name(parameters...) {...}

    It is mostly used in databases, so we can add and remove a variable from a weapon/class/npc.

    findimg() and the tshowimg object:

    There's a function called findimg(index), where you can find the image and set a certain bitmap/image on it, with a certain location, color, alpha etc.

    The findimg() function is available only on clientside.

    So let's go ahead and make a weapon, and put a basic findimg() script:

    PHP Code:
    //#CLIENTSIDE
    function onCreated() {
      
    temp.img findimg(1); // The image ID is 1
      
    temp.img.image "block.png";
      
    temp.img.30;
      
    temp.img.40;
      
    temp.img.alpha 1// no transparency

    And if you add this to yourself, you can see a block image appearing on the client!

    The index of the image can be changed from 1 to any number. But if the number was between 1-200, the image will appear to everybody. So other players can see that block you've put.

    If the image is from 200-AnyNumber it will only be shown to you, the other players cannot see the block.

    As you've seen we used a temp.variable to store findimg(), but it's easier if we use with() which is changing the this. variable to the findimg() variable...
    Last edited by John; 03-06-2014 at 06:58 PM.
    -Johnaudi

  2. #2
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    Here's the same script with the with() statement:

    PHP Code:
    //#CLIENTSIDE
    function onCreated() {
      
    with(findimg(1)) {
        
    image "block.png";
        
    30;
        
    40;
        
    alpha 1;
      }

    A lot prettier, right?

    These are the core basic concepts of the class TShowImg, you can draw polygons too, will teach that later on.

    delegate variables:

    A variable can be a string ("hello"), an integer (123), a double/float (1.3542), an object (player), a burger (not really), an array ({1, 4, 5, "pie"})... But did you know if can be a function too?

    Here's how the variable will look like, it can be anything, but I'm going to call it temp.somefunction:

    PHP Code:
    function onCreated() {
      
    temp.somefunction = function() { echo("Hello"); }; // <- notice, there is a ';' after a delegate function, because it's similar to an arrays' syntax

    Guess what will happen! Nothing! Why? Because I haven't triggered that function yet, so let's go ahead and trigger it:

    PHP Code:
    function onCreated() {
      
    temp.somefunction = function() { echo("Hello"); };
      
    temp.somefunction(); // This is how we trigger it

    And it will echo 'Hello'.

    But what if you want to, for example, add two numbers together, you're going to need to add parameters, this is how parameters works:

    PHP Code:
    function onCreated() {
      
    temp.addnumbs = function(num1num2) { echo(num1 num2); };
      
    temp.addnumbs(105); // Will echo 15.

    string functions:

    This is very important to learn in Gs2, since this is an Object Oriented language, we're going to use string to pack/depack our data as much as we want.

    In this current section, I'm going to teach you the two functions, tokenize(), substring(), upper(), lower(), charat() and pos().

    string.tokenize(char);

    Let's say string is equals to "More PIE. MORE FOOD. MORE !!!!", and our dear manager hates strings! What he wants is us to divide this string into an array... So what I'm going to do is use string.tokenize(".") to create an array of seperation of the '.' character...

    For example, a string like
    str = "I like noobs. Yes I do. I like noobs that sleep with you"

    str.tokenize(".") becomes {"I like noobs", " Yes I do", " I like noobs that sleep with you"}...

    and if you do str[0] you'll get "I like noobs" and str[1] you'll get "Yes I do"... So it technically returns an array of the split string within the given char.

    You can use it for spaces aswell by having .tokenize() or .tokenize(" ")
    This is mostly used to cut chat commands, for example "/ban John", you can do player.chat.tokenize()[1] will return "John".
    PHP Code:
    function onCreated() {
      
    temp.john "J#OH#N";
      echo(
    temp.john.tokenize("#")[1]); // temp.john.tokenize("#") will be {"J", "OH", "N"} and [1] will get the second element which is "OH"
      // This will echo OH

    PHP Code:
    //#CLIENTSIDE
    function onPlayerChats() {
      if (
    player.chat.tokenize()[0] == "/ban") {
        
    //Ban here
        
    banThisPlayer(player.chat.tokenize()[1]); // Made up function, but you get the concept.
      
    }

    string.substring(minindex);
    or
    string.substring(minindex, length);

    This function is like scissors. It cuts your string in anything you index it.

    For example, str = "Hello";
    echo(str.substring(2)); will echo "llo", it has removed the first 2 characters.
    echo(str.substring(4)); will echo "o", it has removed the first 4 characters.
    echo(str.substring(2, 1)); will echo "l", it has removed the first 2 characters, and returned the first character of the string.
    echo(str.substring(1, 3)); will echo "ell", it has removed the first character, and returned the first 3 characters of the string.
    echo(str.substring(0, 4)); will echo "Hell", it has removed no characters, and returned the first 4 characters.

    Simple as that.

    string.upper();
    string.lower();


    They're uppercases and lowercases! "heLlo" with string.lower() becomes "hello" and with upper() becomes "HELLO".

    string.charat(integer);

    You can get the character available in the index given, in other programming languages it can be done by using string[index], but in graal, you use string.charat(index);

    str = "It's a beautiful hell";
    echo(str.charat(2)); // will echo '
    echo(str.charat(6)); // will echo a
    echo(str.charat(0)); // will echo I

    string.pos(string);

    This returns the integer of the index of the string's char/string position.
    In english, it means it returns the position of where the character or the string is.

    for example:
    str = "Yo whattup!";

    echo(str.pos("o")); // will return 1 because it's the 2nd element of the string.
    echo(str.pos("Y")); // will return 0 because it's the 1st element of the string.
    echo(str.pos("up")); // will return 8 because it's the 8th element of the string.
    echo(str.pos("z")); // will return -1 because it's not present in the string.

    That's pretty much it, if you've fully understood this tutorial, and I mean FULLY, make sure you go check the next one!

    These are essentials in Gs2 (except the binary nums), if you've understood them, you've just stepped one step closer into having a lot of knowledge in this language.
    Last edited by John; 04-05-2014 at 08:42 AM.
    -Johnaudi

  3. #3
    aaaaaaaaaaaaaaaaaaaaaaaaa Grief Hero's Avatar
    Join Date
    Jun 2013
    Location
    n/a
    Posts
    1,327
    WOO! Finally, I'll be able to continue learning gs2


    I saw. I conquered. I came.


  4. #4
    Street Boss Ricky's Avatar
    Join Date
    Jun 2013
    Location
    Miami, FL
    Posts
    592

    Advanced Gs2 for the Noobs #1

    Must finish reading previous tutorials! Ahhh

    Thanks to iBliss for the Awesome Signature!

  5. #5
    Zephlyn
    Join Date
    Aug 2013
    Location
    Chile
    Posts
    3,295
    if you don't know nothing of Gs2 and your reading this will blow your mind

  6. #6
    Banned
    Join Date
    Dec 2013
    Location
    Pandcakes
    Posts
    193
    John was a experiment that gone wrong. He now codes uncontrably, the cure is in research... #JohnaudiGetXbox

  7. #7
    Veteran Wolverine8888's Avatar
    Join Date
    Jun 2013
    Location
    Philippines
    Posts
    315
    Well basic gs2 guides is fine to me not this...I hate about math. Not recommending for starter scripters.
    Need Help? or Questions?
    Just Ask me and I will give the answer right away
    Want to skype with me? wolverine_graal
    ~Gani Artist

  8. #8
    It's all double dutch to me.

  9. #9
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825
    If you guys are having trouble with the bits and bytes, you can skip it.
    -Johnaudi

  10. #10
    Veteran Wolverine8888's Avatar
    Join Date
    Jun 2013
    Location
    Philippines
    Posts
    315
    John, know anything we could practice this in a server?
    Need Help? or Questions?
    Just Ask me and I will give the answer right away
    Want to skype with me? wolverine_graal
    ~Gani Artist

Posting Permissions

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