Omnip)o(tentS Forums

Omnip)o(tentS Forums (
-   Mappers' Corner (
-   -   UScript Tutorials 101: Variables (

Optimus_P-Fat 09-29-2007 03:42 AM

UScript Tutorials 101: Variables
OK all, here we go. I'm going to multiple lessons, all in separate threads for this. I'm going to start off with the most basic concept in programming in general, and how it works in UScript. If you are expecting to see anything in this thread about replication, the network in general, objects, parent classes, meshes, collision, etc, you are in the wrong thread (I will be be dealing with all of that wonderful stuff in later threads!) I will be posting this in multiple posts, so be prepared and don't give me any lip for double or triple-posting. :)

Before I begin, let me just say something - those of you who are new to programming, or just to UScript, I suggest you start reading here. Please, please, PLEASE, do not run off into copy-paste-cross-fingers land without knowing what it is you are copy-and-pasting. It's bad for you, because you don't learn anything, and it's bad for the people who will one day be using your content, because god forbid if there is ever a serious bug (take for instance, the shadows crash in Freon), YOU will be the person who will need to not only find it, but fix it. If you are blindly copy and pasting code that looks like it does what you think, but you don't actually know the details of how it does what it does, this task is not going to be easy, and sometimes it may be downright impossible.

Having said that, let's move onto UScript Lesson 101: Variables!

Optimus_P-Fat 09-29-2007 03:43 AM

What IS a variable?

A variable is something that you use to store a value. In most languages, including unrealscript, variables have 3 parts: a TYPE, a NAME, and a VALUE.

The TYPE of the variable is just that - what it is. For example, in UnrealScript, you can have a variable that is an integer value (0, 1, 2, etc), a floating point value (1.56, 0.4, 3.8, etc), etc. The TYPE determines what and how you store the VALUE.

The NAME of the variable is a name that you give it, and you refer to it by in your code. Once you tell the code that you have this variable (which is called DECLARING the variable - more on it in a second), you then refer to it everywhere else by the name you declared it with. This can be anything, though naming rules differ between languages. In UnrealScript your variable names can consist of letters, numbers and the underscore "_", BUT they cannot STARt with a number. So, "my_value", "_a45blah" and "blah32" are all acceptable variable NAMES.

The VALUE of the variable is just that - the actual VALUE assigned to it. In the case of an integer variable this would be something like 2, or 3, or 4. For a floating point, this would be a decimal number like 43.5. What you can put in the VALUE is determined by the TYPE of the variable. You change the value of the variable by ASSIGNING a value to it. This can be something that is referred to as an IMMEDIATE, such as the actual number 5, or it can be another variable.

Optimus_P-Fat 09-29-2007 03:43 AM

How to declare variables in UnrealScript

DECLARING your variable is the first step towards being able to use it in you script logic. Variables are ALWAYS declared at the top of your class file, underneath the declaration of your class (for our purposes now, this is the first line of code in the file - it has the name of your class in it, more on this in a later lesson). They can also be declared LOCALLY in FUNCTIONS, but right now, we will not touch on that until a future lesson on FUNCTIONS. The way you declare a variable is as such:

var<group> <modifiers> <type> <name>;

The keyword var tells the code you are declaring a variable for use later.

The <group> portion is optional - if you specify a name in parenthesis here, you are telling Unrealscript you want this variable to show up in UEd under that group name. If you put empty parenthesis here, you are saying "display this in UEd, but make it part of the default group", and finally if you omit the <group> portion altogether, you are saying that this variable is for internal script usage only and should NOT be exposed through UEd.

The <modifiers> section is 0 or more keywords that apply advanced handling to the variable. For our current situation, we will not discuss these here, and you are not REQUIRED to add modifiers to declare a variable (although you will need some to do specific things with the variable later - but that is not important in this lesson)

The <type> field is a single keyword and is REQUIRED in your variable declaration. This is where you tell the code what TYPE of variable this will be. I will discuss some of the simple types here. For a more complete list of variable TYPEs, please go here:

- bool - this is a boolean value. It's ONLY possible values are true or false.

- int - this is an integer value. It's possible values are whole numbers ranging from -2147483648 to 2147483647.

- byte - this is a special kind of integer value - it is a single byte. As such, it's values range from 0 to 255.

- float - this is a floating point decimal value - i.e. numbers such as 2.5, 4.83, etc.

- string - a STRING in programming is a 'string' of characters grouped together and treated as a single entity or value. Strings in UnrealScript as specified by wrapping them in double quotes, like such: "my string value!", "whoopie doo da", or "she said @$@!" In order to have a double quote in your string, you must do something known as ESCAPING the character with a backslash, like so: "This: \" is a string with a quote in it!". As you may have guessed, getting a backslash in the string requires the same procedure, as such: "This is a backslash: \\". To get a newline, you can use \n.

There are other types you may use, but for this tutorial we will stick with the basics. You may read about the other types in the link I provided above.

The <name> field is also REQUIRED, and is the name that you are giving to the variable. As described above, it can contain letters, numbers, and the underscore, but may NOT begin with a number.

Finally, there is the important SEMI-COLON. The semi-colon is the way you can tell the code, "this line is complete"- think of it as the period to a sentence in code-land. All code statements must end with a semi-colon.

Example variable declarations:

var int FingerCount;
var() string MyMapName;
var(Display) float DrawScale;

In the examples above, the first line declares an integer type variable, named FingerCount, that will NOT be visible in UEd. The second line declares a string variable, called MyMapName that WILL be accessible in UEd in the default group for your object. The third line declares a float type variable named DrawScale that will show up in UEd under the Display group.

Optimus_P-Fat 09-29-2007 03:44 AM

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.

All times are GMT -5. The time now is 04:47 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2004 - 2017 Omnip)o(tentS. All rights reserved.