Note that you always manipulate with references to objects but not with the objects themselves. That is why when you assign one object to another, it means that you simply copy the reference. Once the object is destroyed, its references aren't functional anymore.

Destructors and delete

If a class instance is no longer needed and it's time for it to pass away, it should be properly freed. That is why a destructor is called before the occupied memory is marked as free. Here is how a destructor can be defined:

Operator Overloading

Operator overloading allows operators to have different implementations depending on their arguments. You can provide your own operator to a class by overloading the built-in operator to perform some specific computation when the operator is used on objects of that class.

The name of an overloaded operator is operatorx, where x is the operator, available for overloading in UnigineScript:

operator+

operator-

operator*

operator/

operator%

operator<

operator>

operator==

operator<=

operator>=

operator!=

__set__ (for [])

__get__ (for [])

The following example represents overloading of the operator+, operator- and operator* for the class Vec3:

Keywords

this Keyword

Sometimes it is important to reference the object, for which a method is invoked. In such a situation, this keyword is helpful; it is reference to an object itself, available for member functions of the class.

super Keyword

Base class functions can be overridden in the derived class. However, you can call such functions from the derived class by using the super keyword.
For example, the foo() function is overriden in the Bar class, so, the foo() function of the Foo class can be called using super.

Accessing the Overridden Base Class Function

If in the inherited class a method is overridden, you can still access this member function as it is implemented in the base class using the following syntax:
<base class name>::<overridden function name>

Creating Abstract Methods in a Base Class

You can create an abstract method in the base class and implement it in the derived one. However, in the base class method an error or, if necessary, an exception should be thrown. It is used to indicate that a method implementation is lacking in the derived class.

Class Based Branching

The fast class based branching can be implemented by using the switch-case conditional statement.

If you have several classes defined in the script and their instances require to be processed differently, you can use the classid() function as a constant for the case within the switch to check if the passed value is an instance of the particular class.

In both of these cases, class inheritance is performed in the same way (see an example below).

Inheritance from Engine Classes

Here is an example how to create
MyObjectMesh class which inherits from the base engine
ObjectMeshStatic class.

Objects of the derived class can call methods of base class directly.

When passing object of the derived class to function that takes an object of the base class, inherited class cannot be automatically converted to the base class. Upcasting should be done explicitly via accessing
extern member of the derived class.

To downcast the instance of base class to your custom derived class, use
cast() function. This function is created automatically for all inherited classes and needs not to be implemented manually.

Function Chaining

In UnigineScript, function chaining enables to call a chain of functions in one statement.
The return value of the first function is used to call the second function.
Such approach is useful when there is no need to store intermediate objects. For example:

Source code (UnigineScript)

mesh.getBoundBox().getMin();

First the
mesh.getBoundBox() is executed and an instance of the BoundBox class is returned.
On this returned value the getMin() member function is called.

Notice

The type of the return variable is equal to the return type of the last executed function.

Function chaining can also be used when calling functions of user-defined classes. For example: