Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Basic GSC Coding: While, Break and Wait
#1
Hello

This is my first tutorial about basic gsc coding, Ill explain at first the "while" command, this command what it does is to repeat something continuously (with a wait beetween repetitions). Its structure is the following:

Code:
while( something )
{
WHAT YOU WANT TO REPEAT
wait ( THE FRECUENCY YOU WANT TO REPEAT );
}

In the while is necessary to add a wait, if not you will make a BIG failure. The wait works like this and it is measured in seconds:

Code:
wait ( time you want to wait );

You can also simplify it like this (I put a few examples):

Code:
wait 3;
wait 2.5;
wait 0.9;
wait 0.05;

Or if the time is smaller than 1:

Code:
wait .05;

Lets continue with the while, the while will repeat when is something happening, if you dont care of what happens and you want to keep it running without end, you put in "something": true or 1 (which are both same).

Code:
while( 1 )
while( true )

You can also put in the something a condition, for example, if you have a variable called variable1 and its value is 6 and its changing, you can do like this to make it only repeat when the value is 6

Code:
while( variable1 == 6 )

You can also add a ingame function, for example one that checks your player stance (stand, prone or crouch, in the example I am checking if the stance is crouch):

Code:
while( self getStance() == "crouch" )

Lets try to do something that says some words to all players every 10 seconds:

Code:
while( 1 )
{
    self sayall( "While Tutorial" );
    wait ( 10 );
}

The sayall is a command that says something in chat to all players in the lobby. The problem of the "while"s is that if you dont end them, they will keep running and sometimes if you reach a certain ammount of them running you will suffer problems such as laggs or game crashes, so its very important to destroy them. One way to do this is using another gsc command called "break", it will destroy the "while" in which is contained:

Code:
while( 1 )
{
    self iPrintLnBold( "Break Function" );
    break;
    wait ( 3 );
}

The previous code is quite stupid, because the "while" will never repeat, it will just play once because you destroy it in the first repetition, so the best thing on this is to check a variable and then "break" it:

Code:
while( 1 )
{
    if( self getStance() == "stand" )
        break;
    wait ( 0.05 );
}

In the previous case the "while" will be destroyed when the player stance is "stand". Another way to destroy "while"s which is more used is to add a "endon". The endon waits for some game notification and when it happens it destroys the "while"s contained in the function, the most used ones are these 2:

Code:
self endon( "death" );
self endon( "disconnect" );

The first one will destroy the "while" when you die and the second one when you disconnect. Lets combine a few of the previous examples all in 1 and inside a function, functions have this structure if you dont know:

Code:
Function( )
{
CODES HERE
}

Lets see the example (It will say to all players: While Tutorial every 10 seconds and the "while" be destroyed if you die or disconnect):

Code:
Tutorial( )
{
    self endon( "death" );
    self endon( "disconnect" );
    while( 1 )
    {
        self sayall( "While Tutorial" );
        wait ( 10 );
    }
}

On next tutorials I will explain some other things relationed to this and about other basic gsc coding commands.

Thanks for reading, @Yamato
Reply

#2
+rep
Reply

#3
..................
crAyon makes no warranty with respect to documents or other information available from this place, assumes no legal liability or responsibility whatsoever for the accuracy, completeness, or usefulness of any such information, and does not represent that its use would not infringe privately owned rights. crAyon disclaims all warranties, express and implied, including the warranties of merchantability and fitness for a particular purpose.
Reply

#4
Thankz i learn a lot stay with this tutorials Smile
Reply

#5
thanks a lot yamato ure awesome Smile
Reply

#6
Nice to see tutorials which don't start with: "get a _rank.gsc". Wink

Just some things you might want to add to the while and for -loop tutorials:

If you mention the break; command you might want to talk about continue; too. Smile
And maybe add a sentence thats makes clear that it can be clever at times to use
a self/player/level etc. waittill instead of a wait(time here);.

"In the while is necessary to add a wait or a waittill, if not you will make a BIG failure."

I am sure @Yamato knows what i am talking about, so just for the guys who dont: Big Grin

Code:
    for(;;)
    {
    self waittill("drop_bomb");
        
        if( !self.isBombCarrier )
            continue; //the loop instantly "jumps" to its top again (waits for "drop_bomb" again, without executing the two lines below before)
            
        self.carryObject thread maps\mp\gametypes\_gameobjects::setDropped();
        self.isBombCarrier = false;
    }

Just my two cents...

[Image: lxkf2.jpg]
Reply

#7
(03-06-2012, 00:59)banz Wrote: Nice to see tutorials which don't start with: "get a _rank.gsc". Wink

Just some things you might want to add to the while and for -loop tutorials:

If you mention the break; command you might want to talk about continue; too. Smile
And maybe add a sentence thats makes clear that it can be clever at times to use
a self/player/level etc. waittill instead of a wait(time here);.

"In the while is necessary to add a wait or a waittill, if not you will make a BIG failure."

I am sure @Yamato knows what i am talking about, so just for the guys who dont: Big Grin

Code:
    for(;;)
    {
    self waittill("drop_bomb");
        
        if( !self.isBombCarrier )
            continue; //the loop instantly "jumps" to its top again (waits for "drop_bomb" again, without executing the two lines below before)
            
        self.carryObject thread maps\mp\gametypes\_gameobjects::setDropped();
        self.isBombCarrier = false;
    }

Just my two cents...

Yes, that is something of what Ill talk in future basic tutorials.
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tutorial] Basic GSC Coding: Building your first mod and your first code Yamato 2 10,409 06-07-2016, 21:49
Last Post: FrostbytePG
  [Tutorial] Basic GSC Coding: Functions Yamato 15 11,433 08-17-2013, 10:17
Last Post: hamza-q99
  [Tutorial] Basic GSC Coding: Making your second code Yamato 9 7,922 12-28-2012, 13:30
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Operations Yamato 0 7,573 04-04-2012, 13:07
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Foreach, Continue and Return Yamato 6 7,078 04-04-2012, 08:33
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Switch Yamato 6 5,612 04-03-2012, 18:32
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: If, Else and Else If Yamato 2 5,376 03-30-2012, 10:47
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: Downloading and Installing a mod Yamato 1 4,973 03-30-2012, 10:42
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: For Yamato 4 5,586 03-26-2012, 18:39
Last Post: tsoPANos

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.