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
#include "APlanet.generated.h"
UCLASS()
class API APlanet : public AActor
{
GENERATED_BODY()
public:
APlanet();
~APlanet();
void BeginPlayAPlanet();
void Init(const TCHAR* planetMesh);
};
APlanet.cpp
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
#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
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.