Sniper's Paradise-Unreal for Script Beginners


By Mark Hamilton

Introduction:
Unreal Script, commonly known as Uscript was designed by Epic Games as a way to engineer Unreal Engine modifications without recompiling the engine's core, which is designed in C++. Unreal Script syntax was designed based on Java and JavaScript, thats why many web developers or even people who have done almost no code at all, find this language easy to learn, and simple to understand. Even people who have never coded in their life will enjoy this tutorial and maybe people who have known it for a while will learn something new.

Quick Start:
Unreal Script requires very few things, basically a text editor, and a way to compile. Compilers include such things as UnrealEd found with most games, UMake, or just using a UCC -Make command. This tutorial will not encompass compiling as its a language reference guide, and not a teaching tutorial on compiling.

Compiling tutorials can be found online, and can sometimes be specific to each game.

You will want to have atleast a general text editor running at this point since we are going to just dive right into every part of the uc file from class definitions to the final defaultproperties explaining everything along the way to the best of our knowledge.

What is a "Class"?
A class is a set of written code. Classes in Unreal are based off a hierarchy, ending with Object at the top, and Actor being the common favorite as the top. All classes are under neith either Actor or if you must be exact, Object.

For the purpous of this tutorial, and probably mostly everyone's use of coding, Actor as the top of the hierarchy will be just fine.

For those who don't know what a hierarchy is, its a categorization of a group of people according to ability or status, but for this just think of people being the classes.

Starting your .UC File:
Everything you do in UnrealScript will need to be defined, things called class definitions start you out, an example class definition is:

Code:
class Edit expands Editor;
The class definition there states simply that the class Edit expands the parent class Editor, a parent class is something that is above your current one. A class declarative is simple the word "class" followed by the class name, and then the word "expands" with the name of the class your extending following it. Ofcourse, notice the semicolon, you'll get used to putting that in a lot of places as it works in most OOP as the way to tell the compiler that your at the end of what your doing, and moving on.

Execs:
Execs, otherwise known as Exec Directives, are used for executing a console command at compile time to keep things such as textures or sounds with the code, or for referencing other content packages (files) for organization.

Here is an example #exec used to load files with your code:

Code:
#exec *TYPE* IMPORT FILE=Location\SomeImport Name=*NAME*
As you can see, you have *TYPE* which you will need to replace with either:

- Texture
- Sound
- Font

It depends on what it is your importing, and I didn't list everything, but for more information on importing things from other packages you can look on. I only listed the 3 most common types of importing things, remember, you most likely won't need to deal with static meshes, and other things like that.

Also, remember that the Location\ is the folder inside your main project folder where you keep your imports. Your setup for your uscript files should be something like this:

Main Folder
- Classes
- Textures
- Sounds
- Fonts

But, you don't necessarily have to have textures, sounds, or font folders, not every class needs a texture or a font, its just whether your using it or not. But I would recommend putting things into seperate folders, named after the type of import it is. And also, remember to use file name extensions when importing, and I make sure you know which your importing, and the background on each import. You can check the links in the conclusion of this tutorial for more information about that.

Variables:
Variables are simple things that hold data in some way, shape or form, from text to numbers. For this tutorial we won't go into much more detail, as the types of variables should sum up many questions still floating around about variables.

Variables appear like this:

Code:
var [variable type] [variable name];
Types of Variables:

Variables come in many forms, from bools to strings, and even others, lets go over the different types of variables in Unreal Script:

- Integer
- Floating Point
- Boolean Value
- Byte
- String
- Name
- Enumeration
- Actor Reference
- Class Reference
- Struct
- Conditionals
- Vector

Note: There are many other types of variables, such as the Texture variable, but for this tutorial, I will not go into those, you can look them up on your own.

Integer:
Integer variables are variables that hold whole numbers, they can not be decimals, for example, 250 is an integer but 250.1 is not. Integer variables also take on things such as adding and subtracting to the number to increase or decrease it:

Code:
int++;
Will add to your integer, much like int = int + 1.

Code:
int--;
Will subtract from your number, much like int = int - 1.

Floating Point:
Float values are much like integers in that they hold numerical values, but these values can hold decimals, and whole numbers.

Float values though, can not interact with integers, you would have to convert all integers to floats, or all floats to integers if you wanted to obtain a answer back other then an integer. When trying to use things that involve both integers and floats you need to remember, you will always get an integer back, but it will not be the resulting number you wanted.

Note: int--; and int++; will not work with float values, you will need to do:

Code:
Float = Float + 1;
That would be the same thing as int++; but its the way you would do it with floats to make it work, as Float++; will just return your original float value.

Boolean Value:
Bools have been around for a shorter amount of time than other variable types. They are built around 2 words, true and false. Boolean values are quite simply the backbone of most classes, but it depends on peoples coding style.

You may often see boolean values as such:

Code:
var bool bVariable;
You will not need the b infront of the variable name, it is just a coding style, I believe the coding style is refeered to as hungarian, and was developed by a application developer for Microsoft(TM).

Byte:
A byte is much like an integer with limits on its value of between, 0 and 255. Bytes can be used to replace bools with doing such code as:

Code:
var byte bByte;

if ( bByte == 1 )
{
  Do Something...
}
Which would act the same was as say:

Code:
var bool bBool;

if ( bBool )
{
  Do Something...
}
You may notice bytes being used in reference to things such as Canvas, to define such things as color codes, which fall within that 0 to 255 range.

String:
A string is a set of numbers and letters, alphanumerical in other words, that can be set with the use of variable definition, by setting the variable somewhere in your class. Here is an example of some things you could do with strings:

Code:
var string String;
var string String2;
var string[25] String3;

String = "Uscript Tutorial";
String2 = "Beginners Guide";

// Combining:
String3 = String $ String2;

// Finding Left Most Character of String:
String3 = Left(String, 1);

// Finding Right Most Character of String:
String3 = Right(String, 1);

// Finding Number of Characters in String:
String3 = Len(String1);

// Display String in Caps:
String3 = Caps(String1);
As you can see already that I defined the strings out, but notice the brackets, with the number 25 in them. That simply signifies that the number of characters in that string can only be less then or equal to 25. The things next to the // are quotes, which are commonly used to make notations in your code. Also, remember that strings, when they are given a value, the value must be in quotes.

Name:
Name variables are hardly used, but they still serve a purpous. Strings and Names both hold alphanumerical values, and it can be easy to confuse names with strings, but remember names are not strings. A string can be modified dynamically, but a name is simply a label for an object.

Code:
var name Name;

Name = 'Uscript';
Notice how names use single quotes instead of double, there is not much else to say other then that you really will only use these in more complex developement situations, and even then your going to want to use strings rather then name variables.

Enumeration:
Enumerations are a value type that is a way of defining a type of variable that in turn could / can be one of a certain pre-defined set of values. Enumerations are often refered to as enum values, and they are not necessary since other types of variables can take their place. An example enumeration is as follows:

Code:
// State a New Enumeration
enum Name;
{
  Mark,
  Pat,
  Jeff
}

// Declare Variable of Type Name
var Name MyName;

// Declare a Value
MyName = Mark;
Simply put, enumerations can be confusing to some people so its best to take the little bit extra time and just use strings, intergers, bytes, bools, and name variable types other then enumerations, unless necessary.

Actor Reference:
Actor references are different from other variable types as they refer to an actual object found inside the game you are coding for. Actor References can be difficult for some people to understand right now, but I will atleast for educational purpouses show you what one looks like:

Code:
// Reference to Pawn
var Pawn Me;
As you can see, the actor in the game called Pawn is now in my class being refered to as Me. Actor references are more common in modifications of games, or cheats. Actor references can be done also like:

Code:
var Pawn Me, Target;
In that case, you would be referencing it twice, under different names. As you can see, we use commas to seperate, and a semicollon at the end, its more of a space saver, but you can do it just the same as:

