Interface 함수를 C++에서 구현할 때 해당 함수를 C++에서만 사용하는 방법과 블루프린트에서도 사용하는 방법 2가지로 만들 수 있다.
C++ 에서만 사용할 경우
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreEngine.h"
#include "UObject/Interface.h"
#include "Interface_Character.generated.h"
UINTERFACE(MinimalAPI)
class UInterface_Character : public UInterface
{
GENERATED_BODY()
};
class PROJECTCE_API IInterface_Character
{
GENERATED_BODY()
public:
virtual void SetOnAttack(bool bFlag);
};
<Interface_Character.h>
// Fill out your copyright notice in the Description page of Project Settings.
#include "Interface_Character.h"
// Add default functionality here for any IInterface_PCEPlayerCharacter functions that are not pure virtual.
void IInterface_Character::SetOnAttack(bool bFlag)
{
}
<Interface_Character.cpp>
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreEngine.h"
#include "Interface_Character.h"
#include "PlayerCharacter.generated.h"
class PROJECTCE_API APlayerCharacter : public IInterface_Character
{
GENERATED_BODY()
public:
virtual void SetOnAttack(bool bFlag) override;
bool bOnAttack;
};
<PlayerCharacter.h>
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerCharacter.h"
void APlayerCharacter::SetOnattack(bool bFlag)
{
bOnAttack = bFlag;
}
<PlayerCharacter.cpp>
AActor* Character = GetOwner();
IInterface_PlayerCharacter* Interface = Cast<IInterface_PlayerCharacter>(Character);
if(Interface != nullptr)
{
Interface->SetOnAttack(false);
}
<호출 방법>
블루프린트에서도 사용할 경우
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreEngine.h"
#include "UObject/Interface.h"
#include "Interface_Character.generated.h"
UINTERFACE(MinimalAPI)
class UInterface_Character : public UInterface
{
GENERATED_BODY()
};
class PROJECTCE_API IInterface_Character
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
void SetOnAttack(bool bFlag);
};
<Interface_Character.h>
cpp 파일은 구현하지 않아도 된다.
<Interface_Character.cpp>
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreEngine.h"
#include "Interface_Character.h"
#include "PlayerCharacter.generated.h"
class PROJECTCE_API APlayerCharacter : public IInterface_Character
{
GENERATED_BODY()
public:
virtual void SetOnAttack_Implementation(bool bFlag) override;
bool bOnAttack;
};
<PlayerCharacter.h>
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerCharacter.h"
// _Implementation을 붙인 함수를 사용한다.
void APlayerCharacter::SetOnAttack_Implementation(bool bFlag)
{
bOnAttack = bFlag;
}
<PlayerCharacter.cpp>
AActor* Character = GetOwner();
if (Character->GetClass()->ImplementsInterface(UInterface_PlayerCharacter::StaticClass()))
{
IInterface_PlayerCharacter::Execute_SetOnAttack(Character, false);
}
<호출 방법>
'Unreal Engine > C++' 카테고리의 다른 글
[Unreal Engine4 C++] 클래스의 헤더파일을 전방 선언할 수 있는 조건 (0) | 2021.04.12 |
---|---|
[Unreal Engine4 C++] Delay 구현 (0) | 2021.04.06 |
Unreal Engine4 C++ 특정 액터가 어떤 타입인지 확인하는 방법 (0) | 2021.03.24 |
[Unreal Engine 4 C++] Tutorial 유튜브 사이트 (0) | 2021.03.03 |
[Unreal Engine 4 C++] 반환 값이 2개 이상일 경우에 사용하는 TTuple (0) | 2021.03.03 |