I created a sub component within a component. Then chain their changes together.
// in constructor
subSM = CreateDefaultSubobject<UStaticMeshComponent>("Sub SM");
subSM->SetupAttachment(this);
SetRelativeLocation(FVector(0, 0, 0));
subSM->SetRelativeLocation(FVector(200, 0, 0));
...
// in PostEditChangeProperty
SetRelativeLocation(FVector(0, 0, testHeight*100));
subSM->SetRelativeLocation(FVector(200, 0, testHeight*100));
SetRelativeScale3D(FVector(testScale));
subSM->SetRelativeScale3D(FVector(testScale));
The sub component is supposed to change with the outer component.
So, for example, if I set the outer’s scale to 2, the subcomponent’s effective scale should be 4.
However, upon releasing mouse, the subcomponent’s values automatically revert back to default…
GIF:
Where did I do wrong??
Code:
#pragma once
#include "CoreMinimal.h"
#include "Components/StaticMeshComponent.h"
#include "SubSMCompTest.generated.h"
/**
*
*/
UCLASS(Blueprintable, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class DLCOREUTIL_API USubSMCompTest : public UStaticMeshComponent
{
GENERATED_BODY()
private:
UStaticMeshComponent *subSM;
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (UIMin = "0.0", UIMax = "2.0"), Category = "Test")
float testScale;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (UIMin = "0.0", UIMax = "2.0"), Category = "Test")
float testHeight;
USubSMCompTest();
virtual void PostInitProperties() override;
void UpdateSM();
#if WITH_EDITOR
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
};
cpp:
#include "SubSMCompTest.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
USubSMCompTest::USubSMCompTest()
: testScale(1), testHeight(0){
subSM = CreateDefaultSubobject<UStaticMeshComponent>("Sub SM");
subSM->SetupAttachment(this);
SetRelativeLocation(FVector(0, 0, 0));
subSM->SetRelativeLocation(FVector(200, 0, 0));
static ConstructorHelpers::FObjectFinder<UStaticMesh> conemesh(TEXT("StaticMesh'/Engine/BasicShapes/Cone.Cone'"));
SetStaticMesh(conemesh.Object);
subSM->SetStaticMesh(conemesh.Object);
SetMobility(EComponentMobility::Movable);
subSM->SetMobility(Mobility);
UpdateSM();
}
void USubSMCompTest::PostInitProperties() {
Super::PostInitProperties();
UpdateSM();
}
#if WITH_EDITOR
void USubSMCompTest::PostEditChangeProperty(FPropertyChangedEvent& e) {
Super::PostEditChangeProperty(e);
UpdateSM();
}
#endif
void USubSMCompTest::UpdateSM() {
SetRelativeLocation(FVector(0, 0, testHeight*100));
subSM->SetRelativeLocation(FVector(200, 0, testHeight*100));
SetRelativeScale3D(FVector(testScale));
subSM->SetRelativeScale3D(FVector(testScale));
}