For those of you who have a good programming experiance, don't bother reading this, this is just a
simple thing I decided to type up to prepare those who have no programming experiance for some of the common things in programming languages that will now be in gscript, I aplogize for this being an easy tutorial but I don't really want anyone being confused about these simple things:
1.
Function Params
currently, the only way you can do functions in the current engine is by calling it like
functionname();
and declaring it like
function functionname() {
//script
}
The new engine introduces a popular and great way to 'pass'/declare variables on a function, this is done by using the following:
calling it:
functionname("test",1,a);
declaring it:
function functionname(x,y,z){
//script
}
In the new engine, it gets rid of the need to use setstring to declare a string, you can just go var = "test"; now, so don't be confused by this:
in the example stated before, it is the same as doing in the current engine
x = "test";
y = 1;
z = a;
functionname();
as you can see this is a much more efficent and shorter way to pass/declare variables on a function
2. Declaring a Variable using a Function
what I am talking about, is doing
var = functionname();
Currently you cannot do that, this is an example of using it:
NPC Code:
function functionname(var){
if(var > 10) return(true);
else return(false);
}
The command/function 'return' is used to set the variable calling the function, yes I know right now it is return; you can also use return var; but in the new engine, most of the current commands are converted into functions
3. Switch Statements
these are another way to do else-if statements, some people prefer to use these as they can look neater, example:
NPC Code:
switch(@a) {
case "test":
setplayerprop #c,test;
break;
case 1:
setplayerprop #c,1;
break;
default:
setplayerprop #c,neither 1 or test;
}
this could be considered the equivilent of
NPC Code:
if (a == "test") {
setplayerprop #c,test;
} else if (a == 1) {
setplayerprop #c,1;
} else{
setplayerprop #c, neither 1 or test;
}
personally, I think the switch statement looks neater
the way to start them out is by going
switch (varname) {
}
Please don't ask why I chose to use @, I will explain that on a tutorial explaining some of the new engine's features later.
after that, you can add cases, a case, such as
case "test":
is basically like
if (varname == "test")
if the case is true, it will run all the scripts under it until it reaches the break; command (break(); will work too I believe, I am just using break; for the sake of being less confusing)
also, as you have noticed, I used something that wasn't considered a case, I used
default:
default is basically used when the var isn't equal to any of the cases above it, so it will execute the commands under default: until it finds a break; this is optional so don't feel like you need to include it
4.
do-while loops
these are really similar to while loops, with one difference which I will explain later
NPC Code:
do {
//commands;
} while (condition);
now you may ask,
why not just do
NPC Code:
while (condition) {
//commands;
}
The main difference between while and do-while loops, is that it will execute at least once before it checks the while condition, if the while condition is true, it will continue to do the loop.
5.
More Operators
Post and Pre increment / decrement
post:
var = n++;
this is basically a short way to do
var = n;
n++;
var = ++n;
this is basically a short way or doing
n++;
var = n;
same goes for decrement (--)
Modulus Assignment a %= b
Power Assignment a ^= b
6. Multi Dimention Arrays
An example:
a = b[0][1];
c = b[1][0];
one way you can construct 'b' is by doing
b = {{0,1},{2,3}};
if b was the array in the previous example, a would become 1, c would become 2
there are no limits to how many dimentions you can use, but I really don't see any need to go beyond 3d arrays
7. for loop addition:
I don't really know how to name this, but it is the way java does it, and it is the same as php's
foreach (a as b)
except in gscript, it is
for (b: a)
example:
NPC Code:
a = {5,4,3,2,1};
setarray b,arraylen(a);
i=0;
for(c: a){
b[i] = c;
i++;
}
yes, that was a poor example, but a more practical use would be say...
for(player: allplayers){ ...
which is used on the playerlist script (allplayers can be accessed but not written to clientside)
what it is doing, is taking each instance of the array a, and assigning it as the value b, it can be the equivilent of
NPC Code:
for (i=0; i<arraylen(a); i++){
b = a[i];
}
I may have forgotten some of the new features that are in other coding languages, but if I remember them, I will modify this post to add it, I apologize in advanced for how bad this tutorial was, I admit I kind of lost motivation near the end but I hope it helps

comments and critisism is welcome (constructive only please)
EDIT: made the for(var: array) explanation more clear