Download

C++ Interface not being called

Hello! I’ve been struggling with this for many hours already and it looks like I’m folding :mad: I’ve been trying to follow both the Rama wiki tutorial and the official Epic docs entry for interfaces.

I’m trying to create a C++ Interface for my interactables, here is the .h where it’s declared:



#pragma once

#include "Engine/Engine.h"
#include "InteractInterface.generated.h"

UINTERFACE(Blueprintable)
class UInteractInterface : public UInterface
{
	GENERATED_BODY()
};

class IInteractInterface
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category="Interact")
	bool OnPlayerLookAt();
};


I have nothing in the .cpp (maybe that’s the issue).

This is what I have in the interact object.h (under public):



bool OnPlayerLookAt_Implementation();


and in the interact object.cpp (under public as well):



bool APickup::OnPlayerLookAt_Implementation()
{
	return false;
}


and here is where I call it:



IInteractInterface* InteractObject = Cast<IInteractInterface>(ActorRef);
	
	if (InteractObject)
	{
		Cast<IInteractInterface>(InteractObject)->Execute_OnPlayerLookAt(Cast<UObject>(InteractObject));
		IInteractInterface::Execute_OnPlayerLookAt(ActorRef);
	}


Yes, I’m calling it twice, that’s just me trying to get it to work… The code definitely gets to where it’s supposed to be, because I also tried calling APickup::OnPlayerLookAt_Implementation() directly and it did what it was supposed to. The issue is definitely interface-related, and I have no idea what it is. I tried changing the function declaration in the .h to “virtual ----- override” but it just gives me an error about how there’s nothing to override. If I leave it with virtual and no override it compiles but same result.

I also tried to write a basic declaration in the .cpp of the interface for the function but it claimed it was already defined in the .obj and that it ignored the second definition. I then tried adding “virtual ----- override” and it said that BlueprintImplementable functions couldn’t be virtual. So then I stripped it from any blueprint capabilities and tried compiling again. THEN it complained that the function didn’t override any base class methods.

Thanks for any help and sorry for the nature of this request :confused:

I’m almost sure the function name in the declaration (header) should be just OnPlayerLookAt and only in the cpp should it be OnPlayerLookAt_Implementation

BlueprintImplementableEvent does not allow for C++ implementation, so your _Implementation is just a completely unrelated method. You need to use BlueprintNativeEvent instead.

You should add the override keyword at the end of your declaration in interactobject.h. That way the compiler will let you know if the method hasn’t overridden anything, rather than just compiling fine and giving you an unrelated function that will never be called.

Thank you guys so much for your help! Unfortunately I still haven’t gotten it working.

I thought that too but whenever I tried that compilation would fail and it would say that there is no “OnPlayerLookAt_Implementation” function. So what I did was right under


bool OnPlayerLookAt();

I added


bool OnPlayerLookAt_Implementation();

and then it managed to compile but still the function was not called.

Yeah I remember reading that so I also tried it with BlueprintNativeEvent but it still didn’t work. No compiler errors or anything, just didn’t work.

Here’s a screenshot of the UE4 editor log I get when I just declare


 bool OnPlayerLookAt(); 

in the pickup.h, and implement


 bool OnPlayerLookAt_Implementation() 

in the pickup.cpp. Inside the interface’s .h the only UPROPERTY the value has is BlueprintNativeEvent.
jHMqR8p.png

OnPlayerLookAt should be declared in the IInterface only.

In your object, you need bool OnPlayerLookAt_Implementation() override; in the header, and bool OnPlayerLookAt_Implementation() { implementation } in the cpp.

I tried it and it didn’t work, then I closed the project, rebuilt the VS files, rebuilt everything, and it magically works now.

http://timrymel.com/wp-content/uploads/2014/06/cliff-jump.jpg

I wonder how much time I could’ve saved if I just did that from the beginning. How do I know when I’m supposed to do just rebuild everything? No mention of that in the wiki :confused:

THANK YOU! This was driving me insane. Then I just rebuild directly from VS rather than UE 4.23, and it works exactly as intended…really makes you wonder sometimes…

I’ve only been using UE4 C++ for a couple weeks now, and if there’s anything I’ve learned: either closing everything down and restarting or deleting stuff from the project folder and generating project files will be the solution to 80% of problems.