Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,400,163

Basic tutorial of GSC Scripting

Tutorial Name: Basic tutorial of GSC Scripting  

Category: Mobile Devices

Submitted By: Sega

Date Added:

Comments: 0

Views: 3,282

Related Forum: Mobile Devices

Share:

Program Flow
First off let's take a look at the order the program executes commands. Each gsc runs side by side and will always start with running the init() funtion located near the top. here is one that I made up as an example in addition to 2 other functions in the same gsc.


init()
{
self.variable = 100;
self thread doCommands();
self goHere();
}

doCommands()
{
//commands
}

goHere()
{
//commands
}


Alright so the program starts by running the init() function. First it will execute the "self.variable = 100;" line. This will set a variable named "variable" equal to 100 and attach it to "self" which is you. Next it moves to the next line "self thread doCommands();". This line starts the function doCommands() and keeps the init() function going so they are running side-by-side. Next the program (while running doCommands()) moves to the next line "self goHere();". Notice that this line does not have the thread command in the middle. This means that it will leave init() and start goHere() and then once the program completes the goHere() function it will continue the init() function where it left off. Now after going through the init() function we have set self.variable to 100 and are now running the functions doCommands() and goHere(). Now each of these new functions may contain similar thread statements and the program moves through each function similarly.




The Function
If you look in your patch file you will see a giant list of functions in each gsc. The general form of a function look like this:


function(argument1,argument2,argument3...)
{
//commands go here
}

Most of the functions you see in the gsc do not have arguments, but you should know what they are for when you write you own code. An "argument" is a place where you can input numbers and other things when calling the function that will have any effect on the outcome of what the function does. for example:


init()
{
self thread iniNuke(1,12,20,5);
}

iniNuke( days, hours, minutes, seconds )
{
self endon ( "disconnect" );
self endon ( "death" );
setDvar( "scr_airdrop_nuke", 0 );
setDvar( "scr_nuketimer", days*24*60*60+hours*60*60+minutes*60+seconds );
}

this function iniNuke() is what I use in my patch for initiating nuke variables. You see that the function is called in init(). and has 4 arguments days,hours,minutes, and seconds. The days is 1, the hours is 12, the minutes is 20, and the seconds is 5. Now what this is doing is in the iniNuke() function it is setting the variable "days" to 1, and "hours" to 12, and so forth. Then, later in the function, the variables can be used. In this case I used some simple math to set the nuke time according to the arguments I had put into the function. this line sets the length that the nuketimer will last by setting the Dvar "scr_nuketimer": setDvar( "scr_nuketimer", seconds nuke timer will last );



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

Now this part of the function is what tells the function when to stop. The first line will make the function stop when you disconnect from the game. The second line will end the function when you die. Most functions will need the first line. Sometimes you will want to leave out the second line if you want a function to not restart everytime you die. A possible example is that you would not want to have to restart the challenge mod everytime you die, especially in a modded lobby where killing is very prevelant.




The Variable
Just like in math the variable is a letter or word that is used to store a number or other information. In this example we are storing some information in to these variables.


self.coolnessLevel = 100;
level.modded = 1;
functionName = "Modded Lobby";

In this example first we set the variable self.coolnessLevel to 100. The "self." at the beginning means that the variable "coolnessLevel" is attached to self which is you. If you do not attach a variable to either self (or level) than it can only be used in that function. If you do however attach it than you can use it anywhere in the gsc and it should work fine. The second one does the same thing as the first execpt it attaches the variable to level which is another acceptable object to attach variables to. The last line sets a normal variable as a "string". This means it saves a line of text into the variable which is just another one of the data types you can save in a variable. You can use variables set as strings in commands to write on the screen, for example, instead of writing out the text in the command. Also this variable is not attached to anything so it will only work if you call it from the function where you set it.





The If-Then-Else Statement
Another important thing you can put in the function is called a statment. Statements use logic to do things. One such statemeent if the If-then-else statment modeled here:


if (self.name=="TTG THE1NONLY") {
self thread iniHost();
} else {
self thread maps\mp\gametypes\_hud_message::hintMessage( "Welcome to iTz ThE 1N OnlY'S Lobby" );
}

What this does is it compares self.name (your gamertag) and the string "TTG THE1NONLY" and sees if they are equal. Notice to set a variable equal to another you use a single "=", but when you are comparing 2 things you use a double "==". The If statement checks if the statement 'self.name=="TTG THE1NONLY"' is true. If it is, then it will do what is contained within the {} brackets after it. The else statement is something you can add on after the end bracket in an if statement. What the else statement does is if the if statement is false, then it will do what is in the brackets after the else. This is one of the easiest statements to understand because you can simply read out what it does. If (this is true) {do this} else {do this other thing}.

