Tutorial Basic GSC Coding: Functions

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,998
Reaction score
2,918
Points
1,053
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?
Ask them here.

Thanks for reading, @CabCon.
 

God

Skiddy
Messages
337
Reaction score
240
Points
818
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
 

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,998
Reaction score
2,918
Points
1,053
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
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 :grinning:
 

God

Skiddy
Messages
337
Reaction score
240
Points
818
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 :grinning:

You can't do it with functions, give a syntax error in GSC Studio
 

The Dark Side

Former Staff Member
Messages
1,007
Reaction score
784
Points
993
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. :smile:
 

Inactive Account

Old CCM Member
Premium Member
Messages
480
Reaction score
240
Points
913
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. :smile:
Compact functions are better :smile:
Speack about this is "important" to make clean menu :tongueclosed:
 

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,998
Reaction score
2,918
Points
1,053
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. :smile:
of course you can do it like this. :grinning:
 
Top