Tutorial Basic GSC Coding: While, Break and Wait

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,998
Reaction score
2,918
Points
1,053
This is the next tutorial about basic gsc coding, I´ll 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 );
    }
}

Question?
Ask them here.

Credits are going to Yamato.
 
Top