Skip to content

Instantly share code, notes, and snippets.

@msmoiz
Created April 8, 2021 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msmoiz/590d74812347b521ce668cdf070b1f7e to your computer and use it in GitHub Desktop.
Save msmoiz/590d74812347b521ce668cdf070b1f7e to your computer and use it in GitHub Desktop.
Interface Demo for Unreal Engine 4
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "InterfaceDemo.generated.h"
UINTERFACE()
class UBaseInterface : public UInterface
{
GENERATED_BODY()
};
class IBaseInterface
{
GENERATED_BODY()
public:
// Will cause a compile error
// UPROPERTY()
// int32 Variable;
// Will compile, but should be avoided
// int32 SomeOtherVariable;
};
UINTERFACE(meta=(CannotImplementInterfaceInBlueprint))
class UNativeInterface : public UInterface
{
GENERATED_BODY()
};
class INativeInterface
{
GENERATED_BODY()
public:
virtual void DefinedVirtualFunction() {};
virtual void PureVirtualFunction() = 0;
UFUNCTION(BlueprintCallable)
virtual void ExposedVirtualFunction() {}
};
UINTERFACE(/* Blueprintable */)
class UMixedInterface : public UInterface
{
GENERATED_BODY()
};
class IMixedInterface
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintImplementableEvent, BlueprintCallable)
void BPFunction();
UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
void BPNativeFunction();
// Will cause a compile error
// UFUNCTION(BlueprintCallable)
// virtual void ExposedVirtualFunction() {}
};
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "InterfaceDemo.h"
#include "InterfaceImplementation.generated.h"
UCLASS()
class UImplementor
: public UObject
, public INativeInterface
, public IMixedInterface
{
GENERATED_BODY()
public:
void DefinedVirtualFunction() override
{
// do something
};
void PureVirtualFunction() override
{
// do something else
}
void BPNativeFunction_Implementation() override
{
// do something native
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment