hi,
i’m new to unreal engine (coming from unity). just trying to create some simple item pickup system.
therefore i created a pickup base class with a UStaticMeshComponent as RootComponent and a UShapeComponent as the trigger.
now i want to accomplish that i can select what type of UShapeComponent i like to use.
so i defined an enum with my possible shape values. then in PostEditChangeProperty i check if the TriggerShape has been changed and if so
i create a new Trigger and attach it to my root component.
then i create a blueprint based on my pickup. when i change now the value in the blueprint editor… the unreal editor crashes.
what am i doing wrong? or how should i implement the behaviour i like to achieve?
// Header
UENUM(BlueprintType)
enum class ETriggerShape : uint8
{
S_Box UMETA(DisplayName = "Box"),
S_Sphere UMETA(DisplayName = "Sphere"),
S_Max UMETA(Hidden)
};
UCLASS()
class APickup : public AActor
{
GENERATED_BODY()
public:
APickup();
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Pickup")
ETriggerShape TriggerShape;
protected:
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Pickup")
class UShapeComponent* Trigger;
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Pickup")
class UStaticMeshComponent* Mesh;
private:
virtual void PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent) override;
};
// Cpp
APickup::APickup()
{
PrimaryActorTick.bCanEverTick = false;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
RootComponent = Mesh;
Trigger = CreateDefaultSubobject<UBoxComponent>(TEXT("Trigger"));
Trigger->AttachTo(RootComponent);
}
void APickup::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent)
{
FName propertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
if (propertyName == GET_MEMBER_NAME_CHECKED(APickup, TriggerShape))
{
Trigger->DetachFromParent();
Trigger->DestroyComponent();
if (TriggerShape == ETriggerShape::S_Box)
Trigger = NewObject<UBoxComponent>(this, TEXT("Trigger"));
else
Trigger = NewObject<USphereComponent>(this, TEXT("Trigger"));
Trigger->AttachTo(RootComponent);
}
Super::PostEditChangeProperty(PropertyChangedEvent);
}