1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Tutorial Basic GSC Coding: For

Discussion in 'Tutorial Section' started by CabCon, Aug 1, 2016.

  1. CabCon

    CabCon Head Administrator Staff Member

    2,426
    1,368
    113
    Credits:
    17,446
    This is the next tutorial about basic GSC coding, this time is about the "for" command. Its use is nearly the same as the "while", it repeats something until you stop it and with a frequency you add, its "basic" structure is the following:
    Code:
    for( ; ; )
    {
        CODES
        wait ( TIME );
    }
    You can add "break"s or "endon"s like in "while" command to destroy the "for"s. The advantage of the "for" in relation to the "while" is that you can make it repeat a certain ammount of times in less space than a "while":
    Code:
    for( index = 0; index <= 10; index ++ )
    {
        CODES
        wait ( TIME );
    }
    I called a variable "index", you can call it however you want, examples:
    Code:
    for( i = 0; i <= 10; i ++ )
    for( fortutorial = 0; fortutorial <= 20; fortutorial ++ )
    for( j = 0; j <= 20; j ++ )
    for( dollar = 0; dollar <= 15; dollar ++ )
    The first place inside the ( ) is the number with the one the variable starts, the second place is the repetition when the "for" ends ( in my example it will end at 10 repetitions) and the third place is how the variable value changes after each repetition. What I have put: "index ++", does that the value of the index increases in 1 in each repetition. To change numbers, the most common operators are the following ones (there are more, but they arent that basic):
    Code:
    index += NUMBER (it will add the number you put there)
    index ++ (it is the same as doing index += 1, its shorter)
    index -= NUMBER (it will decrease the variable number in the ammount of units you put on "NUMBER")
    index -- (it is the same as doing index -= 1, its shorter)
    index *= NUMBER (multiplication of the value * the number)
    index /= NUMBER (division of the value / the number)
    
    When inside a "for" there is just 1 line of code and the wait is not needed, you can remove the "{" and the "}". Example:
    Code:
    for( g = 0; g <= 4; g ++ )
    {
        self waittill( "killed_player" );
    }
    You can reduce it to (this example does that it repeats after each time you killed an enemy):
    Code:
    for( g = 0; g <= 4; g ++ )
        self waittill( "killed_player" );
    Question?
    Please login or register to view links or downloads!.

    Credits to Yamato!
     
    BaDSooD likes this.
  2. BaDSooD

    BaDSooD Old CCM Member

    616
    357
    63
    Credits:
    5
    +1

    Cool to see tutorials again :grinning:
    (yeah this message is the message I have posted on the other thread xDD)
     
    CabCon likes this.
  3. CabCon

    CabCon Head Administrator Staff Member

    2,426
    1,368
    113
    Credits:
    17,446
    Yeah, when people are asking how to make a mod menu I send everytime this tutorial section. Here are not much threads so I try to make more to explain everything!
     

Share This Page