Post tutorial RSS Scripting #3: Operators

I'm trying to explain here all the operators, which can be used in GSC.

Posted by on - Intermediate Server Side Coding

Arithmetical operators

You can use these operators here just like in math, but I'll explain them for the sake of clarity.

Addition

When using with numbers, it will add them together:

num = 5 + 3.06;

After executing the code above, the value of num will be 8.06. When used with strings, it concatenates them:

text = "apple";
rdy = text + "tree";

As a result, rdy will be appletree. You can sum up vectors too, this way it will add together the X, Y, and Z coordinates one-by-one:

vector = (133, -4, 0) + (-48, 3, 3477.7);

The value of vector will be (85, -1, 3477.7).

Subtraction

It works the same way as addiction, except the fact that you can't use it with strings. You can subtract two numbers or two vectors from each other in the way described above.

Multiplication, division

You can use these only with numbers. However, even if you are working with integers, division will always return a floating point number.

Abbreviation

You can make these statements shorter in a special way, so instead of this:

num = num + 5;

You can write this:

num += 5;

This way, your code can be shorter, and cleaner. Obviously, you can use it with the rest of the operators too (ie *=).
Moreover, there is a shorter version if you want to increment or decrement your variable only by one. Instead of these:

num = num + 1;
num = num - 1;

You can use these:

num++;
num--;

It is recommended that you always use the shortest possible forms of these operators to maintain the quality and readability of your code. And believe me; you do want that.

Comparison operators

As a result of using these with variables, you will always get true, or false. With the help of these operators, you can check if a number is bigger than another one, if two strings are the same, or if a boolean variable is true or not.

Equality

You can check the equality of two objects of the same type by using the == operator. You can move the result of the equation into a variable the following way:

logical = num1 == 4;

The value of logical will be true if num1 is 4, otherwise false.
Please note that == true can be omitted in case you would like to know if a boolean variable holds a value of true. Basically, this statement:

sth == true

equals to this:

sth

You will need it soon when checking conditions in a branch, because it will make your code much easier to read. In addition, all numbers can be handled as if they were booleans. Zero means false, while every other non-zero value will be interpreted as true.

Smaller, greater

You can use these only with numbers:

logical = num1 > num2;

The logical variable will be true if num1 is greater than num2, otherwise false.

Mixed

You can mix them the same way you would in math, for example:

logical = num1 <= num2;

Logical operators

And

The operator of 'and' is &&. By using it, you can list a series of expressions of which all of them has to be true in order for the whole to be evaluated to true.

logical1 = logical2 && logical3 && logical4;

Or

The operator of 'or' is ||. By using it, you can list a series of expressions of which at least one of them has to be true in order for the whole to be evaluated to true.

logical1 = logical2 || logical3;

Compound

If you would like to use these in one expression, you have to use brackets, since the game must be able to decide the order it should interpret the expressions in. Example:

logical2 = (logical1 || num1 < 5) && (str1 != "smallredtractor");

Negation

The operator of negation in programming is (usually) the exclamation mark (!).
You can negate the value of a variable, but obviously it can only be used with logical values. If you write this:

logical1 = !logical2;

Then if logical2 is true, then logical1 will be false, and if logical2 is false, then logical1 will be true.
You can negate an equation too (it can be used with every variable type which you can use the equation with). If you don't want to check if str is "bush", but instead if it is NOT "bush", then you can simply write this:

neg = str != "bush";

neg will be true, if str is not "bush". Please do not forget when negating an equation that you must use only one equal sign!
We can also negate a complex logical expression by putting it in brackets, and placing the exclamation mark right before it.

sth = i == 4 && !(num1 == 5 || num2 == 5);

Bitwise operators

These are used very rarely, but if we use them when really needed, we can reduce the memory usage of the mod, and it can evaluate some operations much faster. Bitwise variables (basically integer variables, but we are handling them using bitwise operators) can be used (for example) if we want to store which items are owned by the player, and which are not.

This is especially useful because we can tell exactly which of these values are true and which aren't without actually storing all of them one-by-one in different variables or a huge array.

Call of Duty 4 uses these special values to tell which intel has been found by the player during the Single Player campaign. In my Hide 'n Seek mod, I use them to tell which map a specific player has already played on, and which are yet unplayed by them.

We must work with these in binary system. The ID of the first bit is always 1, and the ID of every other bit is two times bigger, than the previous one's. So if you want to store 4 values, then the ID of the first one is 1, the second one is 2, the third is 4, and the fourth is 8. If the first and the third bits are true, then the value of your variable will sum up to 5 (1 + 4). More about the binary system can be found here: En.wikipedia.org. It looks scary, but it really isn't a big deal once you get the idea.

Check

You can check easily, if a specific (e.g. 4th) bit is true using the & character:

logical = 8 & val;

Addition

Since these are simple numbers, a + sign can also add them together, but there is a special sign for adding bitwise values: |. It doesn't matter which one you use, it really is just to keep the code clean and readable:

num = 8 | val;

Shift

You will most likely never use these operators, unless you want to do something really extreme. The operator of left shift is <<, while the same for right shift is >>. This shift is done in binary system, so the meaning of 5 << 2 is that we are shifting 5 left by 2 places:

5 in binary system is 101;
we shift it to the left by appending two zeros to it: 10100;
by converting it back to a regular number, we get a result of 20 (4 + 16).

We can shift the same way to the right, but in that case, we subtract zeros from the end instead of adding them.

Comments
Guest
Guest

next tut?

Reply Good karma Bad karma+1 vote
iCore Author
iCore

Sadly, currently I have no time to finish the others :/

Reply Good karma+1 vote
Guest
Guest

please finish the other its helping me out in a great way

Reply Good karma Bad karma+1 vote
Argoon
Argoon

Call of duty scripting is really powerful is almost a entire programming language!
I use Doom 3/idtech 4 scripting and it can't do arrays or structs for example.

Reply Good karma Bad karma+1 vote
Guest
Guest

This comment is currently awaiting admin approval, join now to view.

HugoCezar
HugoCezar

I am looking to study gsc language, this was the only site teaching and free. Please post more tutorials.

Reply Good karma Bad karma+1 vote
HugoCezar
HugoCezar

I researched many tutorials, gsc classes, but I never found one that started from the basics, this was the only didactic site I found to study. Please!

Reply Good karma Bad karma+1 vote
Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.