I'm a doctor, not a mechanic
UnrealScript
- See UnrealScript overview for all Unreal Wiki topics about learning and using UnrealScript.
UnrealScript is the Unreal Engine's scripting language. It is a staticly/strongly-typed, object-oriented and event-driven programming language very similar to Java and C++, with some influences from Visual Basic. If you want to learn UnrealScript, you should make yourself familiar with the fundamental concepts of object-oriented programming. It definitely helps already to know another programming language, preferably one that is strongly-typed and object-oriented as well.
Anatomy of an UnrealScript source file[edit]
A typical UnrealScript source file might look similar to the following example from UT2004:
-
class DMRosterBeatTeam extends xDMRoster;
-
-
/*
-
* Roster for deathmatch levels.
-
* Each level has its own roster.
-
* This special roster subclass is used to populate an enemy team with the
-
* player's selected teammates.
-
*/
-
-
// called immediately after spawning the roster class
-
function Initialize(int TeamBots)
-
{
-
local GameProfile GP;
-
local int i, j;
-
-
GP = Level.Game.CurrentGameProfile;
-
if ( GP == none ) {
-
Log("DMRosterBeatTeam::Initialized() failed. GameProfile == none.");
-
return;
-
}
-
-
// create roster entries for single player's teammates
-
for ( i=0; i<GP.PlayerTeam.Length; i++ )
-
{
-
j = Roster.Length;
-
Roster.Length = Roster.Length + 1;
-
Roster[j] = class'xRosterEntry'.Static.CreateRosterEntryCharacter(GP.PlayerTeam[i]);
-
}
-
-
// remaining team-specific info, might be used in menus at some point
-
TeamName = GP.TeamName;
-
TeamSymbolName = GP.TeamSymbolName;
-
-
super.Initialize(TeamBots);
-
}
-
-
defaultproperties
-
{
-
TeamName="Death Match"
-
TeamSymbolName="TeamSymbols_UT2003.Sym01"
-
}
As you can see, UnrealScript is a curly brace programming language: blocks of code are enclosed in curly braces. Each code statement is either followed by a block or terminated by a semicolon.
- Line 1 contains the mandatory class declaration. This file contains a class with the name DMRosterBeatTeam. The extends clause specifies that this class inherits attributes, methods and other class members from a class with the name xDMRoster, which is said to be the "super class" or "parent class" of DMRosterBeatTeam.
- Lines 3 to 8 are an example for a block comment. The important parts here are the characters
/*
on line 3 and*/
on line 8. Everything between them is ignored by the UnrealScript compiler. - Line 10 contains another type of comment, an end-of-line comment or just line comment. Outside of block comments this type of comment starts with the characters
//
and continues to the end of the same line. As with block comments, everything on the line following the double slashes is ignored by the compiler. - Line 11 contains a declaration for an instance function. The function's name is specified to be "Initialize" and the function has one parameter of type int called "TeamBots". The block of code on lines 12 to 35 is the function's "body". Some functions do not have a body. The declaration of those functions is terminated by a semicolon instead.
- The line 13 declares a local variable of type GameProfile, a class defined elsewhere, and gives it the name "GP". The variable is initially empty.
- Line 16 assigns a value to the local variable "GP" so it no longer is empty. To determine the value, the function accesses the variable "Level". This variable is not declared here, but in a parent class of DMRosterBeatTeam. The object referenced by "Level" has a variable of its own, called "Game", whose contained object in turn has a variable called "CurrentGameProfile". The content of that variable is assigned to our local "GP" variable.
- On line 17 there's an If statement with a block of code that ends on line 20. That block of code is only executed if the expression in the parentheses following the keyword If returns the value True. The If condition checks, whether the content of the local "GP" variable equals the special value None, which simply means "empty".
- Lines 18 and 19 are only executed if the condition (here: "GP is empty") is true. So, if GP is empty, first the function with the name "Log" is called to write a message to the log file, then the Return statement exits the function. In this case the code after the If block will not be executed. In other cases the If block might not contain a Return and code execution would continue after the If block.
- On line 23 there's a typical For loop that iterates over a dynamic array. The code on lines 25 to 27 is executed for each element of the "PlayerTeam" array in the object stored in the local "GP" variable. Notice how the If statement on line 17 ensured that "GP" actually contains an object.
- Line 34 calls another version of the "Initialize" function, that was declared in a parent class of DMRosterBeatTeam, passing on the TeamBots parameter. This call tells us, that the "Initialize" function of the DMRosterBeatTeam class actually overrides a function with the same name in its parent class xDMRoster or another parent class further up the class hierarchy.
- Lines 37 to 41 are the defaultproperties block. This block defines default values for class variables. Both "TeamName" and "TeamSymbolName" are inherited from a parent class.
Declarations | Preprocessor • Classes • Interfaces • Cpptext • Constants • Enums • Structs • Variables (Metadata) • Replication block • Operators • Delegates • Functions • States • Defaultproperties (Subobjects) |
---|---|
Types | bool • byte • float • int • name • string • Object • Class • Enums • Structs (Vector ⋅ Rotator ⋅ Quat ⋅ Color) • Static arrays • Dynamic arrays • Delegates • Typecasting |
Literals | Boolean • Float • Integer • Names • Objects (None ⋅ Self) • Vectors • Rotators • Strings |
Flow | GoTo • If • Assert • Return • Stop • Switch • While • Do...Until • For • ForEach • Break • Continue |
Specifiers | Super • Global • Static • Default • Const |
UnrealScript | Syntax • .UC • .UCI • .UPKG • Comments • #directives • Native |