如何实例化接口的实现?

时间:2016-06-04 08:06:05

标签: c++ types unreal-engine4

我有两个属性:

UPROPERTY()
TScriptInterface<ICoordinateSystem> CoordinateSystem;

UPROPERTY(EditAnywhere)
TSubclassOf<UCoordinateSystem> CoordinateSystemType;

如何实例化CoordinateSystemType并将结果存储到CoordinateSystem?我尝试了不同的方式,如

CoordinateSystem = NewObject<ICoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType); 
CoordinateSystem = NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType);
CoordinateSystem = Cast<ICoordinateSystem>(NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType));

但是没有人编译说类似的东西可以在NewObject或TScriptInterface =运算符中将UObject *转换为ICoordinateSystem *或ICoordinateSystem *到UObject *。 我在UE 4.11和4.12

中尝试了它

编辑:代码

CoordinateSystem = Cast<ICoordinateSystem>(NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType));

我收到错误:

1>------ Build started: Project: UE44X, Configuration: DebugGame_Game x64 ------
1>  Creating makefile for UE44X (working set of source files changed)
1>  Parsing headers for UE44X
1>    Running UnrealHeaderTool "F:\UE4\UE44X 4.12\UE44X.uproject" "F:\UE4\UE44X 4.12\Intermediate\Build\Win64\UE44X\DebugGame\UE44X.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
1>  Reflection code generated for UE44X in 4,8843075 seconds
1>  Performing 2 actions (4 in parallel)
1>  UE44XGameMode.cpp
1>C:\Program Files (x86)\Epic Games\4.12\Engine\Source\Runtime\CoreUObject\Public\UObject\ScriptInterface.h(150): error C2664: 'void FScriptInterface::SetObject(UObject *)': cannot convert argument 1 from 'ICoordinateSystem *' to 'UObject *'
1>  C:\Program Files (x86)\Epic Games\4.12\Engine\Source\Runtime\CoreUObject\Public\UObject\ScriptInterface.h(150): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>  F:\UE4\UE44X 4.12\Source\UE44X\UE44XGameMode.cpp(26): note: see reference to function template instantiation 'InterfaceType &TScriptInterface<InterfaceType>::operator =<To>(UObjectType *)' being compiled
1>          with
1>          [
1>              InterfaceType=ICoordinateSystem,
1>              To=ICoordinateSystem,
1>              UObjectType=ICoordinateSystem
1>          ]
1>  F:\UE4\UE44X 4.12\Source\UE44X\UE44XGameMode.cpp(26): note: see reference to function template instantiation 'InterfaceType &TScriptInterface<InterfaceType>::operator =<To>(UObjectType *)' being compiled
1>          with
1>          [
1>              InterfaceType=ICoordinateSystem,
1>              To=ICoordinateSystem,
1>              UObjectType=ICoordinateSystem
1>          ]
1>ERROR : UBT error : Failed to produce item: F:\UE4\UE44X 4.12\Binaries\Win64\UE44X-Win64-DebugGame.pdb
1>  Total build time: 11,90 seconds
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets(37,5): error MSB3075: The command ""C:\Program Files (x86)\Epic Games\4.12\Engine\Build\BatchFiles\Build.bat" UE44X Win64 DebugGame "F:\UE4\UE44X 4.12\UE44X.uproject" -waitmutex" exited with code 5. Please verify that you have sufficient rights to run this command.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:1)

接口通常被视为声明函数的类,但这些函数通常在子类中实现。

在UE4中,IInterface不是UObject。您需要创建一个派生自您的界面的类。

class MyClass : public UObject, public ICoordinateSystem
{
    //Overrides functions from ICoordinateSystem
    ....
};

您将能够实例化MyClass。要获取MyClass的CoordinateSystem句柄,请使用Cast模板...

MyClass* myClassInstance;
....
ICoordinateSystem* coordinateInterface = Cast<ICoordinateSystem>(myClassInstance);

希望本文有助于阐明如何使用接口: https://wiki.unrealengine.com/Interfaces_in_C%2B%2B