So since there are a lot of new people comming to the modding community, I decided to make this small post to help others understand GSC better, anyway lets begin.
Contents:
Setting Up
Understanding The Syntax
Understanding Variables
Using 'using' and 'insert'
Basic Scripting
Threading
GSC Functions
Cleaning up your script
FAQ
Setting Up
Spoiler: click to open...
In order to code GSC you do not need any fancy or pricy programs to do so, all you need is a simple text editor. If you want you could use the default notepad application that comes with windows but if you prefer something a bit more advanced that has syntax highlighting and some other groovy features then I suggest using some of the programs I have listed below.
Notepad++ Sublime Text UltraEdit GSC Studio By iMCSx
I'm sure if you were to do a quick google search you could find more but these are probably the best ones!
Understanding The Syntax
Spoiler: click to open...
If you are new to coding getting your head around the syntax can take quiet a bit of time to get the hang off. If you are unsure what I mean by "The Syntax" then feel free to google it and come back when you are ready The Syntax in GSC is similar to most general programming languages such as C++, C#, Ruby and many more.
The 2 Curly Brackets Are Used To Define where the start and end of your functions/statements are. The Semi-Colon is used to define where a line or statement will end it is also used as a separator in some cases. The Brackets are mainly used to indicate an argument, to tell the compiler what data type the function needs to look for first in order to start.
The Square Brackets Are Really Only Used To Declare & Access Arrays
Understanding Variables
Spoiler: click to open...
A Variable is used as a storage point, they are used within almost every programming language and are extremely useful for storing any type of data. Before declaring a variable you should first know about all the different data types, ill list them below.
There is many more but this is all you will need to know for GSC
Now To declare a variable its actually quiet simple, you don't need to say what type of variable it is you are declaring you just need to put the data in, I'll put some examples below. The names of your variables can be anything you desire, by doing self.variableName I am defining a variable for my own player in GSC, I will talk about this later in Basic Scripting.
Using 'using' and 'insert'
Spoiler: click to open...
I'm sure everyone has seen it when at the top of a GSC code you see something that looks like this Think of using as a way of copying and pasting, what the above code would be doing is copying and pasting all the contents from another GSC file into our GSC file. But why would we want to do this? Well the answer to that is quiet simple, so we can use the functions that are in that GSC file in our own GSC file. Insert is basically the same just with variables.
Basic Scripting
Spoiler: click to open...
Now I Will Talk About The Basics of GSC Scripting!
If you have ever seen GSC Coding before I'm sure you are wondering what it means by self and player and all that other stuff that you see before functions. Well to start off self means the entity/player that is calling the current script, example lets say my player just spawned and I run the code below. Now from what I have told you we know that self is my player because my player is calling this code, so therefore my players controls will be frozen.
Now for a more complex example. Lets say we have a function with the argument player. Now I'm sure you are wondering how we are supposed to call this if we don't know what to put in the player argument, well ill explain. There is an array that stores all the players in it, this is what we will use for our argument. So to call it we could do both ways listed below.
Threading
Spoiler: click to open...
Now we know the basics lets move onto something a tiny bit more complex, threading. Threading is extremely useful within GSC, it is mainly used when doing loops, if we were to create a loop on the main thread we would probably freeze the game, but if we were to make a new thread what would happen is that the main thread would keep running and a new thread would run next to it therefore preventing the game from freezing.
Before creating a new thread that uses a loop there is one thing you should know which is that a loop must have a small wait time, if not your game will lag out and freeze ill provide an example shortly of how to add a wait.
to create a new thread its fairly straight forward all you need to do is put thread infront of where your calling your function.
GSC Functions
Spoiler: click to open...
I'm not sure if everyone is the same as me but I cant stand seeing ugly codes, this does not mean your code is bad it just makes it harder to understand for others and sometimes for yourself if you want to go back and review it to improve it or check something in it.
Below is an example of what I would call an "ugly" code. (I just used a function I found on NGU and made it ugly) Now looking at this code its very hard to read and everything is really close together now below I'm going to show you the neat version of this function You can see I have added indents and changed the iPrintln to something more formal. If you are unsure how to do your indentations correctly then I will briefly explain.
Indentations are not important and will not affect the way your code functions but like I said before they make your code much more readable and formal. If you are using a text editor like one of the ones I suggested at the start of this tutorial then getting your indentations correct is easy on every keyboard we have a magical button called the "tab button" what this will do is move your writing pointer across a number of spaces depending on where you have pressed tab from. To indent a function correctly is simple all you have to do is indent your code inside each new code block, example below and further explaination below.
Basically for each code block you define you have to press tab for each code block you are writing in, if you are writing in the main code block you only indent once if you are writing inside a code block thats in the main code block then you indent twice etc.
As for making your code more understandable try to name things to what they are so they make more sense, example below. So just remember to code your functions correctly especially if you are releasing them!
Cleaning up your script
Spoiler: click to open...
Toggles
Cluttered Way Clean WayI use this method myself and it is overall better. It may look like more code however the toggle function can be re-used with multiple functions. So instead of writing the same two lines over and over again you have one function that you call that does it for you. As well as the toggle function itself being smaller.
If statements and 'for' loops
Cluttered Way Clean WayWhen using if statements and for loops, you do not need to add braces to your code IF AND ONLY IF the code that you would put between the braces is one line of code. Also you don't need self.menuOpen == true, self.menuOpen by itself works just as fine. If will read the variable as true because if(self.menuOpen) is like saying if(true). Likewise with variables that are false. You don't need self.menuOpen == false, if(!self.menuOpen) will read the variable as false.
Function calling
Cluttered Way
Clean Way
OR
FAQ
Spoiler: click to open...
Q: My Game Freezes On Loading Screen A: This Means You Either Have A Non-Existant Function or A Syntax Error!
Q: Connection Interrupted then Freeze when calling a function or spawning in A: This Means You Have A Loop With No Waits