Code:
var Pawn Me;
var Pawn Target;
Also, note that you can do actor references and keep what the actor was originally called the same thing.

Class Reference:
Class references are much like Actor references as they reference something found within the game. Class references unlike Actor references, don't reference an object in the game, but rather a class. Here is an example class reference:

Code:
// Declare Class Reference
var class<Actor> Reference;

// Define Reference
Reference = Class'Pawn';
Now, your wondering, woah... what is all that other stuff. Well, let me explain. You see the var class<Actor> bit, now to explain that, the word Actor is declared as the upper limit of this class reference, and that word Actor can be changed to anything you want to have as your upper limit. The part of defining the reference is probably another thing for question. Class references are given values by stating the word, "Class" followed with the class your referencing in single quotes.

Struct:
Strucs, also known as Structures, are a way of defining a new variable made up of other variables. Structs are very simliar to a class in its own way, but a class that only contains variables. An example struct is as follows:

Code:
// Define A New Struct
struct Person
{
  var string Name;
};

// Declare Variables for Person
var Person Me, You;

// Assign values to Variables in the Struct
Me.Name = "Mark";

// Declare Me Equal to You
Me = You;
As you can see, I only had 1 variable, but you could do other variables, its basically whatever you want. Also, notice how when I was assigning values I did "Me.Name", and not "Person.Name". Why? Simple, your defining the Person as different variables, essentially copying it. Your setting the values of the Person for 1 of those variables, and then at the end I declared that "Me" is equal to "You", getting rid of assigning values to the variable "you.

Conditional:
Conditionals are probably something people who have coded before know about, eventhough you may not recognize the name. Conditions in short are the things un uscript that use the if/else/then statements in programming. Conditionals are also the things that check for something using an operator and then perform something, or just don't complete it at all.

Here is the list of Operators that are used with conditionals:

==
Equal to...

!=
Not equal to...

<
Less than...

>
Greater than...

<=
Less than or equal to...

>=
Greater than or equal to...

~=
Approximately equal to...

You'll notice there are somethings that really won't work with a lot of things. Use common sense in determing which operators you can use with which type of variables.

Here is an example use of a conditional:

Code:
var bool bMe;
var int Age;

if ( bMe )
{
  // Code if bMe is True
}
else if ( !bMe)
{
  // Code if bMe is False
}
if ( Age <= 10 )
{
  // Code if Age is Less than Equal to 10
}
else if ( Age > 10 )
{
  // Code if Age is Greater than 10
}
Notice how I use If and Else statements, if the conditional fails in the case you had an If statement, and then if the thing wasn't true do something else, or just skip to the next operation (thing in the class to do). Remember, boolean and integer variables aren't the only things you can use with conditionals.

Vector:
Vectors are the last type of variable I will go over, I put it last for a reason, its not necessary for most of what people will be doing with Uscript. Vectors in short are X, Y and Z values. Working with vectors can be very complicated, so I suggest further reading.

Other Control Devices:
Control Devices are simply other things that affect the code in a class. The type of control devices other then "if" statements, such as the ones listed below, I will explain each of them, the 3 loop types and the switch statement. Switch statements can be more confusing then the type of loops, so you should use them only if you want to, more of an exploratory type thing. Loops have been known sometimes to be faster, and help code speed up in parts where it might tend to lag, remember doing a bunch of different things can be somewhat slow, but doing the same thing over and over can be much faster, remember Game's don't get bored by the code you give them to work with.

- For Loops
- Do Loops
- While Loops
- Switch Statements

For Loops:
For loops are conditions where a set of code is executed continuously until something is done to stop it, which can be anything you decide.

Here is an example For Loop:

Code:
// Set a Integer
var int i;

// Loop
for ( i=0; i < 2; i++)
{
  // Code Here As Long as I Less than 2
}
Basically I set a global integer as I. Then I have the loop. The loop initially sets up what I is equal to, remember you can name your integers whatever you want. The second bit is the case in which the loop keeps repeating until I = 2. And at the end it just adds onto the value of I, which I already explained earlier on.

Do Loops:
Do Loops are very similiar to For Loops in which they complete a certain code until a condition is met. In Do Loops you don't set an initial value for the variable, and you don't increase the value of the variable unless you add something into the loop. Here is an example Do Loop:

Code:
// Set a Integer
 var int i;

// Loop
do
{
  // Code to Do
  i++;
} until ( i == 2 );
Its very similiar to for loops as you can see, just this time the time in which the way the loop stops is at the end, which is the until ( i == 2 ) type. And as you can see, the increasement of the integer is inside the loop.

While Loops:
While loops are like Do loops, but they check initially for the value of an variable, and do something as long as the variable is within the operation of that loop's specification, and not until it is within. In short, as long as an integer is within the boundary of the operation it will complete the loop. Here is an example While loop:

Code:
// Set a Integer
var int i;

// Loop
while ( i < 2 )
{
  // Code Here
  i++;
}
As you can see, its very similiar, and shouldn't require much more explaination.

Switch Statements:
Switch statements are in short over complicated if statements that server a purpous gearter then your average if. Switch statements are used to go through certain sections of code depending on the value of a variable. Here is an example:

Code:
// Set a Integer
var int i;

// Switch
switch ( I )
{
  case 0:
    // Code
    break;
  case 1:
    // Code
    break;
}
As you can see, they are quite different then what you would need to do, but sometimes they are very helpful, they make lots of loops become 1 simple check. I would only recommend using switches once you further understand uscript, remember keep it simple. But since some of you want to know how to use switches, let me explain. You have your variable you want to use, which can be a integer, byte, string, or a float, its what you want it to be. You simply have the text "switch" followed by your variable's name inside parentheses. Inside the switch you have cases, where if the variable you selected is equal to whatever is put after the word "case" it does the block of code after the collon. Also, take notice of the break, that just signifies the end of a case. Some also are wondering possibly, what if the variable is not equal to any of those cases. Well, simply put you can add in a case and call its value of the variable "default" and it will automatically execute the code in that case if the variable is not equal to any of the other cases. I recommend using default cases only with "work in progress" classes, to have a working switch at all times, regardless of the variables values.

Side Note:
You have seen the I++; being used, and if you were paying attention before to what I++ means you will not need to read this, but it may help to remind you. With floats for example you can't use I++ so you have to do, I = I + 1. With a do loop for example you do:

Code:
// Set a Float
var float i;

// Loop
do
{
  // Code
  i = i + 1;
} until ( i == 2 );
I hope that helps to allow you to use more specific numbers, however the likelyhood of that happening is somewhat slim.

Functions:
Functions are the basis of classes, they are in a sense the heart of your class, and without them you wouldn't be able to do much, remember all these things we have been talking about with setting variables and such, need to be contained somehow. Functions are what contain your loops, your switches, and everything other then your variables, your class declaration, and your execs.

But your still wondering, what is a Function, well quite simply a function is a block of code, it is something you work off of to perform an action. Functions can use, or be passed along variables. Functions can also return values. But before a function is ever able to be used, it must be called, here is an example function, and the way to call it:

Code:
// Function
function Msg (string Message)
{
  if ( Me != None )
  {
    Me.ClientMessage(Message);
  }
}

// Calling
Msg("Text");
You will notice how I set up my function Msg to be one for making client messages, my example may not work for all games as it is derived from the code found in Americas Army. For calling the function, you simply would add the code like I put there in any other function that is automatically called, for example, PostRender, PawnRelated, Tick, or PostBeginPlay, all for just simple examples. Its recommended that you download either UTPT and view sources to .u files for good examples of functions, or even use the UnrealEd from one of your Unreal Engine based games, its found in the system folder.

You will also notice, that you can do things in functions with local variables, or variables that only have to do with the function your coding with the local variable called in. For an example on using it you could do:

Code:
// Function
function Example ()
{
  local int I;
  // Rest of code here...
}
It can be useful to have local variables and global variables, as sometimes you will only need local variables to get the job done.

Inheritance:
Inheritance relates to the original statements I made regarding the coding hierarchy present with Uscript classes. Well, Inheritance in relation to that simply means, that all classes under another inherit everything from the ones above them. Simply put, every bit of code from the top can be carried down to the classes below, all variables, functions, states, and default properties. You may think that your class your thinking of writing, or are currently writing is not inheriting anything, your wrong, unless ofcourse your class is at the top of the hierarchy, or nothing is above it.

Whenever you code a new class that follows in a hierarchy your simply adding to the continuing chain of code that is carried down from the top. Whenever you want to change code that is inherited from the top, you override the code using your class. Simply put, to override code inherited, you copy into your class, the name, parameters, and the return values associated with it, and write new ones. In Uscript, this ability is fantastic as in other types, you would have to copy ALL the code down, and make for a lot of repetitive things going on, but with Uscript you don't need to copy much at all, since its all down automatically.

States:
States are simply code that is executed only when the class is in that state. Example of states can be seen with for example the recovery state in Americas Army:

Code:
state RecoveryState
{
  function FindPath ()
  {
    MoveTarget = FindPathToward(MoveObject,True);
    if ( MoveTarget == None )
    {
      actionFailed = True;
      NotifyComplete();
    }
  }
  
  Sleep(0.1);
  FindPath();
  if ( MoveTarget == MoveObject )
  {
    goto ('Recover');
  }
  MoveToward(MoveTarget);
  goto ('Movement');
  MoveToward(MoveObject);
  Pickup(MoveObject).TakeItem(Pawn);
  NotifyComplete();
}
As you can see, it can be hard to follow, but at the same time if you take a look at it you can notice how a state acts, its almost like a switch statement, its own miniature class, and it's got its own functions. States can have more then functions though, they can have labels, labels are something that would look like this:

Code:
state Statename
{
  // Code Here
Begin:
  // Code Here
}
The code before "Begin:" would be executed later, but any code after "Begin:" would be exectuted immediately as the class enters that state, there are more to labels, so lets talk about it. Most of you will not be programming states, however you may get into it, and future research into states should be expected. But you just need to remember that states are words not indented (in most cases), and have a collon after them. You use the code:

Code:
goto Example;
That code is what you add inside a state, or any function for that matter in order to skip ahead and go directly to that label. In that example code "Example" was the name of the label being used.

Also, some more things to point out about states, if you had a functiong going on outside of the state and wanted to make it change for when your class is in that state, you would simply remake that function inside your state and change it to your liking. And ontop of that, you can even have some functions not be active when your class is in that state, doing so is made simple with the ignore statement. Here is an example:

Code:
ignore Timer;
In that example, the function Timer() would be ignored. In the ignore statement, you do not need to add the extra values like when your calling a function, you simply put the name of the function in there, and to have more then 1 function ignored, you use commas, just like having multiple variables on 1 line.

Default Properties:
Default properties is the conclusion to your classes, its also used to define your variables default values, which can be overwritten throughout your class in functions. Default Properties appear as so:

Code:
defaultproperties
{
  variable1=value1
  variable2=value2
}
There is no need to necessarily use semicollans in default properties, but you can if you want. As you can see I have just 2 'fake' variables defined. Please remember though, that any type of variable you have can be defined here, except for a few, such as enumerations, along with actor and class references, since you shouldn't need to redefine them.

Conclusion:
This tutorial has taken some time, hours actually to fully write, and probably not as long to read, but still a lengthy time, I hope you enjoyed the reading, and I hope I helped to explain a lot of things for you.


Spam Killer

Back To Top
2005 Sniper's Paradise
All logos and trademarks are properties of their respective owners.
Unreal™ is a registered trademark of Epic Games Inc.
Privacy Policy
Website by Softly
Powered by RUSH