Expose C++ interface default function implementations to blueprint

I have explained the detail, in full, with code snippets in this post on the forums, which I will copy below:

Hi guys,

I am posting something very similar to this very dead thread.

I am looking to make an interface with a set of UFUNCTIONS with default implementations. Then, I want to have a UActorComponent and a UAnimInstance which “implement” the interface, and expose it to blueprint for our designers to use. Ultimately the AnimInstance and ActorComponent should be calling the same function, they have other differences as well but these functions should be shared between them. It would be particularly nice to have one place to develop/maintain this subset of functionality. The alternative is for us to use a sub-component, but I’m not completely convinced that is a better option.

For now, I have the following code, and it all works at the C++ level (In BeginPlay for example), but does not give me the opportunity to call these functions in Blueprint. Can anyone help me make them visible?

EDIT: Note that the variables (Port/Address) are also not exposed!

Interface.h

UINTERFACE(MinimalAPI)
class UNetworkingDependent : public UInterface
{
	GENERATED_UINTERFACE_BODY()

};

class INetworkingDependent
{
	GENERATED_IINTERFACE_BODY()

public:
	UPROPERTY(Category = "Configuration", meta = (ToolTip = "The IP address of the Server to connect to."), BlueprintReadWrite, EditAnywhere)
		FString Address;

	UPROPERTY(Category = "Configuration", meta = (ToolTip = "The port of the Server to connect to."), BlueprintReadWrite, EditAnywhere)
		int32 Port;


	UFUNCTION(Category = "Data access ", meta = (ToolTip = "Retrieves the selected data point from the Server.", Keywords = "Server"), BlueprintPure)
		float GetDataValue(const ESomeEnum::Type DataPoint);

	UFUNCTION(Category = "Data access", meta = (ToolTip = "Returns...", Keywords = "Orientation"), BlueprintCallable)
		FRotator GetHRotation();

	UFUNCTION(Category = "Connection", meta = (ToolTip = "Returns true..."), BlueprintCallable)
		bool IsConnectedToServer();

protected:
	UPROPERTY()
		AUsefulClass* UsefulPtr;

	float DataCache[NUMBEROFVALUES];
};

Interface.cpp

UNetworkingDependent::UNetworkingDependent(const class FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{

}

float INetworkingDependent::GetDataValue(const ESomeEnum::Type DataPoint)
{
	return UsefulPtr != nullptr ? UsefulPtr.GetData[DataPoint] : 0.0f;
}

FRotator INetworkingDependent::GetHeadRotation()
{
	if (UsefulPtr!= nullptr)
	{
		FRotator RawValue = UsefulPtr->UpdateRotation();
		return FRotator(-RawValue.Pitch, -RawValue.Yaw, RawValue.Roll);
	}
	else
	{
		return FRotator::ZeroRotator;
	}
}

bool INetworkingDependent::IsConnectedToLiveServer()
{
	return UsefulPtr != nullptr && UsefulPtr->IsConnected;
}

OurAnimInstance.h

UCLASS()
class PLUGIN_API UOurAnimInstance : public UAnimInstance, public INetworkingDependent
{
	GENERATED_BODY()

public:
	virtual void NativeInitializeAnimation() override;
	virtual void NativeUpdateAnimation(float DeltaTimeX) override;

	UPROPERTY(Category = "Configuration", meta = (ToolTip = "Links the available Data Points with things"), BlueprintReadWrite, EditAnywhere)
	TArray<FSomething> SomeLocalVar;
};

OurActorComponent.h

UCLASS( ClassGroup=(SomeGroup), meta=(BlueprintSpawnableComponent) )
class PLUGIN_API UOurComponent : public UActorComponent, public INetworkingDependent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UOurComponent ();

	// Called when the game starts
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

	FString GetEnumStringValue(const ESomeEnum::Type DataPoint);
	
};

If you want the derived classes to be able to do their own implementation of those functions, you have to declare them as ‘virtual’ in the base class. In the derived implementation declare them as virtual and add ‘override’ at the end.

That’s uh, not the main issue. Although I will fix that too.

I want the derived class to be able to use a default implementation as made available in the interface, and then have that be exposed to blueprint as well.

The way your question is written makes it hard to answer.

You are basically just saying “my code doesn’t work please fix it for me”. Many people are very busy trying to fix their own bugs and don’t have time to respond to that. You will have much better like if you boil down your question to one specific problem.

In particular, don’t provide so much source code. Try to just get one function working and if you can’t just post the smallest possible amount of source code to make it easier for people to figure out the problem.

From a quick look, it seems like you are not following Epic’s instructions on how to do c++ interfaces. You need both a UInterface and an IInterface. Try to make a very simple example using the various C++ interface tutorials out there. Once you get that working, then worry about code sharing.

Also, I didn’t see stuff in your code about _Implementation functions in your interfaces.

You may want to repost a simplified version of this question after you look into those issues.

Good luck.

I have one specific problem: Things in C++ interfaces are not exposed to blueprint (more or less my title).

I’m sorry if you are unable to interpret my question, but you will find that

  1. I follow Epic’s advice on interfaces. In fact the first 5 lines of code I have in my question are UINTERFACE/IINTERFACE that you don’t think that I’ve done.
  2. I have provided the bare minimum of code required to replicate the issue (with several examples of the same issue with slightly different styles to indicate that I have tried many more things)
  3. I am not asking for BLUEPRINT override-able functionality, but C++ override-able with blueprint access whether overridden or not
  4. you’ll note in my edit that I also note that any variables in interfaces are unable to be exposed to blueprint either

Someone can correct me if I’m wrong, but I’m pretty sure Interfaces force classes using it to have an implementation. If you want a default implementation that is overridable, then I wouldn’t use an interface. I would make it a base class. The only difference should be an extra Cast to use the ufunction.

To be fair, and for future reference, a minimal example would consist of 1 single function you’re trying to make work. Your INetworkingDependent has 7 different member functions/variables in it and makes it difficult for someone jumping in from the outside. Most busy people shy away from a wall of text like that. Same for your UOurAnimInstance class. IMO, it would also be better for yourself if you remove all confounding factors (i.e. reduce it to 1 single function).

Here’s an example from the wiki: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums. It presents a single, simple, interface function: ToString. Which just returns “Frog” or “Flower”. That’s a better minimal case.

Anyway, not giving you a hard time, just my 2 cents on what would help you get more responses.

I’m sorry I couldn’t solve your problem. Instead of a thank you, you down voted my response. Not cool considering you specifically asked me to reply to your post.

You can’t have UObject derived alternate base classes though (due to diamond inheritence), meaning you can’t have UPROPERTY or UFUNCTION members.

And I can’t inject a common method between an AnimInstance and an ActorComponent any other way, afaik?

If I’m understanding your question right, some videos I made about this should help: C++ Interface tutorial

They cover how to write c++ interfaces which are intended to have C++ base implementations, with the potential to add blueprint functionality on top of that, which sounds like what you’re after.

Thanks! This looks like it has a lot of potential!

I’ll look into it (and accept when I have time to test it) :slight_smile: