
In the
last part, we learned what a game on UnrealEngine is, learned how to build geometry, and arrange actors. Included with the games on UnrealEngine and even UnrealRuntime are quite a lot of standard actors, such as scenery, all sorts of triggers, weapons and other useful things. With their proper use, you can make a variety of interesting levels for games, however, this will not give complete creative freedom. The game will have a standard start, standard rules of winning and losing, it will not even be possible to enter an extra control button. Here it is time to get acquainted with UnrealScript. Immediately make a reservation if you are an UnrealScript guru, then most likely you will not be interested. The rest is welcome under cat.
Basics of the language
UnrealScript is an object oriented language, syntactically similar to Java. The language does not support multiple inheritance, interfaces (it supports the third version). It supports operator overloading, but not methods. I see no point in rewriting the entire language syntax. I hope that you are familiar with any programming language. Therefore, I will describe the main points.
Data types
UnrealScript contains the following simple data types:
- bool - the type can be true or false
- byte - without a signed integer with a range of values ββfrom 0 to 255
- int - signed 32-bit integer with a range of values βββ2 147 483 648 to 2 147 483 647
- float - 32 bit floating point number
- string - a string of unciode characters
- enum is an enumeration type, contains a set of named constants; the first constant is 0, the second is 1, and so on.
- name - the type indicating the name of something, for example the name of a state, static mix or class
There are also reference data types:
- Object - a link to an object, namely a link, that is, several variables can point simultaneously to one object
- Actor - reference to an object of class Actor or inherited from it
- Class - the link indicates the type of the class or the class inherited from it.
Composite types:
- Struct - structure consisting of a fixed number of fields
- Array - static array
Declaring classes, variables, and functions
Class declaration
class declaration looks like this:
class classname extends parentclass [modifiers] ;
Where
classname
name of the class being declared,
parentclass
name of the parent class,
modifiers
modifiers.
Basic modifiers:
- Config (name) - the name of the .ini file where the class will save its settings
- Native - the class is partially implemented in C ++ code
- NativeReplication - replication is made from C ++ code
other modifiers can be found here
http://wiki.beyondunreal.com/Class#ModifiersVariables (properties)
For variable declarations, there are two keywords,
var
and
local
. The first is used to declare class properties and is described immediately after the class declaration, the second is used to specify local variables of methods.
The syntax for using these keywords is as follows.
var [modifiers] type variablename1, variablename2, ...;
local type variablename1, variablename2, ...;
Where
type
is the type of a variable,
variablename
respectively,
variablename
names.
modifiers
modifiers affecting the behavior of a variable.
list of basic modifiers- Const - the variable is constant and cannot be changed
- Private - private class variable, available only inside the class.
- Protected - protected class variable, available in both class and its subclasses
- Public - public variable, available from any class (by default all public variables)
- Config - the variable value can be saved and retrieved from the ini file
There are a lot of modifiers and for different versions of the engine they differ with their list can be found at
http://wiki.beyondunreal.com/Variables#Modifiers Variable initialization
There is a special structure for initializing variables, which is usually described at the end of the class.
defaultproperties { varname=value arrayname(index)=value structname={( member1=value1, member2=value2 )} }
Accordingly, varname, arrayname, structname are the names of variables, arrays and structures. Important semicolon, in this block is not put at the end, a line break is used.
')
Functions (methods)
Functions are declared in the following way:
[modifiers] function [returntype] functionname ( [parameter] )
Where
modifiers
modifiers affect the behavior of the function,
returntype
type returned by the function,
functionname
function,
parameter
incoming (and in some cases outgoing) parameters of the function.
list of basic modifiers- Private - private variable function, available only inside the class.
- Protected - a protected function, available in both class and its subclasses
- Public - public function, available from any class (by default all variables are Public)
- Static is a static function, it can be called without creating an instance of the class, since the function is called without creating an instance of the class, it does not have a reference to its self object and cannot be replicated
- Final - a function with this modifier cannot be redefined in subclasses
- Exec - the function can be called from the console engine.
- Native function implemented in C ++ code
There are a lot of modifiers and for different versions of the engine they differ with their list can be found at
http://wiki.beyondunreal.com/Functions#Modifiers The function parameters are described as follows:
[parameter modifiers] type parametername[arraysize]
parameter modifiers
modifiers that affect the behavior of incoming parameters, the type of the parameter being passed,
parametername
name of the parameter,
arraysize
, if an array is passed, then specify its size.
Modifiers for the parameters are much smaller, only two main ones:
- optional - indicates that this parameter is optional and allows you to set a default value.
- out - indicates that the parameter value can be changed inside the function, it is very close to the analogue of passing a value by reference, but this is not exactly it.
States (states) and latent functions
If everything that is described before is found in all OOP languages, but the states are the UnrealScript feature. In game mechanics, a lot of things are built on states. For example, your character may be in several states: stand, sit, lie down. In each state, some options for action will be different, for example, the speed of movement, the ability to jump, and so on. There are several options to realize this, for example, when a jump team is in control of the current state of the player and whether he can do it, and only then decide whether he will jump, or just get up to his full height. UnrealScript offers us another option. We describe the states in which our class can be located and in these states we describe functions that will be called instead of normal functions if the player is in this state. The state is described as:
[modifiers] state statename [extends parentstate] { ... };
modifiers
- modifiers, this time from only two:
- Auto - an instance of the class automatically goes into this state when created.
- Simulated - says that the state can be executed on the client during replication
statename
- the name of the state
parentstate
β indicates that it expands another state.
Hidden text class example extends actors; function test(){ log("No state"); } state StateOne { function test(){ log("In state one"); } } state StateTwo { function test(){ log("In state Two"); } } state StateThree { function test(){ log("In state Three"); } }
The point is that by calling the
test
method we get the output to the console of the current state of the object depending. It's pretty simple. We can also describe the state of the set of commands that is executed when we go into it
state StateOne { function test(){ log("In state one"); } log("Going into StateOne"); }
As a result, when the object goes to the StateOne state, we get to see in the console the string Going into StateOne.
The condition is a very flexible thing, but you need to work with them carefully so as not to get confused when and what happens.
Also in UnrealScript there is such a concept - latent functions. Latent functions are functions that take a certain amount of playing time to complete. For example, the function Sleep, which pauses the execution of commands for a certain time. It is possible to cause latent functions only in states. While the state is unlikely to be useful to you, but they must be remembered, especially considering that they are inherited.
Working with scripts
As you can see, the UnrealScript language is similar to other OOP, there is nothing supernatural in it. There are many nuances but in general, nothing extraordinary, and they are not afraid of it. Therefore, let us now understand how to create and compile scripts, there are two ways one of them through UnrealEd, but in my opinion this is not the most convenient option, so let's learn how to do it with
ucc .
A little bit about what packages are
UnrealEngine stores all of its resources (textures, models, sounds, music (not always), scripts, etc.) in packages. Packages may have different utx / umx / usx / u extensions depending on the type, however, these are all the same packages - the separation is for convenience. From this comes the first incident, never make two packages with the same name, but differing in extension, the engine will normally see only one of them. Also, there is a second point - anything can be stored in the package (that is, there can be sounds, textures and scripts at the same time).
Create your first package
So, in order to create your script package with ucc, we need to create a directory at the root of the game (not to confuse the System directory of the game, root to the directory above) and call it the name of a future package, for example TestPack. Now in this directory you need to create a subdirectory Classe and already there to create our scripts. One of the conditions of the engine is that one file should contain a description of one class, its name should be like the name of the class being described and have the extension
.uc
Now let's create a file called test.uc and contents:
class Test extends Commandlet; event int Main( string Parms ){ log("Hello World!"); return 0; }
What is the CommandletCommandlet
is a special class that allows you to run yourself through the ucc shell. The engine itself contains quite a few utilities that are made in the form of commandlets, And with the above class, we made our own, which brings everyone's favorite Hello World to the console.
Now we need to build our package. To do this, go to the System directory and find the main ini file there, it will also be named as the .exe file of the selected game, that is, UT2004 is UT2004.ini, and for UnrealEngine2Runtime it is UE2Runtime.ini We tear it off and look for the Editor section .EditorEngine in this section we find the lines of the form EditPackages = ... and append another line EditPackages = TestPack, close the file. We execute the command ucc make in the console in the game directory, and if everything is done correctly, we will get an output like this:
ucc make D:\Upload\UnrealEngine2Runtime\System>ucc make --------------------Core - Release-------------------- --------------------Engine - Release-------------------- --------------------Fire - Release-------------------- --------------------Editor - Release-------------------- --------------------UnrealEd - Release-------------------- --------------------IpDrv - Release-------------------- --------------------UWeb - Release-------------------- --------------------GamePlay - Release-------------------- --------------------GUI - Release-------------------- --------------------Runtime - Release-------------------- --------------------RTInterface - Release-------------------- --------------------TestPack - Release-------------------- Analyzing... Parsing test Compiling test Importing Defaults for test Success - 0 error(s), 0 warning(s)
Now we can execute the
ucc TestPack.Test
and see the work of our commaidlet, namely the HelloWorld output! .. Congratulations on the first package created.
Little tricks
ucc make
will not rebuild the already created packages, even if you change the source, in order for it to rebuild the package, delete the already built one. For convenience, I am writing simple .bat files that reassemble the packages I need.
Any text editor can be a code editor, but there is also
WOTgreal that I liked very much in this regard. It has syntax highlighting, outputting the source tree by packages and inherited classes, and other usefulness.
You can also get most of the game class code, run UnrealEd, go to ActorBrowser and select
File-> Export All Script . At the end of the process in the root of the game you will see the catalogs for packages containing scripts already used by the game. Only those packages that have been loaded into the editor will be displayed, if something is not available, try downloading the required package separately and repeating the process. You should not try to rebuild the game packages, most of them contain links to Native code, so they will not be able to get together.
Hierarchy and frequently used classes
It's time to talk about the class hierarchy in UnrealEngine. The root class is the Object class. All the main classes are inherited from it. Object - contains a description of the basic structures of the engine and some system methods. You should not try to rebuild the game packages, most of them contain links to Native code, so they will not be able to get together. The second most important is the class Actor. It describes the basic capabilities of all objects that can be added to the level - their properties, movement, physics, replication, and so on. All other objects that are added to the level are inherited from this class. Below I write the purpose of the main classes with which you will come across.
Actor
Base class for all other game objects. It describes the types of physics, lighting, network roles and other things, the class should definitely be studied at least diagonally. It declares the PreBeginPlay and PostBeginPlay functions, which are then used by all subclasses for initialization.
Gameinfo
GameInfo - the class is responsible for the rules of the game, its beginning and end, sets the class management for the players, and also controls their connection and disconnection from the game. Functions that are worth exploring for
PreLogin ,
Loging ,
PostLogin ,
Logout . A also variables
HUDType - sets the class HUDa;
PlayerControllerClassName - sets the class PlayerController;
bDelayedStart - whether the game starts immediately or after any condition.
Pawn
Pawn (pawns) - class which describes the actors who take direct part in the game, such as bots, npc, players. The class describes a lot of functions, though, almost all are applicable to 3D action. If you make a new character, then you will inherit it from this class. I would pay particular attention to the
bSpecialCalcView variable, which indicates who is responsible for displaying the camera's Pawn class or the PlayerController class. If set to
true
, the
SpecialCalcView function of your class will be called where you can set the camera position.
Playercontroller
PlayerController is the class that is responsible for handling the control; all button presses, mouse movement, joystick position, etc., are flocked here. Particular attention should be paid to the
PlayerTick function
, which is called every tick, and
PlayerCalcView which is responsible for the position of the camera if Pawn is not responsible for it. You should also keep in mind that many events first come here, and only then can be transferred to Pawn, for example, NotifyBump, NotifyHitWall. For the management of bots is responsible class AIController.
Hud
HUD - responsible for drawing HUDa. The most interesting feature is
PostRender , which displays the entire HUD.
Conclusion
Here we briefly and met with UnrealScript. The language is very powerful, but simple. In general, you are ready to create the subclasses you need and start making your game, or presentation, or your own way managing the smart home as
stdamit did in the
Unreal LED article
, or load management from Unreal Tournament . The main objective of this article is to show that UnrealScrip is not scary, and, I hope, I coped with it.
Links for information
http://wiki.beyondunreal.com/UnrealScript. Reference GuideUnrealScript. Reference Guide. Part twoPs. In the next part we will talk about replication, what it is, what it is for and how to use it.