Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 extends Light;
- //---------------------------------------------------------------------
- // 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 &amp;amp;&amp;amp; 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' );
- }
- }
- The key elements to look at in this script are:
- The class declaration. Each class "extends" (derives from) one parent class, and each class belongs to a "package," a collection of objects that are distributed together. All functions and variables belong to a class, and are only accessible through an actor that belongs to that class. There are no system-wide global functions or variables. More Details
- The variable declarations. UnrealScript supports a very diverse set of variable types including most base C/Java types, object references, structs, and arrays. In addition, variables can be made into editable properties, which designers can access in UnrealEd without any programming. These properties are designated using the var() syntax, instead of var. More Details
- The functions. Functions can take a list of parameters, and they optionally return a value. Functions can have local variables. Some functions are called by the Unreal engine itself (such as BeginPlay), and some functions are called from other script code elsewhere (such as Trigger). More Details
- The code. All of the standard C and Java keywords are supported, like for, while, break, switch, if, and so on. Braces and semicolons are used in UnrealScript as in C, C++, and Java.
- Actor and object references. Here you see several cases where a function is called within another object, using an object reference. More Details
- The "state" keyword. This script defines several "states", which are groupings of functions, variables, and code that are executed only when the actor is in that state. More Details
- Note that all keywords, variable names, functions, and object names in UnrealScript are case-insensitive. To UnrealScript, Demon, demON, and demon are the same thing.
Advertisement
Add Comment
Please, Sign In to add comment