如何实例化接口的实现

How to instantiate an implementation of interface?

本文关键字:实现 接口 实例化      更新时间:2023-10-16

我有两个属性:

UPROPERTY()
TScriptInterface<ICoordinateSystem> CoordinateSystem;
UPROPERTY(EditAnywhere)
TSubclassOf<UCoordinateSystem> CoordinateSystemType;

如何实例化坐标系统类型并将结果存储到坐标系统?我尝试了不同的方式,例如

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:UE4UE44X 4.12UE44X.uproject" "F:UE4UE44X 4.12IntermediateBuildWin64UE44XDebugGameUE44X.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 Games4.12EngineSourceRuntimeCoreUObjectPublicUObjectScriptInterface.h(150): error C2664: 'void FScriptInterface::SetObject(UObject *)': cannot convert argument 1 from 'ICoordinateSystem *' to 'UObject *'
1>  C:Program Files (x86)Epic Games4.12EngineSourceRuntimeCoreUObjectPublicUObjectScriptInterface.h(150): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>  F:UE4UE44X 4.12SourceUE44XUE44XGameMode.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:UE4UE44X 4.12SourceUE44XUE44XGameMode.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:UE4UE44X 4.12BinariesWin64UE44X-Win64-DebugGame.pdb
1>  Total build time: 11,90 seconds
1>C:Program Files (x86)MSBuildMicrosoft.Cppv4.0V140Microsoft.MakeFile.Targets(37,5): error MSB3075: The command ""C:Program Files (x86)Epic Games4.12EngineBuildBatchFilesBuild.bat" UE44X Win64 DebugGame "F:UE4UE44X 4.12UE44X.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 ==========

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

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

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

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

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

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