I describe the problem immediately with a minimal example.
Initially, I have the following.
class USceneComponent;
class UStaticMeshComponent;
UCLASS()
class MY_API AMyActor : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleDefaultsOnly, Category = Components)
UStaticMeshComponent* MeshComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Components, meta = (AllowPrivateAccess = true))
USceneComponent* SceneComponent;
public:
AMyActor();
};
AMyActor::AMyActor()
{
PrimaryActorTick.bCanEverTick = false;
SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
SetRootComponent(SceneComponent);
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetupAttachment(SceneComponent);
}
In this case, when I move the actor, the mesh moves with it, everything is ok, good.
The hierarchy in the blueprint editor is as follows:
RootComponent
-MeshComponent
I decided to split it into several components (USceneComponent), in this case I just moved the mesh into an intermediate component. Got the following:
//MyComponent
class UStaticMeshComponent;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MY_API UMyComponent : public USceneComponent
{
GENERATED_BODY()
UPROPERTY(VisibleDefaultsOnly, Category = Components)
UStaticMeshComponent* MeshComponent;
public:
UMyComponent();
};
UMyComponent::UMyComponent()
{
PrimaryComponentTick.bCanEverTick = false;
MeshComponent = CreateDefaultSubobject<UGeometryComponent>(TEXT("MeshComponent"));
MeshComponent->SetupAttachment(this);
}
//MyActor
class USceneComponent;
class UMyComponent;
UCLASS()
class MY_API AMyActor : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Components, meta = (AllowPrivateAccess = true))
UMyComponent* MyComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Components, meta = (AllowPrivateAccess = true))
USceneComponent* SceneComponent;
public:
AMyActor();
};
AMyActor::AMyActor()
{
PrimaryActorTick.bCanEverTick = false;
SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
SetRootComponent(SceneComponent);
MyComponent = CreateDefaultSubobject<UMyComponent>(TEXT("MyComponent"));
MyComponent->SetupAttachment(SceneComponent);
}
The hierarchy in the blueprint editor is as follows:
RootComponent
-MyComponent
--MeshComponent
And after such a separation, when the actor moved at the level or my component in the blueprint editor, the mesh stopped moving, it feels like it has switched to the world coordinate system.
Another error periodically began to appear (only with one, the last created component)
Ensure condition failed: false [File:D:\build\++UE5\Sync\Engine\Source\Runtime\Engine\Private\Components\SceneComponent.cpp] [Line: 2004]
Error LogOutputDevice Template Mismatch during attachment. Attaching instanced component to template component. Parent 'HelpComponent' (Owner 'Default__BP_MyActor_C') Self 'SplineComponent' (Owner 'BP_MyActor_C_0').
I got 2 components, in one there are only UInstancedStaticMeshComponent and UStaticMeshComponent inside, in the other USplineComponent and UNiagaraComponent as subcomponents. The only уerror is where there are no meshes, but they stopped traveling too with their parents.
There are suspicions that errors are connected. Back to combine everything in one is not an option, it turns out to be too big a class and mixed warm with soft.
How to fix the error and get the actor component moving back together with the actor itself?