UDN
Search public documentation:

UnrealScriptInterfaces
日本語訳
中国翻译
한국어

Interested in the Unreal Engine?
Visit the Unreal Technology site.

Looking for jobs and company info?
Check out the Epic games site.

Questions about support via UDN?
Contact the UDN Staff

UE3 Home > UnrealScript > UnrealScript Interfaces

UnrealScript Interfaces


Overview


UnrealEngine3 UnrealScript has support for interface classes. Like much of UnrealScript the interface implementation resembles much of the Java implementation.

As with other programming languages interfaces can only contain function declarations and no function bodies. The implementation for these declared methods must be done in the class that actually implements the interface. All function types are allowed - normal functions and also events. Even delegates can be defined in interfaces (though with restrictions, as described in the Limitations section).

An interface can only contain declarations which do not affect the memory layout of the class - enums, structs and consts can be declared, but not variables.

Creating interfaces


The syntax for declaring an interface class is as follows (parts between square brackets are optional):

   interface InterfaceName [extends ParentInterface] [classSpecifiers];

   // interface body

And interface can optionally extend another interface. If no base interface is specified, it implicitely inherits from Core.Interface.

Also some class specifiers can be used. The following class specifiers are valid for interfaces:

native
This will make the interface native, a C++ interface declaration will be created. Native declared interfaces can only be implemented by native classes. Other restrictions for native classes are also relevant for native interfaces. The `native` keyword can be appened with a name of the filename to export the native declaration to. For example native(MyInterface). Using the name inherit will inherit the filename from the parent interface.
dependson(Class)
This doesn't add any functionality, it will just hint the compiler.

The interface body can also contain a cpptext block. This will simply be exported together with the native interface declaration.

Implementing Interfaces


Implementing interfaces works just as in Java:

   class MyClass extends Object implements(InterfaceName)

This class will also have to declare all functions as declared by the interface. The function declaration in the class must match the declaration in the interface, with the exception of the native keyword. A function declared native in an interface will not necessarily need to be native in a class which implements the interface, and vice versa.

A class can implement more than one interface using the following syntax:

   class MyClass extends Object implements(FirstInterface, SecondInterface)

Using interfaces


Using interface works just like using any other class.

   var InterfaceName InterfaceReference;

   function DoSomething()
   {
      InterfaceReference.callInterfaceFunction();
   }

   function DoSomethingElse(Object anObject)
   {
      if (InterfaceReference(anObject) != none)
      {
         // ...
      }
   }

In C++, a native interface variable is represented as a TScriptInterface, declared in UnTemplate.h. This struct stores two pointers to the same object - a UObject pointer and a pointer of the interface type.

The UObject function GetInterfaceAddress can be used to check whether a UObject instance implements a particular interface, and get a pointer to it if it does. For example,

UObject* pObj;
IInterfaceName* Interface = (IInterfaceName*)pObj->GetInterfaceAddress(IInterfaceName::UClassType::StaticClass());
// (if pObj does not implement IInterfaceName, Interface will be NULL.)

Limitations


  • Native interfaces (interface classes declared with the native keyword) can only be implemented by native classes.
  • Implementing multiple interface classes which have a common base is not supported and will result in incorrect vtable offsets. For this reason it is recommended that you organize your interface classes as a flat hierarchy, rather than using class inheritance for organization or logical grouping.
  • While delegates can be defined in interfaces, they can not be called or assigned new values through an interface reference; you must cast to an object implementing the interface instead.