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;
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*
- 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];
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++;
Code:
int--;
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;
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;
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... }
Code:
var bool bBool; if ( bBool ) { Do Something... }
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);
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';
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;
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;
Code:
var Pawn Me, Target;
Code:
var Pawn Me; var Pawn Target;
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';
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;
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 }
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 }
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 );
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++; }
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; }
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 );
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 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... }
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(); }
Code:
state Statename { // Code Here Begin: // Code Here }
Code:
goto Example;
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;
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 }
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.
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