Skip to content

Instantly share code, notes, and snippets.

@CoffeeVampir3
Created April 9, 2023 01:04
Show Gist options
  • Save CoffeeVampir3/2a535d4212b64510f3e29da887b5abaf to your computer and use it in GitHub Desktop.
Save CoffeeVampir3/2a535d4212b64510f3e29da887b5abaf to your computer and use it in GitHub Desktop.
Example interface in Unreal Engine 5.
AActor* ActorToUse = /* Get the actor that implements the ItemInterface */;
if (ActorToUse && ActorToUse->GetClass()->ImplementsInterface(UItemInterface::StaticClass()))
{
IItemInterface::Execute_Use(ActorToUse);
}
#include "ItemActor.h"
AItemActor::AItemActor()
{
PrimaryActorTick.bCanEverTick = true;
}
void AItemActor::Use_Implementation()
{
// Default C++ implementation, can be empty if you want to force Blueprint implementation only
}
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ItemInterface.h"
#include "ItemActor.generated.h"
UCLASS()
class AItemActor : public AActor, public IItemInterface
{
GENERATED_BODY()
public:
AItemActor();
UFUNCTION(BlueprintNativeEvent, Category = "Item")
void Use();
virtual void Use_Implementation() override;
};
#include "CoreMinimal.h"
#include "ItemInterface.generated.h"
class IItemInterface
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintNativeEvent, Category = "Item")
void Use();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment