# 実装したインターフェース関数を呼び出せない。

**URL:** <https://forums.unrealengine.com/t/topic/1837957>\
**Category:** 日本語 (Japanese)\
**Tags:** unreal-engine, question, CPP\
**Created:** [May 3, 2024, 11:45am UTC](https://forums.unrealengine.com/t/topic/1837957 "2024-05-03T11:45:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![masamichi00000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@masamichi00000](https://forums.unrealengine.com/u/masamichi00000)\
**Post date:** [May 3, 2024, 11:45am UTC](https://forums.unrealengine.com/t/topic/1837957/1 "2024-05-03T11:45:55Z")

</div>

**環境**  
Windows10  
UE5.1

**やりたい事**

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/3/4/f34f4bcf90fa49a722ff1f8ec60170ba07a5d81f.png)  
画像の様な事をC++で実装したいです。

**問題**

```auto
AActor* hitActor = HitResult.GetActor();

if (Cast<IIF_Interact>(hitActor))
{
	Cast<IIF_Interact>(hitActor)->Execute_FocusOn(hitActor);
}

```

上記記述で実行すると  
クラス IIF\_Interact にメンバー Execute\_FocusOn がありません  
というエラーが発生します。

**他ファイル**

インターフェース  
IF\_Interact.h

```auto
UINTERFACE(MinimalAPI)
class UIF_Interact : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class CUSTOMGAME_API IIF_Interact
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	virtual void FocusOn();
};

```

IF\_Interact.cpp

```auto
// Add default functionality here for any IIF_Interact functions that are not pure virtual.
void IIF_Interact::FocusOn() {}

```

実装クラス  
AC\_CraftingTable.h

```auto
UCLASS()
class CUSTOMGAME_API AAC_CraftingTable : public AA_BaseConstruct, public IIF_Interact
{
	GENERATED_BODY()
	
public:
	virtual void FocusOn() override;

};

```

AC\_CraftingTable.cpp

```auto
// Add default functionality here for any IIF_Interact functions that are not pure virtual.
void IIF_Interact::FocusOn() {}

```

**試した事**  
実装クラス内で実装時の関数名をFocusOn\_Implementation()にしてみた。  
その場合、基底クラスにそんな関数ないよと怒られました。

**参考にしたサイト**

> [@Calling Interface Functions In C++](https://forums.unrealengine.com/t/calling-interface-functions-in-c/323521?lang=ja):
>
> I am trying to implement an interaction system via an interface but am having trouble figuring out how to call functions in my interface via C++. I am able to call these interface functions in Blueprints with the message nodes. Some answers online talk about using Execute\_\* functions but I can’t find any clear description on how to do this. I have tried casting with Cast\<\>() and InterfaceCast but these cause my project to crash. Specifically, what I am trying to do is run a trace to see if the…

> **[Unreal Engine のインターフェース | Unreal Engine 5.3 ドキュメンテーション | Epic Developer...](https://dev.epicgames.com/documentation/ja-jp/unreal-engine/interfaces-in-unreal-engine?application_version=5.3)**
>
> インターフェースの作成と実装に関するリファレンス。

> [@C++ Interfaces - Execute\_FunctionName() vs FunctionName\_Implementation()](https://forums.unrealengine.com/t/c-interfaces-execute-functionname-vs-functionname-implementation/770619):
>
> Interfaces are probably for me one of the most clunky implementations in the Unreal framework in my opinion. Straight question. I have a class called Foo. Foo has a function called DoThis(). I have an interface called IFooable which has a function DoThis(); Foo implements IFooable. I know that in unreal I can’t say MyIFooObject-\>DoThis() (where MyIFooObject is an Interface variable). However… this is where I need some guidance because I can’t find a straight answer. The way I learned unr…

---

<div class="post-metadata">

**Author:** ![T\_Sumisaki](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@T\_Sumisaki](https://forums.unrealengine.com/u/T_Sumisaki)\
**Post date:** [May 3, 2024, 3:01pm UTC](https://forums.unrealengine.com/t/topic/1837957/2 "2024-05-03T15:01:02Z")

</div>

> 上記記述で実行すると  
> クラス IIF\_Interact にメンバー Execute\_FocusOn がありません  
> というエラーが発生します。

その理由は、呼び出している関数がUFUNCTIONではないからです  
`Execute_ ***` は、関数がUFUNCTIONであるときに、UHTにより自動生成される** BP用の** 関数です

C++実装のみであれば、通常のC++関数として呼びましょう  
UFUNCTIONであるときも、事前にCastするのであれば `Execute_***` の方を使う必要はないです

> 実装クラス内で実装時の関数名をFocusOn\_Implementation()にしてみた。  
> その場合、基底クラスにそんな関数ないよと怒られました。

こちらも同様に、UFUNCTIONかつBlueprintNativeEventである必要があります  
その場合も`***_Implementation` の方を直接呼んではいけません  
それはBPによるOverride実装を無視することになります

---

<div class="post-metadata">

**Author:** ![masamichi00000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@masamichi00000](https://forums.unrealengine.com/u/masamichi00000)\
**Post date:** [May 4, 2024, 11:41am UTC](https://forums.unrealengine.com/t/topic/1837957/3 "2024-05-04T11:41:29Z")

</div>

ありがとうございます！  
少し試してから、自身の理解踏まえて後ほどお返事します！

---

<div class="post-metadata">

**Author:** ![masamichi00000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@masamichi00000](https://forums.unrealengine.com/u/masamichi00000)\
**Post date:** [May 5, 2024, 3:09am UTC](https://forums.unrealengine.com/t/topic/1837957/4 "2024-05-05T03:09:44Z")

</div>

頂いたお返事を元に色々試して下記で動作しました！  
ありがとうございます！

**インターフェース**  
IF\_Interact.h  
FocusOnに緑の波線で関数定義が見つからないと出ているが動く

```auto
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UIF_Interact : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class CUSTOMGAME_API IIF_Interact
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
	void FocusOn();
};

```

IF\_Interact.cpp  
前述の関数定義が見つからないエラーの為定義すると既に本体を持っている(C2084)となり実行不可

```auto
// Add default functionality here for any IIF_Interact functions that are not pure virtual.
//void IIF_Interact::FocusOn() {}

```

**実装クラス**  
AAC\_CraftingTable .h

```auto
UCLASS()
class CUSTOMGAME_API AAC_CraftingTable : public AA_BaseConstruct, public IIF_Interact
{
	GENERATED_BODY()
	
public:
	virtual void FocusOn_Implementation() override;

};

```

AAC\_CraftingTable .cpp

```auto
void AAC_CraftingTable::FocusOn_Implementation()
{
	UKismetSystemLibrary::PrintString(this, "called in AAC_CraftingTable", true, true, FColor::Red, 5.0f, "None");
}

```

**関数呼び出しクラスの一部**

```auto
	GetWorld()->LineTraceSingleByObjectType(OUT HitResult, start, end, FCollisionObjectQueryParams(ECollisionChannel::ECC_WorldStatic));
	AActor* hitActor = HitResult.GetActor();
	DrawDebugLine(
		GetWorld(),
		start,
		HitResult.Location,
		FColor(255, 0, 0),
		true, -1, 0,
		12.333
	);

	if (hitActor == nullptr)
	{
		UKismetSystemLibrary::PrintString(this, "its null !!!!", true, true, FColor::Red, 5.0f, "None");
		return;
	}

	UKismetSystemLibrary::PrintString(this, "=================================", true, true, FColor::Red, 5.0f, "None");
	//Cast<IIF_Interact>(hitActor)->Execute_FocusOn(hitActor);
	bool bImpl = hitActor->GetClass()->ImplementsInterface(UIF_Interact::StaticClass());
	if (bImpl)
	{
		UKismetSystemLibrary::PrintString(this, "hit actor has Interact Interface", true, true, FColor::Red, 5.0f, "None");
    // This is calling IF Function.
		IIF_Interact:: Execute_FocusOn(hitActor);
	}
	else
	{
		UKismetSystemLibrary::PrintString(this, "No Interact Interface", true, true, FColor::Red, 5.0f, "None");
	}

```

**実装クラス** を継承したBPの子クラスでは、クラス設定の中の継承インターフェースに  
**IF\_Interact** がある事も確認できました。  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/5/e/e5ee3273b1d2a92d3c6eba4e42d9093b6b8ef6b9.png)

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 4, 2024, 3:10am UTC](https://forums.unrealengine.com/t/topic/1837957/5 "2024-06-04T03:10:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
