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: Functions

Discussion in 'Tutorial Section' started by CabCon, Jan 25, 2016.

  1. CabCon

    CabCon Head Administrator Staff Member

    2,400
    1,355
    113
    Credits:
    16,792
    Hello

    This is the first basic tutorial about GSC, first time about Functions, I will explain how are they and how to call them, gsc codes are all inside functions, by calling new functions you do different things in game.

    The default function structure:

    Code:
    Function1( ) //Name of the Function Name
    {
    CODES HERE
    }
    Example of the default function structure
    The thing I called "Function1" is the name I put to the function, the "( )" is added because without it, is not a function (it is needed). Then there is a opening bracket ( { ), after it is where you are going to place your code and when you have ended writing it, you must close the function with a closing bracket ( } ). An example of function:
    Code:
    SayTheWords()
    {
        self iPrintLnBold( "You are reading" );
        wait ( 1 );
        self iPrintLnBold( "Functions tutorial" );
    }
    Something you cant make on a function is to put 2 functions in 1, example:
    Code:
    FunctionA()
    {
        FunctionB()
        {
            SOMETHING
        }
    }
    Call Functions
    By adding a code to a file, it wont be enough to make it work, so, you´ll have to "thread"/call it. There are many ways, I´ll say a few of them, the most used one is this one:

    This is the example Function which we will call in the following examples:
    Code:
    SayTheWords()
    {
        self iPrintLnBold( "You are reading" );
        wait ( 1 );
        self iPrintLnBold( "Functions tutorial" );
    }
    1. the normal calling looks like this:
    Code:
    self thread YOUR FUNCTION NAME;
    Example: 
    self thread SayTheWords();
    2. Call a Function as a Input of a other Function.
    Code:
    myTestFunction(function)
    {
    self thread [[function]]();
    }
    
    Example: 
    self thread myTestFunction(::SayTheWords); // :: will say that the variable "SayTheWords" is a function.
    
    Inputs of a Function
    A function name can also have inside the "( )" variables which have some use inside the function, for example:
    Code:
    SayTheWords2( thing1, tutorial )
    {
        self iPrintLnBold( thing1 );
        wait ( 1 );
        self iPrintLnBold( tutorial );
    }
    It will say first what I put in "thing1" and then what I put in "tutorial", for example, I could call it like this:
    Code:
    self thread SayTheWords2( "Hello CabConModding User", "You are reading Functions Tutorial" );
    Question?
    Please login or register to view links or downloads!.

    Thanks for reading, @CabCon.
     
    iRnZ, GreenPeace and God like this.
  2. God

    God Skiddy Premium Member

    336
    211
    43
    Credits:
    179
    Great thread for the beginners, and I want to add (if it's correct) you don't need the "{" brackets if there's only one line of code, I know it works for "if" statements
     
  3. CabCon

    CabCon Head Administrator Staff Member

    2,400
    1,355
    113
    Credits:
    16,792
    I know that you can use it for if like:
    Code:
    if(something==true)
    self iprintln("TRUE");
    
    Also I was thinking you can use it seperated by a coma like:
    Code:
    if(something==true)
    self iprintln("TRUE"), self iprintln("Still true!");
    

    But I think you can't do it with functions. Feel free to test it and message me if it is possible :smile:
     
    dorathekiller97 likes this.
  4. God

    God Skiddy Premium Member

    336
    211
    43
    Credits:
    179
    You can't do it with functions, give a syntax error in GSC Studio
     
    CabCon likes this.
  5. BaDSooD

    BaDSooD Old CCM Member

    616
    357
    63
    Credits:
    5
    Thank's, helpful for beginners :tongueclosed:
     
    CabCon likes this.
  6. dorathekiller97

    dorathekiller97 Modder

    594
    322
    63
    Credits:
    495
    Just a minor thing, im not sure if this is what god was talking about but

    Code:
    myTestFunction(function)
    {
    self thread [[function]]();
    }
    Could look like

    Code:
    myTestFunction(function)
    { self thread [[function]](); }
    Instead. Just looks cleaner. :grinning:
     
    BaDSooD likes this.
  7. BaDSooD

    BaDSooD Old CCM Member

    616
    357
    63
    Credits:
    5
    Compact functions are better :grinning:
    Speack about this is "important" to make clean menu :tongueclosed:
     
  8. CabCon

    CabCon Head Administrator Staff Member

    2,400
    1,355
    113
    Credits:
    16,792
    of course you can do it like this. :smile:
     

Share This Page