沿用(十三)中的类文件。
在GAMEMODEBASE文件中新增BeginPlay方法。
1 #include "CoreMinimal.h"
2 #include "GameFramework/GameModeBase.h"
3 #include "MyProject7_INTERFACEGameModeBase.generated.h"
4
5 /**
6 *
7 */
8 UCLASS()
9 class MYPROJECT7_INTERFACE_API AMyProject7_INTERFACEGameModeBase : public AGameModeBase
10 {
11 GENERATED_BODY()
12
13 public:
14 virtual void BeginPlay() override;
15 };
并实现它
1 #include "MyProject7_INTERFACEGameModeBase.h"
2 #include "MyActor.h"
3 #include "MyProject7_INTERFACE/MyInterface.h"
4 #include "Engine.h"
5
6 void AMyProject7_INTERFACEGameModeBase::BeginPlay() {
7 Super::BeginPlay();
8
9 FTransform spawnLocation;
10 //FTransform trans = FTransform(FRotator(0), FVector(0), FVector(0));
11 AMyActor* spawnActor = GetWorld()->SpawnActor<AMyActor>(AMyActor::StaticClass(), spawnLocation);
12
13 UClass* actorClass = spawnActor->GetClass(); //获取这个对象的类的引用
14
15 if (actorClass->ImplementsInterface(UMyInterface::StaticClass())) { //判断条件是如果这个类是实现了某个接口时候,注意这里是UMyInterface类,而不是IMyInterface类
16 GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("Implements the Interface"));
17 }
18 }
效果如下: