View Single Post
Old 09-29-2007, 02:44 AM   #4

Optimus_P-Fat's Avatar
Join Date: Jan 2006
Location: New Jersey
Posts: 3,633
Reply With Quote

How do I work with variables after I have declared them?

You are allowed to do two things with a variable (of the simple types above, we'll get into advanced uses later when it comes to variables of type STRUCT and OBJECT). The first thing you can do is ASSIGN it values. The second thing you can do is perform OPERATIONS using it.

ASSIGNING a value to a variable is done with the = operator. An operator is a symbol character used to denote a specific operation should be done in code. The = operator tells the code that you want to assign whatever VALUE is to the RIGHT of the = sign to whatever variable is to the LEFT of the = sign. For now, you may ONLY assign values of the correct TYPE to a variable (there is a way to convert, and sometimes it's even done implicitly, but I'll talk about that in a later lesson - for now, assume the rule is "it must be the correct type ALWAYS"). This means that if the variable's TYPE is int, only INTEGER values can be assigned to it. The following statement is an example of how to assign the value 5 to a variable of type int named a.

a = 5;

In such an example, assigning a value of 5 to an int typed variable is allowed. Assigning a string value of "Hello there!" to it is not. You may also assign the current value of another variable to a variable. Let's say I have declared two int variables, a and b. Let's say a's value is 5, and b's value is 10. I can enter the following statement:

a = b;

This assigns whatever the current value of b is (in this case, 10) to a.
Again, remember, an assignment statement, like all other statements, must end with a semi-colon.

Performing OPERATIONS on variables requires the use of operators. There are many different operators. Most of the basic operators are symbols, such a +, -, *, <, >, =, +=, >= and others. Some are even words, such as dot and cross. A list of operators can be found here:

You may ASSIGN a variable to the result of an OPERATION performed on one or more variables. As such, a statement like this, where a, b, and c are all int variables, is allowed:

a = b + c;

In this case, + is the operator, and in this context, means "add the numbers to the left and to the right of the + sign". There are operators for comparisons that will yield a true or false result, operators for math that will yield a result in the TYPE you used with them, operators for manipulating strings, such as concatenation, and others. There are also operators that work on their own as a statement, that can be used for incrementing or decrementing numbers. For instance, the following statement:


is a complete and valid statement. It is applying the operator ++ to a. In this case, this operator means "increment the value to the left by one" (it actually does one other important thing, but you don't need to worry about that for now). There are also operators that will perform an operation and assign in the same step. For example:

a += 2;

The above statement will add 2 to a, and assign that result to a. This has the same effect as incrementing the value in a by 2. In this case, the assignment didn't require an = opertator, since the += operator did the assignment in the same step as the addition.

There are MANY other operators that are used in unrealscript. This is the simplest set of examples and explanations, for those of you with NO experience whatsoever. If you have any questions on this, please post them in this thread, and I will answer them. Do not ask questions on the topics that I stated would be discussed later, as I will obviously be discussing them in a future thread. Make sure you have the concept of variables down in your head, as a CONCEPT, and not a standard set of words to be typed in a specific order, before you go any further with the tutorials. As I said, if you are curious, you may follow the links I provided for a more detailed list of types and operators.
Optimus_P-Fat is offline