Results 1 to 10 of 19

Thread: Advanced Gs2 for the Noobs #1

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

    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

Posting Permissions

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