How to call an interface implemented both BP and C++ from C++

I implemented a code to call interface in C++. The interface is implemented both BP and C++. I wrote like below and it works well. Are there any problems with this way? Is there any better code?

bool CallTest(AActor* Target, FParamA& A, const FParamB& B)
{
	if (Target->Implements<UTestInterface>()) // Call Blueprint interface if implemented in BP.
	{
		return ITestInterface::Execute_Test(Target, A, B);
	}

	else if (ITestInterface* Interface = Cast<ITestInterface>(Target)) // Else call C++ interface if implemented in C++.
	{
		return Interface->Execute_Test(Target, A, B);
	}

	return false;
}

use the 1st one. no need for the other.

check the section “determining …”
there are 3 ways. and the text says " use any" . afaik you don’ t need to call them twice. just once.

in the projects i had work on we usually used

	if (Target->Implements<UTestInterface>()) // Call Blueprint interface and cpp too
	{
		return ITestInterface::Execute_Test(Target, A, B);
	}

though personally i dont love it, i think is the most explicit.

Thank you for the answer!

That was what I wanted to know. I didn’t have confidence if my code was correct.

1 Like

all good. actually being curious and asking/researching when in doubt helps you learn faster and deeper. :clap:
(though i’d be cautious of answers that might be dogmatic)

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