Announcement

Collapse
No announcement yet.

ConstructorHelpers::FObjectFinder doesn't load the right static mesh when I use inheritance

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    ConstructorHelpers::FObjectFinder doesn't load the right static mesh when I use inheritance

    Hello,

    I have some troubles to understand how inheritance on the UE4 works.

    I want to spawn different planets on my level. I created a general "APlanet" class and a few other classes inheriting from APlanet (APlanetEarth, AMoonLikePlanet...)

    My problem : CreateDefaultSubobject doesn't load the right static mesh, it always loads the same static mesh for all my classes inheriting from APlanet

    Here's my simplified code

    APlanet.h
    Code:
    #include "APlanet.generated.h"
    
    UCLASS()
    class API APlanet : public AActor
    {
        GENERATED_BODY()
    
    public:
        APlanet();
        ~APlanet();
    
        void BeginPlayAPlanet();
    
        void Init(const TCHAR* planetMesh);
    };
    APlanet.cpp
    Code:
    APlanet::APlanet()
    {
        PrimaryActorTick.bCanEverTick = false;
    }
    
    void APlanet::Init(const TCHAR* planetMeshStr)
    {
         Logger::Info("Init : " + FString(planetMeshStr)); //Here I checked if the right path is send to Init, the path seems to be correct
         static ConstructorHelpers::FObjectFinder planetMesh(planetMeshStr);
    
         _planet = CreateDefaultSubobject(planetMeshStr);
         _planet->SetStaticMesh(planetMesh.Object);
         Logger::Info( _planet->GetStaticMesh()->GetPathName()); //The displayed path is different from the path send as parameter
         // ...
    }
    And here's an example of a class inheriting from APlanet, All classes inheriting from APlanet are written the same way, only the path changes :

    APlanetHot.h
    Code:
    #include "APlanet.h"
    #include "APlanetHot.generated.h"
    
    UCLASS()
    class API APlanetHot : public APlanet
    {
       GENERATED_BODY()
    
       public:
          APlanetHot();
          ~APlanetHot();
    
       protected:
          virtual void BeginPlay() override;
    };
    ​​​​​​​APlanetHot.cpp
    Code:
    APlanetHot::APlanetHot()
    {
        PrimaryActorTick.bCanEverTick = false;
        Init(TEXT("StaticMesh '/Game/long path/Hot.Hot'"));
        //Here I am calling the Init method of the parent class, The parameter is the path of the static mesh I want to display
    }
    
    APlanetHot::~APlanetHot()
    {
    }
    
    void APlanetHot::BeginPlay()
    {
        Super::BeginPlay();
    
        BeginPlayAPlanet();
    }
    When I place APlanetHot on my level, the wrong static mesh gets loaded, instead of loeading the Static Mesh "Hot", another one gets loaded, and it's always the same static mesh that gets loaded : the one from the class APlanetEarth, and when I change the path of the static mesh in APlanetEarth, no static mesh gets loaded anymore for all planets.

    What's the issue?

    Thanks in advance.
    Last edited by MyroG; 01-20-2019, 04:30 PM.

    #2
    Remove the word static from the line:
    Code:
    static ConstructorHelpers::FObjectFinder planetMesh(planetMeshStr);
    In your APlanet::Init function;

    HTH

    Comment


      #3
      Thanks for your reply, but sadly this didn't solved my issue, nothing changed, I also recompiled my project just to be sure.

      EDIT : Nevermind, this solved my issue, it worked after I pressed "Compile" via the UE4 interface, for some reason compiling through VS didn't worked, and no error got displayed in the output log, it worked nicely before

      Thank you very much !

      EDIT 2 : I am sorry, the problem was not related to the static keyword, it was the VS compiler.... I added the static keyword again, changed the log text just to be sure that the changes are accepted, and it still works nicely
      Last edited by MyroG; 01-21-2019, 04:45 PM.

      Comment


        #4
        Hi MyroG,
        I tested and it works fine for me.

        Here is my base Planet class declaration for the test:

        Code:
        UCLASS()
        class EDEX_API APlanet : public AActor
        {
            GENERATED_BODY()
        
        public:    
        
            APlanet();
        
        protected:
            void Init(const TCHAR* MeshAssetReference);
        
            class UStaticMeshComponent* PlanetMeshComponent;
        
        };
        My base class constructor:

        Code:
        APlanet::APlanet()
        {
            RootComponent = CreateDefaultSubobject(TEXT("RootComponent"));
            PlanetMeshComponent = CreateDefaultSubobject(TEXT("PlanetMeshComponent"));
            PlanetMeshComponent->SetupAttachment(RootComponent);
        }
        My base class Init function:

        Code:
        void APlanet::Init(const TCHAR * MeshAssetReference)
        {
            ConstructorHelpers::FObjectFinder MeshAsset(MeshAssetReference);
            PlanetMeshComponent->SetStaticMesh(MeshAsset.Object);
        }
        I then created 2 Planet Subclasses APlanetType1 and APlanetType2 and simply called the Init function from there constructors with different asset references:

        Code:
        APlanetType1::APlanetType1()
        {
            Init(TEXT("StaticMesh'/Game/Meshes/SM_Planet1.SM_Planet1'"));
        }
        Code:
        APlanetType2::APlanetType2()
        {
            Init(TEXT("StaticMesh'/Game/Meshes/SM_Planet2.SM_Planet2'"));
        }
        HTH

        Edit: Angle brackets in my code have been removed by forum software making it incorrect, sorry.

        Anybody know how to post code and retain angle brackets?

        EDIT 2: Can't seem to post a link to files either, sorry man !
        Last edited by splodginald; 01-21-2019, 04:57 PM.

        Comment


          #5
          Hi spodginald, thanks for your reply,

          When I edited my previous comment I didn't saw your reply, I solved the issue, it was a bug related to VS (see my edits).

          Thanks again, and have a nice day.
          Last edited by MyroG; 01-21-2019, 04:50 PM.

          Comment


            #6
            Hi Myro, that's good to know.
            I Sent you a private message with link to zip containing my test files if you're interested.

            All the best.
            Last edited by splodginald; 01-21-2019, 05:05 PM. Reason: grammer

            Comment

            Working...
            X