UE4 4.22 C++ Interface Implementation

Hi,

I’m trying to implement an interface so that when an actor overlaps a box collider i can use interface logic to call the specified function on the overlapped actor if it implements the interface. I’m having some issues getting this to work and none of the resources online I can find have been of any use. It seems like what was just previously working for other people no longer works, or atleast doesnt work for me.

Here’s my current implementation. I’d really appreciate some help or clarification with this.

Thank you.

Are you working from these notes: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

If so then the one thing they don’t really make clear is that you only need to bother with the Implementation and Execute post / pre fixes if you are working with Blueprint Native Events (or at least that’s the conclusion I’ve come to. If this is wrong then someone please correct me!)

In your case, your functions aren’t exposed to blueprints in any way, so you should be fine just use it as you would a normal C++ function (i.e. no Implementation of Execute modifiers).

You might also want to consider making your method pure virtual to enforce proper usage (i.e. in you interface header, use virtual void ModifyMovement() = 0;

I’ve got some of my methods defined as pure virtual. Example:

class YMK_API ISavable
{
...
virtual const bool IsSaveable() = 0;
...
}

Again, it’s a simple C++ only example where I don’t want anything exposed to blueprints, but works as expected.

I ended up figuring it out. Here’s the what worked in my case. PS. I had to rebuild code files to get some of the errors to go away.

As far as i’ve seen through researching pure virtual functions in unreal in the recent past, you can’t make functions pure virtual in unreal, at least not through that syntax.

I assume that’s not an unreal class at all? I can understand it working if so. Otherwise i’m confused haha. Thank you for your responses btw.

No it is an Unreal class and generated through the Editor. The whole thing is attached as reference in case it helps you or anyone else. I think the only real difference is I’m not using UFUNCTIONs here as I don’t need them.

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Dom/JsonObject.h"
#include "Savable.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class USavable : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class YMK_API ISavable
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

	virtual const bool IsSaveable() = 0;
	virtual const FString GetSaveObjectName() = 0;

	virtual void SaveObject(TSharedPtr< FJsonObject >& _saveJson) = 0;
	virtual void LoadObject() = 0;

	void SetSaveManager(class USaveManager* _saveManager);
	void ClearSaveManager();

	static FORCEINLINE FName GetAssetPath(const UObject* _obj)
	{
		if (_obj == NULL)
		{
			return NAME_None;
		}
		//~

		FStringAssetReference thePath = FStringAssetReference(_obj);

		if (!thePath.IsValid()) return NAME_None;

		//The Class FString Name For This Object
		FString str = _obj->GetClass()->GetDescription();

		str += "'";
		str += thePath.ToString();
		str += "'";

		return FName(*str);
	}

	template <typename ObjClass>
	static FORCEINLINE ObjClass* LoadAssetFromPath(const FName& _path)
	{
		if (_path == NAME_None)
		{
			return NULL;
		}
		//~

		return Cast<ObjClass>(StaticLoadObject(ObjClass::StaticClass(), NULL, *_path.ToString()));
	}

protected:
	const TSharedPtr<FJsonObject> GetLoadedObjectData();
	TWeakObjectPtr<class USaveManager> SaveManager;
	
};