An introduction to UnrealScript

Download Report

Transcript An introduction to UnrealScript

UnrealScript
An Introduction
Copyright © 2015 Curt Hill
Motivation
• Up to now we have created levels
• Although we may animate things
there are no other pawns
• A pawn is either:
– A player
– An AI controlled agent
• To do these we now need to delve
into UnrealScript
• This is a complicated system
Copyright © 2015 Curt Hill
General Characteristics
• Text based script
• May be edited with Notepad++
– There is also a plugin for Visual Studio
• It is called a script but is compiled
– The Unreal Front End contains the
compiler
– This is done outside of the general UDK
interface
– .UC is extension of UnrealScript
– .U is extension of compiler output
Copyright © 2015 Curt Hill
Engine
• The Unreal engine knows about two
kinds of classes
• Native
– Built into the engine or from libraries
– Always in C++ or Assembly
• Non-native
– Always UnrealScript
Copyright © 2015 Curt Hill
Language Characteristics
• Fully object oriented
– Derivation is generally required
– The engine knows about certain
classes, so it may also use any
derivations of these classes
– Everything is thus class based
• Similar to C++ and Java
– It is not C++ or Java
– Based on but different
• Unlike either, is not case sensitive
– Reserved words and variable names in
either or mixed
case
Copyright © 2015 Curt Hill
C++
• C++ is a hybrid language
• It supports any non-object program
– It is upward compatible with C
• It also supports objects, inheritance
and polymorphism
• It supports programs with any
combination of these characteristics
Copyright © 2015 Curt Hill
UnrealScript is not hybrid
• More fully OO than C++
– Similar to Java in that respect
• Does not support global variables or
global functions
– Properties, methods and parameters
only
• It appears that there is only a single
inheritance hierarchy with Object
the base
– Java has only one
– C++ may have many
Copyright © 2015 Curt Hill
Terminology
• Peculiar to UnrealScript
• Reverts back to some of the
terminology of C++
• There are no properties
– They are called instance variables
• There are no methods
– They are called member functions
Copyright © 2015 Curt Hill
Headers
•
•
•
•
Like Java there are no headers
Everything is defined in a .uc file
No imports either
Those items known have been
processed in the order dictated by
the UDKEngine.ini file
Copyright © 2015 Curt Hill
Reserved Words
• UnrealScript has a ton of them
– More than 180
• Much larger than we see in Java or
C++
• This is because this is a specialized
language
Copyright © 2015 Curt Hill
Comments
• Uses both of C++ comments:
• Multiline: /* */
• Single line: //
Copyright © 2015 Curt Hill
Primitive Types
• int – 32 bit
• float – 32 bit
– Constants must have a digit prior to
decimal point – not .5
• bool – one byte
• byte – one byte, 0-255
• enum – one byte
– May be cast into a byte
– Do not use as function parameter
Copyright © 2015 Curt Hill
More Types
• string – anything enclosed in double
quotes
– A primitive not an object
– We will see some string functions later
• name
– Represents a object or class name
– Constant once item is created
– Constant names are in apostrophes
• struct – similar to C style struct
Copyright © 2015 Curt Hill
Predefined Structs
• Several exist
• Vector – X, Y, Z represents a point or
direction
• Plane – Contains a W, X, Y, Z
• Color – An RGB value
• Among others
Copyright © 2015 Curt Hill
Arrays
• One dimensional and static only
• Denoted by a bracketed constant
• Thus these look very much like C++
static arrays
• There is a dynamic array as well
– Which we might or might not get to
Copyright © 2015 Curt Hill
Objects
• Similar to Java declaration without
visibility
• Class name must match filename
• Does not have enclosing braces
since entire file is the class :
class CurtPawn extends UTPawn;
• No contructor
– Uses a default properties section
instead
– Slightly different syntax
Copyright © 2015 Curt Hill
Order
• In a class and function definition
order is important
• Class declaration must be the first
thing in file
– Except for blank lines and comments
• All of the instance variables must be
declared first in the class
• All the member functions next
• The default properties last
Copyright © 2015 Curt Hill
Member function
• Most similar to syntax and semantics
to C style functions
– Not C++ and Java methods
• Functions must start with reserved
word function
– Possibly prefixed with modifiers
• All local variables must be declared
first
• Followed by all executable
statements
– Terminated by semicolons
Copyright © 2015 Curt Hill
Function Result
• There is no void type
• A void function merely leaves off the
return type
• A return without a value may
terminate a function or be left off
Copyright © 2015 Curt Hill
Default Properties
• Last part of class declaration
• Replaces the C++ or Java
constructor
– Unlike a constructor, no parameters
are allowed
• Starts with defaultproperties
reserved word
• Followed by compound statement
• The compound statement contains
simple assignment statements
Copyright © 2015 Curt Hill
More
• This contains simple assignments on
instance variables
–
–
–
–
–
Variable=Literal
Semicolons are optional
No function calls
No calculations
No flow of control
• A sub-object gets a begin end
construct to initialize its instance
variables
Copyright © 2015 Curt Hill
Begin Object End Object
• The initialization of a sub-object
uses the following syntax:
Begin Object
Name=objectname
Class=classname
subobject assignments …
End Object
• We identify the sub-object with name
and/or class
Copyright © 2015 Curt Hill
Variable
declaration
Prefixed by a keyword
•
• Instance variables are declared
after the class declaration
– Must be prefixed by var
• Member functions may use
automatic variables
– Must be prefixed by local
• There may also be one of several
specifiers or metadata
– We may get time to see some of these
• Not needed for parameters
Copyright © 2015 Curt Hill
Example
• We will now look at two screens that
have a valid class definition
• Valid in this case does not mean
useful
Copyright © 2015 Curt Hill
Example First Half
class CurtJunkPawn extends UTPawn;
var int trash;
function int junk(int morejunk){
local Float f;
f = 0.;
trash = trash * 2 / f;
trash = 0;
return 2;
}
Copyright © 2015 Curt Hill
Example 2 Second Half
// Rest of CurtJunkPawn
Defaultproperties {
Begin Object
Name =
WPawnSkeletalMeshComponent
bOwnerNoSee=false
End Object
trash = 0
Name="CurtJunkPawn"
}
Copyright © 2015 Curt Hill
Assignments
• The C family style assignment is
used
variable = expr ;
• There are a larger variety of
operators than most langauges
• When it looks like a C family
operator is likely similar in:
– Precedence
– Arity
Copyright © 2015 Curt Hill
Familiar Operators
• ++ -• + - (unary) !
• */%
– Modulo has less precedence than *
•
•
•
•
•
•
+>> <<
< == <= > >= !=
&|^
&& || ^^
= += -= *= /= Copyright © 2015 Curt Hill
Operators
• These operators can be applied to
more than simple primitives
• For example the Vector may be used
with numeric operators
• There are a variety of other
operators not present in the C family
• Many of these we will not need but
here is a sampling
Copyright © 2015 Curt Hill
Other Operators
• ** exponentiation
• $ @ string concatenation
– Also $=
• Dot – Matrix dot product
• Cross – Matrix cross product
• new – similar to C++ and Java new
but with many more options
Copyright © 2015 Curt Hill
Class Declarations Again
• Following the extends and ancestral
class and preceding the semicolon
we can apply several modifiers
• No commas or semicolons separate
– The single semicolon terminates the
header
•
•
•
•
Placeable and noplaceable
ClassGroup
Implements
There are others
as well
Copyright © 2015 Curt Hill
placeable
• Allows this class to be created by
the editor
• Noplaceable is the default
• This carries on to all descendants
• The class must usually be a
descendant of actor
• This modifier has no parameters
Copyright © 2015 Curt Hill
ClassGroup
• Makes this class visible in the
Browser and the Actor tab
• This is followed by one or more
names
– The names do not have to be enclosed
in quotes
– The order given is the hierarchy shown
in that tab
• Example: ClassGroup(Curts,Junks)
Copyright © 2015 Curt Hill
Implements
• Specifies an interface
• Interfaces are similar to Java
• In order to not be abstract the class
must define all the member
functions given by the interface
Copyright © 2015 Curt Hill
Variable Declarations
Again
• There are many more options that
can be applied to variable
declarations
• ()
• const
• editconst
Copyright © 2015 Curt Hill
()
• Empty parentheses behind the
variable publishes this variable
var () int MyInteger;
• This means that they can be
examined in the editior
• Without means only usable in
UnrealScript
• Putting something in the
parentheses is the display name
within the editor
var (MyCategory) bool myBool;
Copyright © 2015 Curt Hill
Consts
• The const attribute means this
variable may only be changed in the
defaultproperties section
– Any other assignment is in error
• The editconst attribute makes this
unchangeable in the properties
dialog
– Editconst does not force const
Copyright © 2015 Curt Hill
Context
• Unlike many languages a type and
variable may have the same name
• Thus:
var Pawn Pawn;
is legal
• The compiler tells the difference
because of context
Copyright © 2015 Curt Hill
Metadata
• We can give additional information
to the compiler with metadata
• Metadata is enclosed in angle
brackets <>
• Form is <Name1=Value1,…>
• Example:
var float MyVar<DisplayName=
My Float Variable>;
• ToolTip and DisplayName are two
common metadata items
Copyright © 2015 Curt Hill
Constants
• We may use a constant with the
const keyword
• Form:
const name = value;
• Where value is a literal value
• This may occur anywhere a
statement can occur
– Unlike a declaration
• No type is needed
– Type is inferred
Copyright © 2015 Curt Hill
Objects
• Declaring an object (the instance of
a class) is just like a primitive
• Like in Java this is actually a handle
to an object, not the object itself
• To initialize use new
• No need to dispose, garbage
collector will clean up
• The equivalent of null is reserved
word none in UnrealScript
• The reserved word self is
equivalent to this in Java and C++
Copyright © 2015 Curt Hill
Objects again
• Like Java the dot separates the
handle from the instance variable or
method
• No exception for dereferencing a
null pointer
Copyright © 2015 Curt Hill
Type Casts
• Like C many casts are implicit
– You need to do nothing
• If you need to explicitly cast the form
is the same as the C++ new style
cast
– Typename as a function with value in
parentheses
• Example:
X = String(variable);
• Not all casts are supported
Copyright © 2015 Curt Hill
Flow of Control
• Much similarity to C family flow
• If – like Java must have bool as the
condition
– Else is optional
– Only one statement follows
• For – same as C
– No variable declaration
• While – also same as Java
• Switch case – same a C
• Do until – opposite exit condition of
Copyright © 2015 Curt Hill
do while
Foreach
• The iteration for of Java has a
different twist
– Not quite as general
• The form is:
foreach(iterator)
• This will give each item in a
collection
Copyright © 2015 Curt Hill
Other Flow
• The dreaded goto is allowed:
goto expr;
– Where expr is a statement label
• Assert ( bool ) acts like the assert of
C++
• Switch needs break but loops can
also use
– There is also a continue
Copyright © 2015 Curt Hill
Proprocessor
• In UDK 3 a C style preprocessor was
introduced
• Instead of the octothorp (#) they use
the grave accent (`)
– Below ~ left of 1
• Most of the C preprocessor is
present
– Define
– If else endif
– include
Copyright © 2015 Curt Hill
States
• A Finite State Machine is commonly
used to implement pawn behavior
– We saw this in Minecraft animation
– Here it is more for AI
• We will have to see the state thing in
a subsequent presentation
Copyright © 2015 Curt Hill
Events
• In most languages an event is a
designated (void) function
• In UnrealScript the event reserved
word replaces the function reserved
word
• We do not implement new events but
instead override our base class
events
• We execute the ancestral events
with the reserved word super and
then the event name
Copyright © 2015 Curt Hill
Disclaimer
• You may need to examine various
classes that are in the
Development\Src directory
• You will find lots of other things in
some of these
• This may include cpptext{…}
– This is real C++ that is inserted into
UnrealScript
– Might include C style preprocessor
• You may find other things as well
– This is not complete coverage
Copyright © 2015 Curt Hill
Unreal Frontend
• This is the program that will compile
the UnrealScript files
• It may also launch the editor or the
game
• Here we are interested in the
compiler
Copyright © 2015 Curt Hill
Compilation
• The process is external to the
editors
• Otherewise the play buttons would
be a problem
– The editors actually use the engine for
display
• We instead invoke the Unreal
Frontend
• Error messages show up in its
output
Copyright © 2015 Curt Hill
Compile Scripts
Copyright © 2015 Curt Hill
Error/Warning Output
Copyright © 2015 Curt Hill
Errors
• Errors are in red
– The above was an undefined variable
• Warnings are in orange
– Unused variable is one such warning
• The error messages do not always
give a clear message
• They do indicate the file and line that
the error was detected on
Copyright © 2015 Curt Hill
Concurrent Operation
• The front end can start the editor
• The compiler generally can compile
scripts when the editor is running
• It cannot delete the replaced script
• Therefore you need to exit the editor
to recompile properly
Copyright © 2015 Curt Hill
Finally
• This is the quick overview of
UnrealScript
– Or not so quick
• Since we all have background in
other OO languages it might even
make sense
Copyright © 2015 Curt Hill