我有两个属性:
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 ==========