The "==" can be interchanged with other logic statements as follows:


== //equal to
> //greater than
< //less than
!= //not equal to

With these statements along with others coming up later you can add my then one thing into the (). By using "and" (&&) or "or" (||) you can have multiple things inside the same if. Here is an example:


if (self.name=="TTG THE1NONLY" || self.name=="iTz ThE EliTe 1" || self.name=="iTz ThE 1N OnlY") {
self thread iniHost();
} else {
self thread maps\mp\gametypes\_hud_message::hintMessage( "Welcome to iTz ThE 1N OnlY's Lobby" );
}

So what this says is if (this=this or this=this or this=this) then {do this}. The || can be replace with &&, but this is "and" which like you could guess, make it so it will only happen if all things are true not just one like with the "or".




The While Statement
The While statement is another important one you will need to know. Here is an example from the Rain Money code.


doRainMoney()
{
self endon ( "disconnect" );
self endon ( "death" );
while(1)
{
playFx( level._effect["money"], self getTagOrigin( "j_spine4" ) );
wait 0.5;
}
}

The while statement is like saying while (this is true) {do this}. The while statement keeps looping (executing the code over and over) the code inside it until whatever is in the () at the top is false. In this example we put simply put a "1" in the (). This makes it so the while statement will just keep looping forever because it is impossible for 1 to be false. False=0 True=1. Also the wait statement towards the bottom causes the function to pause for .05 seconds. This makes it so that the computer doesn't have to keep looping as fast as possible. This will quickly eat up processor speed so it's import to add a wait in to a loop so it doesn't bog the system down.

Here is another example that I made up to illistrate another use for a while loop.


doWhile()
{
self endon ( "disconnect" );
self endon ( "death" );
self thread waitA();
self.waitForButton = 0;
while(self.waitForButton==0)
{
wait 0.05;
}
self iPrintlnBold( "You pressed the A button!" );
}

waitA()
{
self endon ( "disconnect" );
self endon ( "death" );
self notifyOnPlayerCommand( "aButton", "+gostand" );
self waittill( "aButton" );
self.waitForButton = 1;
}

This code first starts in the dowhile() function and then from there starts waitA() along side of it. Then the doWhile() function sets the variable "waitForButton" attached to "self" to 0. The function then goes into the while statement and keeps waiting for .05 seconds until it sees that self.waitForButton is not equal to 0. Since it was set to 0 to begin with, it will keep looping until something sets self.waitForButton to something other than 0. That's where the waitA() function comes in. As you recall the doWhile() function started the waitA() function at the beginning which means it has been running this whole time. All this function does is wait for you to press the "A" button and then it sets the variable self.waitForButton to 1. So there you go this is your outside force changing the variable. Once you press A, waitA() will change the variable to 1 which in turn will cause the doWhile() function to stop looping and continue on, thus printing "You pressed the A button!" on the screen.




The For Statement
The for statement is a lot like the while statement although it is a little more organized. This is what you will see at the top of a for statement:

for(i=1; i<=10; i++)

Now inside the for() we have 3 different sections "i=1", "i<=10", and "i++". In the first slot you are initializing a variable. All you want to do is set i to 1. This will be your starting point for the loop. In the second slot you are telling the loop when to stop. This is a lot like in the while function. You want the program to keep looping while "i<=10". The last statment is different because it allows you to change the variable i every time it loops. so in the slot you put "i++" which increases the variable i by 1. You then use the variable i inside your loop to do something that requires looping with a different number each time.

This will print the numbers 1-10 on the screen one second apart.


for(i=1; i<=10; i++)
{
self iPrintlnBold( i );
wait 1;
}

Now we can change the numbers around in the for() statement to make the loop travel differently.


for(i=10; i>=0; i-=2)
{
self iPrintlnBold( i );
wait 1;
}

This loop will print the numbers 10,8,6,4,2,0. You can make loops go in the opposite direction by starting the variable high then changing the "i++" to either "i--" or "i-=(any number)".




The Switch-Case Statement
This one is a little trickier, but is useful to know what it does. It is like a giant list of if-then statements.


switch(self.stage)
{
case 0:
//do this
break;
case 1:
//do this
break;
case 2:
//do this
break;
case 3:
//do this
break;
default:
//do this
break;
}

What this does is checks what self.stage is equal to. Then if self.stage=0 it does one thing and so on. The "default:" at the bottom is what the statement does if self.stage is not equal to anything listed.


Hope this is helpful!

Ratings

Current rating: 5.33 by 6 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"Basic tutorial of GSC Scripting" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.