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
中国翻译
한국어
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
Creating interfaces
interface InterfaceName [extends ParentInterface] [classSpecifiers]; // interface bodyAnd 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 nameinherit
will inherit the filename from the parent interface. - dependson(Class)
- This doesn't add any functionality, it will just hint the compiler.
cpptext
block. This will simply be exported together with the native interface declaration.
Implementing Interfaces
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
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.