| Home Page | Recent Changes | Preferences

UnrealScript Language Reference/Expressions

Expressions

Constants

In UnrealScript, you can specify constant values of nearly all data types:

  • Integer and byte constants are specified with simple numbers, for example: 123
  • If you must specify an integer or byte constant in hexidecimal format, use i.e.: 0x123
  • Floating point constants are specified with decimal numbers like: 456.789
  • String constants must be enclosed in double quotes, for example: "MyString"
  • Name constants must be enclosed in single quotes, for example 'MyName'
  • Vector constants contain X, Y, and Z values like this: Vect(1.0,2.0,4.0)
  • Rotator constants contain Pitch, Yaw, and Roll values like this: Rot(0x8000,0x4000,0)
  • The "None" constant refers to "no object" (or equivalantly, "no actor").
  • The "Self" constant refers to "this object" (or equivalantly, "this actor"), i.e. the object whose script is executing.
  • General object constants are specified by the object type followed by the object name in single quotes, for example: texture ĎDefaultí
  • EnumCount gives you the number of elements in an enumeration, for example: EnumCount(ELightType)
  • ArrayCount gives you the number of elements in an array, for example: ArrayCount(Touching)

You can use the "const" keyword to declare constants which you can later refer to by name.  For example:

const LargeNumber=123456;
const PI=3.14159;
const MyName="Tim";
const Northeast=Vect(1.0,1.0,0.0);

Constants can be defined within classes or within structs.

To access a constant which was declared in another class, use the "classname.constname" syntax, for example: Pawn.LargeNumber

Expressions

To assign a value to a variable, use "=" like this:

function Test()
{
       local int i;
       local string[80] s;
       local vector v, q;

       i = 10; // Assign a value to integer variable i.
       s = "Hello!"; // Assign a value to string variable s.
       v = q; // Copy value of vector q to v.
}

In UnrealScript, whenever a function or other expression requires a certain type of data (for example, an "float"), and you specify a different type of data (for example, an "int), the compiler will try to convert the value you give to the proper type. Conversions among all the numerical data types (byte, int, and float) happen automatically, without any work on your part.

UnrealScript is also able to many other built-in data types to other types, if you explicitly convert them in code. The syntax for this is:

function Test()
{
       local int i;
       local string[80] s;
       local vector v, q;
       local rotator r;
 
       s = string(i); // Convert integer i to a string, and assign it to s.
       s = string(v); // Convert vector v to a string, and assign it to s.
       v = q + vector(r); // Convert rotation r to a vector, and add q.
}

Here is the complete set of non-automatic conversions you can use in UnrealScript:

String to Byte, Int, Float
Tries to convert a string like "123" to a value like 123. If the string doesnít represent a value, the result is 0.
Byte, Int, Float, Vector, Rotation to String
Converts the number to its textual representation.
String to Vector, Rotation
Tries to parse the vector or rotationís textual representation.
String to Bool
Converts the case-insensitive words "True" or "False" to True and False; converts any non-zero value to True; everything else is False.
Bool to String
Result is either "True" or "False".
Byte, Int, Float, Vector, Rotation to Bool
Converts nonzero values to True; zero values to False.
Bool to Byte, Int, Float
Converts True to 1; False to 0.
Name to String
Converts the name to the text equivalant.
Rotator to Vector
Returns a vector facing "forward" according to the rotation.
Vector to Rotator
Returns a rotator pitching and yawing in the direction of the vector; roll is zero.
Object (or Actor) to Int
Returns an integer that is guaranteed unique for that object.
Object (or Actor) to Bool
Returns False if the object is None; True otherwise.
Object (or Actor) to String
Returns a textual representation of the object.

Converting Object References Among Classes

Just like the conversion functions above, which convert among simple datatypes, in UnrealScript you can convert actor and object references among various types. For example, all actors have a variable named "Target", which is a reference to another actor. Say you are writing a script where you need to check and see if your Target belongs to the "Pawn" actor class, and you need to do something special with your target that only makes sense when itís a pawn – for example, you need to call one of the Pawn functions. The actor cast operators let you do this. Hereís an example:

var actor Target;
//...

function TestActorConversions()
{
       local Pawn P;

       // See if my target is a Pawn.
       P = Pawn(Target);
       if( P != None )
       {
               // Target is a pawn, so set its Enemy to Self.
               P.Enemy = Self;
       }
       else
       {
              // Target is not a pawn.
       }
}

To perform an actor conversion, type the class name followed by the actor expression you wish to convert, in parenthesis. Such a conversion will either succeed or fail based on whether the conversion is sensible. In the above example, if your Target is referencing a Trigger object rather than a pawn, the expression Pawn(Target) will return "None", since a Trigger canít be converted to a Pawn. However, if your Target is referencing a Brute object, the conversion will successfully return the Brute, because Brute is a subclass of Pawn.

Thus, actor conversions have two purposes: First, you can use them to see if a certain actor reference belongs to a certain class. Second, you can use them to convert an actor reference from one class to a more specific class. Note that these conversions donít affect the actor youíre converting at all – they just enable UnrealScript to treat the actor reference as if it were a more specific type.

Another example of conversions lies in the Inventory script. Each Inventory actor is owned by a Pawn, even though its Owner variable can refer to any Actor. So a common theme in the Inventory code is to cast Owner to a Pawn, for example:

// Called by engine when destroyed.
function Destroyed()
{
       // Remove from owner's inventory.
       if( Pawn(Owner)!=None )
               Pawn(Owner).DeleteInventory( Self );
};
see also Typecasting on the Wiki

Prev Page: /VariablesSection 3 of 9 – Next Page: /Functions

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In