Mostly Harmless

Difference between revisions of "Legacy:UnrealScript For CPlusPlus Programmers"

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
(*revert)
 
(Misread.)
 
(One intermediate revision by the same user not shown)
(No difference)

Latest revision as of 15:17, 6 January 2010

UnrealScript was expressly designed to give C++ and Java programmers a warm feeling of familiarity. If anything Unreal Script is more similar to Java than C++ (see UnrealScript For Java Programmers). The main differences between C++ and UnrealScript are summarised below.

Just like C++, Unreal Script is an object-oriented language. The notions of classes, inheritence, objects, and overriding (redefining in a subclass) methods apply. The main differences in the behaviour of objects are listed below.

  • Multiple inheritance is not supported. A class may only have a single superclass.
  • All methods are virtual.
  • All objects are created on the heap and handled only via references. So every variable that points to an instance of an object is a pointer to that object (a reference if you perfer).
  • There is no delete operator. Deleting objects is automatically handled by a garbage collector; objects of class actor and below must be manually removed from the Actor list by calling their Destroy method before they are discarded. Objects are only garbage collected when the total number of references to them reaches zero.
  • Regular methods cannot be overloaded although operators can. However, it is possible to make any or all of the parameter(s) of a method optional.
  • Unlike C++, UnrealScript is a case-insensitive language ("Function" equals "function", "LOG" equals "log", and so on).

Other differences between UnrealScript and C++ are:

  • Structures created with the struct keyword and classes are fundamentally different concepts in UnrealScript. Structures are always handled by value. Classes are always handled by reference.
  • All variables in UnrealScript must be explicitly scoped:
    • Instance variables are available from anywhere within the object after their declaration (ie. object scope) and are declared with the var keyword
    • Local variables are available from anywhere within the function in which they're declared, after their declaration, and are preceded with the local keyword
  • Templates do not exist.
  • There's no main function that is automatically executed when your mod is started; instead, the engine instantiates a (specific) class from your mod package (a Mutator subclass for mutators, or a GameInfo (UT) subclass for game type mods, as specified by you in your mod's INT File) and executes its "event" methods as they apply.
  • Related classes (and other resources) are stored in packages. Classes contained in a package are dynamically bound to the class tree when the package is loaded.

The operators available in UnrealScript are similar to the ones of C++. Global Function available in all subclasses of Object (that is, everywhere) provide generic functionality for handling UnrealScript's built-in data types.

UnrealScript source code needs to be compiled before it can be used in the game. The compiler takes source code and any other resources referenced from your source code (like textures and sounds) and creates a code package.

Related Topics[edit]

Note[edit]

The name of this page is a result of the limitations of the UseModWiki script. Both [[Legacy:UnrealScript For CPlusPlus Programmers|UnrealScript for CPlusPlus Programmers]] and [[Legacy:UnrealScript For C++ Programmers|UnrealScript for C++ Programmers]] are valid links to it.

Related Topics[edit]

Comments[edit]

Mychaeel: Just some notes so far. Feel free to rearrange and elaborate.

Heinz57: I edited the previous version of this page to elaborate on the scope of variables and functions. Correct me if I'm wrong, but, as far as I can tell it is possible to have variables and functions whose scope is global in UnrealScript. See the UnrealScript Language Reference under Variables :-). Also, in terms of global scope, keep in mind that I used the term Object, rather than Class in describing scope, because a global variable can be declared within a struct that same as within a Class.

Wormbo: Actually you can declare functions and class variables as private or protected if you don't want them to be public.

Heinz57: My mistake. I was actually just editing this page to make the correction, and got a conflict, because of your edit. Thanks Wormbo! Also, another thing I shouldn't have spoke so soon about: The term Global can be interpreted a couple of ways (in general OO terms): a function or variable could be said to be Global to a particular Object (the one it was declared in), or to all objects. It appears that, according to the Global Function page, in UnrealScript Global is thought of as the latter. Mychaeel had previously hinted that functions and variables could not be global. Could you elaborate?

Mychaeel: On UnrealScript Language Reference/Introduction Tim Sweeney himself says "There are no system- wide global functions or variables." That is exactly what I am referring to – any variable you declare in UnrealScript is actually just a property of a certain object. The only way to "fake" global variables is instantiating an object and making it more or less globally known (like a map's LevelInfo that's known to all actors via the "Level" reference declared in Actor).

I don't understand the sentence "Variables must be declared for what they are" in the text above, by the way. I can only guess its meaning from the examples given directly following it (so it basically boils down to "Variables are explicitly scoped," but in fact the use of "local" or "var" in UnrealScript variable declarations is completely redundant with the place of a variable declaration).

Foxpaw: I believe in C++ is is possible to make a pointer that is just a pointer.. the type of thing is going to point to does not need to be declared and it can change if the programming dictates it. (I think that is the case. It has been YEARS since I've even looked at a program in C++, but I used to use it a lot.) PS: I also changed the bit about optional variables.. any parameter can be optional, not just the last ones. To opt out of an optional variable while setting the mandatory ones just put the two commas together, like AFunction( Other,, Instigator,,, X ) In this example, three total parameters have been skipped.

Mychaeel: So the following would compile?:

function MyOptionalParamTest(int iFirst, optional int iSecond, int iThird);
//...
MyOptionalParamTest(1, , 3);

You're right that in C++ you can have an untyped pointer; but in UnrealScript you strictly speaking don't even have the notion of a pointer at all, merely object references (which certainly are implemented as pointers internally). The closest equivalent to an untyped pointer in UnrealScript would probably be an Object reference since Object is among the ancestors of any UnrealScript class. (Then you can typecast that reference to whatever you like, as you could with a C++ pointer.)

Foxpaw: I expected it would, after testing it under UT2003 it doesn't seem that it will. I could have sworn that I freely intermixed optional and regular variables in UT though.