ItsMods

Full Version: Basic GSC Coding: While, Break and Wait
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
+rep
..................
Thankz i learn a lot stay with this tutorials Smile
thanks a lot yamato ure awesome Smile
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...

(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.