x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Can't compile new UInterface

I've been trying to create a C++ interface following this page of the 4.14 documentation to implement user interaction with actors. So, I made a a default UInterface using the Create C++ class wizard in the editor. The idea is that the Main Character class fires a single line trace to identify an Actor, get the Actor and check whether or not it implements my interface. If it does, fire the event on the Actor. My interface looks like this;

MainCharacterInteraction.h

 #pragma once

 #include "MainCharacterInteraction.generated.h"
 
 UINTERFACE(Blueprintable)
 class UMainCharacterInteraction : public UInterface
 {
     GENERATED_BODY()
 };

 class MYAPI_API IMainCharacterInteraction
 {
     GENERATED_BODY()
 
 public:
     UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "Player interaction")
     void Interact() const;
 };

MainCharacterInteraction.cpp

 void AMainCharacterInteractionActor::Interact() const
 {
 
 }

Now, the guide itself is quite vague about the implementation side, because it shows how to implement the interface, but not how to give any functionality to it.

MainCharacterInteractionActor.h

 #pragma once
 
 #include "CoreMinimal.h"
 #include "Interfaces/Character/MainCharacterInteraction.h"
 #include "MainCharacterInteractionActor.generated.h"
 
 UCLASS()
 class MYAPI_API AMainCharacterInteractionActor : public AActor, public IMainCharacterInteraction
 {
     GENERATED_BODY()
 
 public:
     AMainCharacterInteractionActor(const FObjectInitializer& ObjectInitializer);
 
     UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Player interaction")
     virtual void Interact() const override;
 };

So, I'd assume that on the implementation side of the class I should do something like;

MainCharacterInteractionActor.cpp

 void AMainCharacterInteractionActor::Interact() const
 {
 
 }

But whenever I try and compile this code (either via the Editor -> Compile, or Visual Studio -> Build) it fails with the following error:

 MainCharacterInteractionActor.h:17:27: error: only virtual member functions can be marked 'override'

What am I doing wrong?

Product Version: UE 4.18
Tags:
more ▼

asked Mar 05 '18 at 12:23 PM in C++ Programming

avatar image

Craimasjien
31 4 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

As it turns out, the guide I followed was specifically tailored for BlueprintImplementableEvents, what I needed was a BlueprintNativeEvent. For people that might end up with the same issues as me; the trick is that the BlueprintNativeEvent variants magically get some extra methods when creating the events using an interface. Calling these methods should be done using the Execute_ method, which gets generated during compilation.

My code for future reference:

MainCharacterInteraction.h

 UINTERFACE(Blueprintable)
 class UMainCharacterInteraction : public UInterface
 {
     GENERATED_BODY()
 };
 
 class MYAPPI_API IMainCharacterInteraction
 {
     GENERATED_BODY()

 public:
     UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Player interaction")
     void Interact();
 };

MainCharacterInteraction.cpp

 #include "MainCharacterInteraction.h"

MainCharacterInteractionActor.h

 UCLASS()
 class MYAPI_API AMainCharacterInteractionActor : public AActor, public IMainCharacterInteraction
 {
     GENERATED_BODY()
 
 public:
     AMainCharacterInteractionActor(const FObjectInitializer& ObjectInitializer);
 
     UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="Player interaction")
     void Interact();
     virtual void Interact_Implementation() override;
 };

MainCharacterInteractionActor.cpp

 AMainCharacterInteractionActor::AMainCharacterInteractionActor(const FObjectInitializer& ObjectInitializer)
 {
 }
 
 void AMainCharacterInteractionActor::Interact_Implementation()
 {
     // Either called via ObjectThatImplementsInterface->ExecuteInteract(Caller); or using a Blueprint override.
 }



more ▼

answered Mar 05 '18 at 02:02 PM

avatar image

Craimasjien
31 4 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hello!

this should be void Interact() const; virtual void Interact() const; in your interface class

and actually you dont need const because you did not return anything..

so go ahed and use

 virtual void Interact();
more ▼

answered Mar 05 '18 at 12:26 PM

avatar image

AmphDev
1.5k 15 9 33

avatar image Craimasjien Mar 05 '18 at 12:34 PM

Thanks for your answer. It didn't help though; If I declare the interface function as virtual void Interact(), I get this error: Error: BlueprintImplementableEvents in Interfaces must not be declared 'virtual'

avatar image AmphDev Mar 05 '18 at 12:50 PM

you have right sry... missed that is a blueprintimplementableEvent :) I said then wrong...

So the first problem with that.. BlueprintImplementableEvents must be overrided in Blueprint! This is why we call them BlueprintImplementableEvents..

You cant override them.. in C++! If you want override in c++ .. so you wanna do things both in c++ code and blueprint code you shuld use BlueprintNativeEvent

So you cant declare interface function as blueprintimplementableevent, then override as blueprintnative event..

That must be delcared as blueprintnativeevent and when you override you must override in this form:

virtual void Interact_Implementation() override;

Further Informations about interfaces and UFUNCTION marks:

https://wiki.unrealengine.com/Interfaces_in_C%2B%2B

https://wiki.unrealengine.com/UFUNCTION

avatar image Craimasjien Mar 05 '18 at 01:56 PM

Your answer pushed me in the right direction. Thanks.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question