You can use these operators here just like in math, but I'll explain them for the sake of clarity.
When using with numbers, it will add them together:
After executing the code above, the value of num will be 8.06. When used with strings, it concatenates them:
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:
The value of vector will be (85, -1, 3477.7).
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.
You can use these only with numbers. However, even if you are working with integers, division will always return a floating point number.
You can make these statements shorter in a special way, so instead of this:
You can write this:
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:
You can use these:
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.
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.
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:
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:
equals to this:
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.
You can use these only with numbers:
The logical variable will be true if num1 is greater than num2, otherwise false.
You can mix them the same way you would in math, for example:
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.
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.
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:
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:
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 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.
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.
You can check easily, if a specific (e.g. 4th) bit is true using the & character:
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:
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.