Always snap to grid

Legacy:UnrealScript For Visual Basic Programmers

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

Introduction[edit]

If you've ever programmed in Wikipedia:Visual Basic, you already know a lot of the basic concepts of coding (no pun intended). UnrealScript is a pretty full-fledged object-oriented language though, while Visual Basic's OOP support is more or less limited to the notion of having pre-defined objects (for instance, user interface elements) with some properties and methods. You can create and use your own classes in Visual Basic (in form of "class modules"), but the average Visual Basic programmer hardly ever will feel the need to do so.

A good read for getting into the notion of Object-Oriented Programming (OOP) is Object Oriented Programming Overview.

Familiar Concepts[edit]

Variables[edit]

In UnrealScript, all variables need to be predeclared using either var (for class-level variables) or local (for function-level variables). Variables declared on class level are globally accessible in that class. See UnrealScript Language Reference/Variables for details.

There's no "Variant" type in UnrealScript for variables that can hold any sort of data; variables are strongly typed, and you have to typecast them in most cases if you want to assign the value of a variable of one type to a variable of a different type.

Structured Programming[edit]

UnrealScript provides very similar means for structured programming (conditionals, loops) as Visual Basic. The following table lists corresponding idioms; see UnrealScript Language Reference/Program Structure for details of their syntax.

Statements in UnrealScript always end with a semicolon (;), not (like in Visual Basic) with an end-of-line. You can therefore continue a statement in the next physical line just by not ending it with a semicolon; there's no need of an explicit line-continuation character like Visual Basic's underscore (_).

User-defined Data Types[edit]

VB6 and below:
Type Name
  Var1 As DataType1
  Var2 As DataType1
  Var3 As DataType2
End Type
struct Name {
  var DataType1 Var1;
  var DataType1 Var2;
  var DataType2 Var3;
};
struct Name {
  var DataType1 Var1, Var2;

  var DataType2 Var3;
};
VB7 and above:
Structure Name
  Var1 As DataType1
  Var2 As DataType1
  Var3 As DataType2
End Structure
struct Name {
  var DataType1 Var1;
  var DataType1 Var2;
  var DataType2 Var3;
};
struct Name {
  var DataType1 Var1, Var2;

  var DataType2 Var3;
};

Comments[edit]

Rem Comment
' Comment
/* Multi-Line Comment */
// Comment

Conditionals[edit]

If condition Then
ElseIf condition Then
Else
End If
if (condition) {
} else if (condition) {
} else {
}
Select Case expression
  Case value1
    DoSomething

  Case value2, value3
    DoSomethingElse


  Case Else
    DoDefaultSomething
End Select
switch (expression) {
  case value1:
    DoSomething();
    break;
  case value2:
  case value3:
    DoSomethingElse();
    break;
  default:
    DoDefaultSomething();
}

Loops[edit]

For var = start To end
Next
for (var = start; var <= end; var++) {
}
For var = 1 To number_of_times
Next
for (var = 0; var < number_of_times; var++) {
}
For var = start To end Step step
Next
for (var = start; var <= end; var += step) {
}
Do
  If condition Then Exit Do
Loop
for (;;) {
  if (condition) break;
}
Do While condition
Loop
while (condition) {
}
Do
Loop Until condition
do {
} until (condition);

You can make your UScript more readable with the continue keyword. With it, it makes easier (thus, a good practice) to go to the next loop item if a certain condition is not met.

Subs and Functions[edit]

Sub subname (param1 As Integer, param2 As String)
  If condition Then Exit Sub
End Sub
function subname (int param1, string param2) {
  if (condition) return;
}
Function funcname (...) As Double
  If condition Then funcname = value : Exit Function
  funcname = value
End Function
function float funcname (...) {
  if (condition) return value;
  return value;
}

Arrays[edit]

Arrays in UScript are easy, there is just 1 major thing to remember. Arrays start at 0! always. No wierd:

Dim SomeArray(2042 to 35930) AS Integer

just a simple:

var int SomeArray[420];

There are also Dynamic Arrays (they also ALWAYS start at 0).

Event-Driven Architecture[edit]

Everything in UnrealScript is driven by engine events, very similar to Visual Basic. The Actor class declares a variety of events that are inherited by Actor subclasses and called by the engine for every object of those classes. Sometimes you can even control how and when events are called; the Timer event, for instance, is controlled by the SetTimer function.

To have objects of your class do something, implement some or all of its events. You can, of course, also call functions from code executed for other objects, including objects of different classes. The functions of the Mutator class, for example, are actually called by other UnrealScript code.

Related Topics[edit]