Check out our discord at https://discord.gg/3u69jMa
UnrealScript Language Reference: Difference between revisions
Created page with "== UnrealScript Language Reference == Tim Sweeney Epic MegaGames, Inc. http://www.epicgames.com/ == Introduction == '''Purpose of this document''' This is a technical document describing the UnrealScript language. It’s not a tutorial, nor does it provide detailed examples of useful UnrealScript code. For examples of UnrealScript prior to release of Unreal, the reader is referred to the source code to the Unreal scripts, which provides tens of thousands of lines o..." |
No edit summary |
||
Line 28: | Line 28: | ||
'''Example program structure''' | '''Example program structure''' | ||
This example illustrates a typical, simple UnrealScript class, and it highlights the syntax and features of UnrealScript. Note that this code may differ from that which appears in the current Unreal source, as this documentation is not synced with the code. | |||
<syntaxhighlight lang="c++"> | |||
//============================================================================= | |||
// TriggerLight. | |||
// A lightsource which can be triggered on or off. | |||
//============================================================================= | |||
class TriggerLight expands Light | |||
package(UnEngine); | |||
//----------------------------------------------------------------------------- | |||
// Variables. | |||
var() float ChangeTime; // Time light takes to change from on to off. | |||
var() bool bInitiallyOn; // Whether it's initially on. | |||
var() bool bDelayFullOn; // Delay then go full-on. | |||
var ELightType InitialType; // Initial type of light. | |||
var float InitialBrightness; // Initial brightness. | |||
var float Alpha, Direction; | |||
var actor Trigger; | |||
//----------------------------------------------------------------------------- | |||
// Engine functions. | |||
// Called at start of gameplay. | |||
function BeginPlay() | |||
{ | |||
// Remember initial light type and set new one. | |||
Disable( 'Tick' ); | |||
InitialType = LightType; | |||
InitialBrightness = LightBrightness; | |||
if( bInitiallyOn ) | |||
{ | |||
Alpha = 1.0; | |||
Direction = 1.0; | |||
} | |||
else | |||
{ | |||
LightType = LT_None; | |||
Alpha = 0.0; | |||
Direction = -1.0; | |||
} | |||
} | |||
// Called whenever time passes. | |||
function Tick( float DeltaTime ) | |||
{ | |||
LightType = InitialType; | |||
Alpha += Direction * DeltaTime / ChangeTime; | |||
if( Alpha > 1.0 ) | |||
{ | |||
Alpha = 1.0; | |||
Disable( 'Tick' ); | |||
if( Trigger != None ) | |||
Trigger.ResetTrigger(); | |||
} | |||
else if( Alpha < 0.0 ) | |||
{ | |||
Alpha = 0.0; | |||
Disable( 'Tick' ); | |||
LightType = LT_None; | |||
if( Trigger != None ) | |||
Trigger.ResetTrigger(); | |||
} | |||
if( !bDelayFullOn ) | |||
LightBrightness = Alpha * InitialBrightness; | |||
else if( (Direction>0 && Alpha!=1) || Alpha==0 ) | |||
LightBrightness = 0; | |||
else | |||
LightBrightness = InitialBrightness; | |||
} | |||
//----------------------------------------------------------------------------- | |||
// Public states. | |||
// Trigger turns the light on. | |||
state() TriggerTurnsOn | |||
{ | |||
function Trigger( actor Other, pawn EventInstigator ) | |||
{ | |||
Trigger = None; | |||
Direction = 1.0; | |||
Enable( 'Tick' ); | |||
} | |||
} | |||
// Trigger turns the light off. | |||
state() TriggerTurnsOff | |||
{ | |||
function Trigger( actor Other, pawn EventInstigator ) | |||
{ | |||
Trigger = None; | |||
Direction = -1.0; | |||
Enable( 'Tick' ); | |||
} | |||
} | |||
// Trigger toggles the light. | |||
state() TriggerToggle | |||
{ | |||
function Trigger( actor Other, pawn EventInstigator ) | |||
{ | |||
log("Toggle"); | |||
Trigger = Other; | |||
Direction *= -1; | |||
Enable( 'Tick' ); | |||
} | |||
} | |||
// Trigger controls the light. | |||
state() TriggerControl | |||
{ | |||
function Trigger( actor Other, pawn EventInstigator ) | |||
{ | |||
Trigger = Other; | |||
if( bInitiallyOn ) Direction = -1.0; | |||
else Direction = 1.0; | |||
Enable( 'Tick' ); | |||
} | |||
function UnTrigger( actor Other, pawn EventInstigator ) | |||
{ | |||
Trigger = Other; | |||
if( bInitiallyOn ) Direction = 1.0; | |||
else Direction = -1.0; | |||
Enable( 'Tick' ); | |||
} | |||
} | |||
</syntaxhighlight> |
Revision as of 14:30, 20 May 2024
UnrealScript Language Reference
Tim Sweeney
Epic MegaGames, Inc.
Introduction
Purpose of this document
This is a technical document describing the UnrealScript language. It’s not a tutorial, nor does it provide detailed examples of useful UnrealScript code. For examples of UnrealScript prior to release of Unreal, the reader is referred to the source code to the Unreal scripts, which provides tens of thousands of lines of working UnrealScript code which solves many problems such as AI, movement, inventory, and triggers. A good way to get started is by printing out the "Actor", "Object", "Pawn", "Inventory", and "Weapon" scripts.
This document assumes that the reader has a working knowledge of C/C++, is familiar with object-oriented programming, has played Unreal and has used the UnrealEd editing environment.
Design goals of UnrealScript
UnrealScript was created to provide the development team and the third-party Unreal developers with a powerful, built-in programming language that maps naturally onto the needs and nuances of game programming.
The major design goals of UnrealScript are:
- To support the major concepts of time, state, properties, and networking which traditional programming languages don’t address. This greatly simplifies UnrealScript code. The major complication in C/C++ based AI and game logic programming lies in dealing with events that take a certain amount of game time to complete, and with events which are dependent on aspects of the object’s state. In C/C++, this results in spaghetti-code that is hard to write, comprehend, maintain, and debug. UnrealScript includes native support for time, state, and network replication which greatly simplify game programming.
- To provide Java-style programming simplicity, object-orientation, and compile-time error checking. Much as Java brings a clean programming platform to Web programmers, UnrealScript provides an equally clean, simple, and robust programming language to 3D gaming. The major programming concepts which UnrealScript derives from Java are: a pointerless environment with automatic garbage collection; a simple single-inheretance class graph; strong compile-time type checking; a safe client-side execution "sandbox"; and the familiar look and feel of C/C++/Java code.
- To enable rich, high level programming in terms of game objects and interactions rather than bits and pixels. Where design tradeoffs had to be made in UnrealScript, I sacrificed execution speed for development simplicity and power. After all, the low-level, performance-critical code in Unreal is written in C/C++ where the performance gain outweighs the added complexity. UnrealScript operates at a level above that, at the object and interaction level, rather than the bits and pixels level.
During the early development of UnrealScript, several major different programming paradigms were explored and discarded before arriving at the current incarnation. First, I researched using the Sun and Microsoft Java VM’s for Windows as the basis of Unreal’s scripting language. It turned out that Java offered no programming benefits over C/C++ in the Unreal context, added frustraging restrictions due to the lack of needed language features (such as operator overloading), and turned out to be unfathomably slow due to both the overhead of the VM task switch and the inefficiencies of the Java garbage collector in the case of a large object graph. Second, I based an early implementation of UnrealScript on a Visual Basic variant, which worked fine, but was less friendly to programmers accustomed to C/C++. The final decision to base UnrealScript on a C++/Java variant was based on the desire to map game-specific concepts onto the language definition itself, and the need for speed and familiarity. This turned out to be a good decision, as it has greatly simplified many aspects of the Unreal codebase.
Example program structure
This example illustrates a typical, simple UnrealScript class, and it highlights the syntax and features of UnrealScript. Note that this code may differ from that which appears in the current Unreal source, as this documentation is not synced with the code.
//=============================================================================
// TriggerLight.
// A lightsource which can be triggered on or off.
//=============================================================================
class TriggerLight expands Light
package(UnEngine);
//-----------------------------------------------------------------------------
// Variables.
var() float ChangeTime; // Time light takes to change from on to off.
var() bool bInitiallyOn; // Whether it's initially on.
var() bool bDelayFullOn; // Delay then go full-on.
var ELightType InitialType; // Initial type of light.
var float InitialBrightness; // Initial brightness.
var float Alpha, Direction;
var actor Trigger;
//-----------------------------------------------------------------------------
// Engine functions.
// Called at start of gameplay.
function BeginPlay()
{
// Remember initial light type and set new one.
Disable( 'Tick' );
InitialType = LightType;
InitialBrightness = LightBrightness;
if( bInitiallyOn )
{
Alpha = 1.0;
Direction = 1.0;
}
else
{
LightType = LT_None;
Alpha = 0.0;
Direction = -1.0;
}
}
// Called whenever time passes.
function Tick( float DeltaTime )
{
LightType = InitialType;
Alpha += Direction * DeltaTime / ChangeTime;
if( Alpha > 1.0 )
{
Alpha = 1.0;
Disable( 'Tick' );
if( Trigger != None )
Trigger.ResetTrigger();
}
else if( Alpha < 0.0 )
{
Alpha = 0.0;
Disable( 'Tick' );
LightType = LT_None;
if( Trigger != None )
Trigger.ResetTrigger();
}
if( !bDelayFullOn )
LightBrightness = Alpha * InitialBrightness;
else if( (Direction>0 && Alpha!=1) || Alpha==0 )
LightBrightness = 0;
else
LightBrightness = InitialBrightness;
}
//-----------------------------------------------------------------------------
// Public states.
// Trigger turns the light on.
state() TriggerTurnsOn
{
function Trigger( actor Other, pawn EventInstigator )
{
Trigger = None;
Direction = 1.0;
Enable( 'Tick' );
}
}
// Trigger turns the light off.
state() TriggerTurnsOff
{
function Trigger( actor Other, pawn EventInstigator )
{
Trigger = None;
Direction = -1.0;
Enable( 'Tick' );
}
}
// Trigger toggles the light.
state() TriggerToggle
{
function Trigger( actor Other, pawn EventInstigator )
{
log("Toggle");
Trigger = Other;
Direction *= -1;
Enable( 'Tick' );
}
}
// Trigger controls the light.
state() TriggerControl
{
function Trigger( actor Other, pawn EventInstigator )
{
Trigger = Other;
if( bInitiallyOn ) Direction = -1.0;
else Direction = 1.0;
Enable( 'Tick' );
}
function UnTrigger( actor Other, pawn EventInstigator )
{
Trigger = Other;
if( bInitiallyOn ) Direction = 1.0;
else Direction = -1.0;
Enable( 'Tick' );
}
}