Worst-case scenario: the UEd Goblin wipes the map and burns down your house.
UnrealScript compared with other languages
Please help improve this article or section by expanding it. Further information might be found on the talk page. |
UnrealScript is a curly bracket programming language, so its syntax should look familiar to you if you already know other languages such as C++, Java, PHP or any ECMAScript dialect, such as JavaScript or ActionScript. This article will shed some light on the differences between UnrealScript and other languages, especially the ones mentioned above.
Unique UnrealScript features[edit]
One of the unique language features in UnrealScript are states, which can be used to implement different behavior in the same class, depending on the current situation.
Another feature is the defaultproperties block, which specifies the initial values for newly-declared and inherited variables alike. Starting with Unreal Engine 2 this defaults block can be used to create object instances at compile time.
UnrealScript also allows (and requires) you to define server-client communication (replication) at the language level.
UnrealScript vs. Java[edit]
UnrealScript's entire design was originally based on Java, so it's not surprising that both languages are quite similar.
Main differences:
- UnrealScript is a completely case-insensitive language: Identifiers (variable/function/class names) and keywords make no difference between upper- and lower-case letters.
- Only exactly one class definition is allowed per UnrealScript source file. The declared class name and the file name (without extension) must match.
- There is only a single "layer" of packages and all classes must be contained in a named package. Unreal packages can also contain other objects, such as textures and sounds, or even entire levels. Packages are always compiled as a whole and end up being a single file.
- There are no "import" statements in UnrealScript. If another package is required by a class in the package to compile, that package must be loaded at compile time, which is usually done through the INI file entry "EditPackages" or ModPackages".
- Classes must specify which parent class they extend, even if it's just the Object class itself. Interfaces are only available starting with Unreal Engine 3.
- The type string is a primitive data type, while in Java strings are actually instances of the java.lang.String class. Also, there are no object versions of the primitive UnrealScript data types.
- UnrealScript does not allow method overloading, only overriding. When overriding UnrealScript methods, it is not allowed to "narrow" the return type to a more derived class. You can declare and overload operators in UnrealScript, though.
- UnrealScript knows two array types, static arrays and dynamic arrays. The length of static arrays is defined at compile time and cannot be changed at runtime. Dynamic arrays on the other hand are much like Java's java.util.ArrayList class, except that dynamic arrays are no objects, but can also be used with a primitive inner type. UnrealScript does not allow arrays of type bool and does not support multi-dimensional arrays.
- UnrealScript doesn't support generics, annotations or exception handling. Things like NullPointerException or IndexOutOfBoundsException are handled gracefully in UnrealScript by just returning null values after writing a warning message to the log file. Typecasting object references is safe and also works for empty references.
UnrealScript vs. C++[edit]
Apart from the many Java-like features, UnrealScript also borrows some features from C++, for example operator definitions.
- All UnrealScript code must be contained in classes. There are no global functions, variables or constants.
- Up to Unreal Engine 2 there is no built-in preprocessor support in UnrealScript. There are a few third-party preprocessor tools, though.
- UnrealScript only supports single inheritance and all classes must have a parent class, except for class Object, the upper-most class in the hierarchy. In Unreal Engine 3 you can define and implement common interfaces for otherwise unrelated classes to declare common function prototypes.
- Functions can only be overridden, not overloaded. They are virtual by default, unless declared final.
- Operators can be overloaded, but not overridden. Only a fixed set of operator symbols are available, but operators can also be identifiers. Regular operators must have exactly two parameters and are always used in infix notation (i.e. like "a + b"), prefix and postfix operators always have exactly one parameter.
- UnrealScript has no constructor or destructor methods. Object initialization happens automatically and in most cases certain special methods are called automatically. Object destruction happens implicitly through the garbage collector. Actor subclasses are destroyed explicitly during the game and implicitly at map change. Before explicit destruction, the Destroyed() method of the destroyed Actor instance is called.
- UnrealScript doesn't provide pointers. The memory addresses of values are not accessible. Arrays are explicitly declared as static or dynamic arrays. Multi-dimensional arrays are not supported, but can be faked by using arrays or structs containing array members.
- The conditions in If statements and While loops are required to be of the type bool. Other types are not converted to boolean values automatically.
- Structs are not public classes, they are really just collections of variables.
- The values of enum constants are hidden in UnrealScript. Implicitly they correspond to the byte values 0, 1, 2, ... in the same order as they were defined.