Skip to content

Instantly share code, notes, and snippets.

@rdeioris
Created April 2, 2020 16:12
Show Gist options
  • Save rdeioris/b5c9ed4352ba63cf954860842a23d35e to your computer and use it in GitHub Desktop.
Save rdeioris/b5c9ed4352ba63cf954860842a23d35e to your computer and use it in GitHub Desktop.

Create a new UObject subclass and modify it:

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "UObject/Interface.h"
#include "BetterKillerInterface.generated.h"

/**
 * 
 */
UINTERFACE(MinimalAPI)
class UBetterKillerInterface : public UInterface
{
	GENERATED_BODY()
	
};

class AIVPROGRAMMAZIONE3_API IBetterKillerInterface
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
	void Shot();

	UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
	void Kill();
};

Now both the Blueprint Classes as well as the C++ ones can inherit from it (Blueprint interfaces can be set from the class settings panel in the blueprint editor)

class FooBar : public AActor, public IBetterKillerInterface
{
};

You can call interface methods after checking for availability

if (FoundActor->GetClass()->ImplementsInterface(UBetterKillerInterface::StaticClass()))
{
  IBetterKillerInterface::Execute_Kill(FoundActor);
}

Note that you need to call the static method generated with the Execute_ prefix passing the this/context/owner as the first argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment