Results 1 to 3 of 3

Thread: Advanced Gs2 for the Noobs #2

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

    Advanced Gs2 for the Noobs #2

    Hey guys,

    This is yet another tutorial for Gs2, but this time make sure you have fully understand all the couple tutorials I've posted before this, since stuff is getting harder and harder every-time, and you'll not be able to keep up with everything if so.

    This tutorial will help you maintain your script as stable, editable, open for teamwork and easier to use and read.

    hardcoding:

    What is hardcoding? In short and easier terms, it's when you input all the data that's going to be used in the script itself. In other words, when your strings are attached to the script itself (not retrieving it from a TXT file or so), what also can be hardcoded is a some GUI, or making something only usable in a specific area of its script itself.

    Enough chit-chats, let's start learning.

    If you're into updating your script after time, or insert more options into it, hardcoding is not a good option, but if your code is run and use with no expansions, it would be quicker to hard code it.

    Here's a small example of a local hardcoded script:
    PHP Code:
    function onCreated() {
      echo(
    "Your list of items is: Bow, Burgers, T-Shirt, Weights, Batman, Burger Batman with weights without a T-Shirt");

    And now here's when it makes it less hard-coded (even though, it can become not hard-coded at all by getting it via a TXT file/other variable, making it more accessible)
    PHP Code:
    function onCreated() {
      
    temp.list = {
      
    "Bow",
      
    "Burgers",
      
    "T-Shirt",
      
    "Weights",
      
    "Batman",
      
    "Burger Batman with weights without a T-Shirt"
      
    }; // Edit this if you want to change something in it
      
      
    echo("Your list of items is:");
      for (
    temp.itm temp.list) {
        echo(
    temp.itm);
      }

    It makes it easier to edit, yet it's still considered hard-coded since it's not editable from an outside source, aka harder to access and everything is built-in.

    script skeleton:
    Before starting to code, have a type of concept before doing so.

    Let's say our dear and lovely snk asks us to make a small application that will inverse a string (example: "hello" should become "olleh")... He wants it as a function, and wants me to call the it inverseThatStringBaby(stringhere) and it will return the inverted string.

    We need to plan ahead, here's how you should be able to think:
    Hello?
    Anyone here...?
    Oh wait, yes, right, hello, this is John's Mind, what do you want again? A function that inverses a string?

    Hmm, let's see, the things that are being changed in that script are the characters' position.
    So what I have to do, is change the position of the of the characters and make them upside down.

    There are lots of ways! Let me pick the easiest because I'm lazy.
    First I have to turn the string into a character array, to be able to check each character by himself, right?

    Now what I have to do is try to make a script which will make a new array, but with the elements inverted.

    All I have to do then is turn the array into a string!

    Seems easy, isn't it?

    Let's get started!

    PHP Code:
    function inverseThatStringBaby(str) {
      
    temp.arr = {}; 
      for (
    temp.0temp.str.length(); temp.i++) {
        
    temp.arr.add(str.charat(temp.i)); // this will add each character of the string by itself to temp.arr
      
    }

    Alright, so temp.arr is the array of characters, what was I gonna do with it again? Oh right, invert the positions of the characters.

    PHP Code:
    function inverseThatStringBaby(str) {
      
    temp.arr = {}; 
      for (
    temp.0temp.str.length(); temp.i++) {
        
    temp.arr.add(str.charat(temp.i));
      }
      
      
    temp.iarr = {};
      
      for (
    temp.temp.arr.size() - 1temp.>= 0temp.i--) {
        
    temp.iarr.add(temp.arr[temp.i]);
      }


    There we go, temp.iarr is now the inverted form of temp.arr... But wait, why store it in an array if I can store it in the string directly? Let's do that instead!

    PHP Code:
    function inverseThatStringBaby(str) {
      
    temp.arr = {}; 
      for (
    temp.0temp.str.length(); temp.i++) {
        
    temp.arr.add(str.charat(temp.i));
      }
      
      
    temp.iarr "";
      
      for (
    temp.temp.arr.size() - 1temp.>= 0temp.i--) {
        
    temp.iarr @= temp.arr[temp.i];
      }


    There we go! temp.iarr is now the inverted string of str!
    Hmm... I'm forgetting something, right! I should 'return' the inverted string!

    PHP Code:
    function inverseThatStringBaby(str) {
      
    temp.arr = {}; 
      for (
    temp.0temp.str.length(); temp.i++) {
        
    temp.arr.add(str.charat(temp.i));
      }
      
      
    temp.iarr "";
      
      for (
    temp.temp.arr.size() - 1temp.>= 0temp.i--) {
        
    temp.iarr @= temp.arr[temp.i];
      }
      
      return 
    temp.iarr;

    There you go snk! Can I get a day off?
    The skeleton is the way you are implementing your code, it's the way you're placing the variables.

    In the last example I give, I can recall the skeleton as the following:
    main string -> main string array -> inverted the main string array -> made an array of the inverted main strings' array (but then removed it) -> returned inverted main string


    debug code:

    It is really helpful to have some troubleshooting code in case you're having issues with a certain thing.

    You can always make a small variable asking if you want to run the script in debug mode, and trying to see where the issue is.
    And here's a small example of how it can help you...

    PHP Code:
    const DEBUG false// You should usually make that variable a constant, call it whatever you want, but make it a constant because you'll be switching from debug to normal and vice versa once you'll be updating the weapon... Unless you're trying to see some live statistics then you can make the variable dynamic.

    function onPlayerChats() {
      if (
    player.chat == "Test") {
        
    // Do something...
        
    if (DEBUG) { 
          
    // Check players' stats etc...
        

      }

    You can also make use of it for outputting data.
    PHP Code:
    const DEBUG_MODE true;

    function 
    onCreated() {
      
    // stuff or calculations here...
      
    print(some_calculation_variable_you_want_to_see);
      
    // more stuff...
    }

    function print(
    str) {
      if (
    DEBUG_MODE) {
        echo(
    "DEBUG: " str);
      }

    And when you see the variable you need, you can finish by making DEBUG_MODE to false, which will no longer make it echo anymore without having to edit the whole script.

    (writing...)
    Last edited by John; 08-17-2014 at 12:15 PM.
    -Johnaudi

  2. #2
    I never did understand your guides

  3. #3
    Banned
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,237
    I wish I could script; but I don't have the time to learn GS2

Posting Permissions

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