Calling Interface Functions In C++

I use them extensively, here’s an example:

.h (blueprint callable interface, but implemented in c++)

#pragma once
//this is important for the auto-generated executed functions.
#include "EntityInterface.generated.h"

/** Interface for actors which can be associated with teams */
/** Class needed to support InterfaceCast<IToStringInterface>(Object) */
UINTERFACE(meta = (CannotImplementInterfaceInBlueprint = true))
class UEntityInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class IEntityInterface
{
	GENERATED_IINTERFACE_BODY()

	/** returns the owner number of the actor (0=world, otherwise it's a player) */
	UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
	virtual int32 GetOwnerNum() const = 0;

	/** sets the owner number of the actor */
	UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
	virtual void SetOwnerNum(const int32 OwnerNum) = 0;

	/** returns the id number of the actor */
	UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
	virtual int32 GetEntityId() const = 0;

	/** sets the id number of the actor */
	UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
	virtual void SetEntityId(const int32 NewEntityId) = 0;

	/** Remove the given amount from entity's healt (net unsafe) */
	UFUNCTION(BlueprintCallable, Category = "ImperoEntities")
	virtual void ApplyDamage(int32 Amount) = 0;
};

another .h (Blueprint implementable events)

#pragma once

#include "SelectableInterface.generated.h"

/** Interface for actors which can be associated with teams */
/** Class needed to support InterfaceCast<IToStringInterface>(Object) */
UINTERFACE()
class USelectableInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class ISelectableInterface
{
	GENERATED_IINTERFACE_BODY()

	/** tries to select actor */
	UFUNCTION(BlueprintImplementableEvent, Category = Selection)
	void OnSelectionGained();

	/** tries to deselect actor */
	UFUNCTION(BlueprintImplementableEvent, Category = Selection)
	void OnSelectionLost();

	UFUNCTION(BlueprintImplementableEvent, Category = Commands)
	bool DoOnTargetEvent(const TScriptInterface<IEntityInterface>& Target, const TEnumAsByte<EJobType::Type>& JobType);
};

.cpp (tip: you can implement more than one interface in only 1 cpp, as u can see)

#include "MyProject.h"
//#include "Interfaces/SelectableInterface.h"
//#include "Interfaces/MultipleSelectableInterface.h"

USelectableInterface::USelectableInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}

UMultipleSelectableInterface::UMultipleSelectableInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}

UEntityInterface::UEntityInterface(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}

and here’s how i call the functions:

//i have multiple targets at once
TArray<IEntityInterface*> Subjects;
//fill the array somehow

//and then call the function on the interface
//note that this function is only declared in the interface,
//the implementation is on Blueprints side

for (IEntityInterface* Subject : Subjects)
{
    ISelectableInterface* s = Cast<ISelectableInterface>(Subject);
    //note that actually i'm using 2 different interfaces
    if (s)
    {
        s->Execute_DoOnTargetEvent(Cast<UObject>(s), Target, Job);
    }
}

Hope this helps you :slight_smile: