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"

Return From Interface Function Returning Null

I am attempting to work with the UE4 Interfaces in C++ and, it seems like this should all work, I get no errors or compiler issues but I am getting results that make no sense. This is a very simple interface that should return a component from a character.

   #include "CoreMinimal.h"
     #include "UObject/ObjectMacros.h"
     #include "UObject/Interface.h"
     #include "InventoryInterface.generated.h"
     
     class UInventoryComponent;
     class AEquipmentItem;
     
     // This class does not need to be modified.
     UINTERFACE(BlueprintType)
     class UInventoryInterface : public UInterface
     {
         GENERATED_BODY()
     };
     
     /**
      * 
      */
     class SWORDSNMAGICANDSTUFF_API IInventoryInterface
     {
         GENERATED_BODY()
     
     public:
         UFUNCTION(BlueprintCallable, BlueprintIMplementableEvent, Category = "Inventory")
         UInventoryComponent* GetInventoryComponent();
     };
 

and then in the .h file for my character...

 class SWORDSNMAGICANDSTUFF_API ASNMCombatCharacter : public ASNMCharacter, public IAbilitySystemInterface, public IInventoryInterface
 {
     GENERATED_BODY()
         
 public:
 UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category="Inventory")
             UInventoryComponent* GetInventoryComponent();
             virtual UInventoryComponent* GetInventoryComponent_Implementation();

and the .cpp

 UInventoryComponent* ASNMCombatCharacter::GetInventoryComponent_Implementation()
 {
     return InventoryComponent;
 }

Yet this always returns null, I am positive the class has the component which is stored in that variable.

This is where it is created in the parent class.

 InventoryComponent = CreateDefaultSubobject<UInventoryComponent>(TEXT("Player Inventory"));

I have tried several methods to pull this info through the interface and they all return null.

Product Version: UE 4.19
Tags:
more ▼

asked Jun 12 '18 at 04:37 AM in C++ Programming

avatar image

DeadlyMidnight
21 2 3 10

avatar image DeadlyMidnight Jun 12 '18 at 04:40 AM

Further testing shows it doesnt seem to actually be calling the function on the character class. Documentaion on Interfaces in C++ seem to be pretty contradictory to reality. When attempting to add override to this which is show in EVERY doc it fails to compile.

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

1 answer: sort voted first

In your implementation, you don't have to re-state the UFUNCTION macro nor the original getter... but you must do the _Implementation, like you have done. However, one thing you're missing is the override specifier at the end of your function declaration.

Perhaps something like:

 class SWORDSNMAGICANDSTUFF_API ASNMCombatCharacter : public ASNMCharacter, public IAbilitySystemInterface, public IInventoryInterface
  {
      GENERATED_BODY()
          
  public:
      virtual UInventoryComponent* GetInventoryComponent_Implementation() override;

I dropped the UFUNCTION and the getter, then added the override specifier at the end.

Good luck!

more ▼

answered Jun 12 '18 at 07:57 AM

avatar image

Jawdy
359 2 4 6

avatar image DeadlyMidnight Jun 12 '18 at 02:24 PM

ill give this a try!

avatar image DeadlyMidnight Jun 13 '18 at 12:28 AM

Part of the problem was a typo in the original code so it was not implementing the interface UFUNCTION properly, but outside of that this answer was correct, and provides a cleaner implementation with less funk.

(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