Expose C++ interface default function implementations to blueprint

vikhik
Wed, 16 Sep 2015 23:44:24 +0000
UI

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);

};

11 posts - 1 participant