1
\$\begingroup\$

I have made a class that is derived from USceneComponent which contains 4 references for itself and two UStaticMeshComponents. Simply I am trying to make a tree structure. When I first create actor (thus my component) it works flawlessly. But when I give it the command to create its children, they do not register somehow (Or I just cannot see them, been working on this case for hours now). When I did this by deriving my class from UStaticMeshComponent, it just worked with the same code. However I definitely need a Scene Component for my needs. Here is the relevant bits of my code

.h

public: 
    // Sets default values for this component's properties
    UModifiableWall();

    // Called when the game starts
    virtual void BeginPlay() override;

    // Called every frame
    virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UModifiableWall* LeftWall;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UModifiableWall* RightWall;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UModifiableWall* TopWall;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UModifiableWall* BottomWall;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UAutoRegisterSMComponent* PlacedObject;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UAutoRegisterSMComponent* MainWall;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    bool IsBrokenDown;

    UPROPERTY(BlueprintReadWrite, Category = Wall)
    UMaterialInstanceDynamic* MaterialInstance;

    UFUNCTION(BlueprintCallable, Category = Wall)
    void Initialize(UStaticMesh* Mesh, FVector Location, FVector WorldScale);

    UFUNCTION(BlueprintCallable, Category = Wall)
    void PlaceObject(UStaticMesh* ObjectMesh, FVector ObjectWorldLocation, UModifiableWall* NewLeftWall, UModifiableWall* NewRightWall, UModifiableWall* NewTopWall, UModifiableWall* NewBottomWall);

    UFUNCTION(BlueprintCallable, Category = Wall)
    UModifiableWall* CheckForPlaceObject(UStaticMesh* ObjectMesh, FVector ObjectWorldLocation);

.cpp

UModifiableWall::UModifiableWall()
{
    // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
    // off to improve performance if you don't need them.
    bWantsBeginPlay = true;
    PrimaryComponentTick.bCanEverTick = true;

    IsBrokenDown = false;

    MainWall = CreateDefaultSubobject<UAutoRegisterSMComponent>(FName("MainWall"));
    PlacedObject = CreateDefaultSubobject<UAutoRegisterSMComponent>(FName("PlacedObject"));

    MainWall->AttachTo(this);
    PlacedObject->AttachTo(this);

    bAutoActivate = true;
    bAutoRegister = true;
}

void UModifiableWall::PlaceObject(UStaticMesh* ObjectMesh, FVector ObjectWorldLocation, UModifiableWall* NewLeftWall, UModifiableWall* NewRightWall, UModifiableWall* NewTopWall, UModifiableWall* NewBottomWall)
{
    FVector LocalLocation = UKismetMathLibrary::InverseTransformLocation(this->GetComponentTransform(), ObjectWorldLocation);
    FVector ObjectSize = ObjectMesh->GetBounds().GetBox().GetSize();

    float XProjected = LocalLocation.X + (50 * MainWall->GetComponentScale().X);
    float ZProjected = LocalLocation.Z + (50 * MainWall->GetComponentScale().Z);

    //LeftWall

    float LeftXScale = (XProjected - ObjectSize.X / 2) / 100;
    FVector LeftWallScale(LeftXScale, MainWall->GetComponentScale().Y, MainWall->GetComponentScale().Z);
    FVector LeftWallLocation((LeftXScale - MainWall->GetComponentScale().X) * 50, 0, 0);

    LeftWall = NewLeftWall;
    LeftWall->AttachTo(this);
    LeftWall->Initialize(MainWall->StaticMesh, LeftWallLocation, LeftWallScale);
    LeftWall->SetVisibility(true);

    //RightWall

    float RightXScale = (MainWall->GetComponentScale().X * 100 - (XProjected + ObjectSize.X / 2)) / 100;
    FVector RightWallScale(RightXScale, MainWall->GetComponentScale().Y, MainWall->GetComponentScale().Z);
    FVector RightWallLocation((MainWall->GetComponentScale().X * 50) - (RightXScale * 50), 0, 0);

    RightWall = NewRightWall;
    RightWall->AttachTo(this);
    RightWall->Initialize(MainWall->StaticMesh, RightWallLocation, RightWallScale);
    RightWall->SetVisibility(true);

    //BottomWall

    float BottomZScale = (ZProjected - ObjectSize.Z / 2) / 100;
    FVector BottomWallScale((MainWall->GetComponentScale() - LeftWall->GetComponentScale() - RightWall->GetComponentScale()).X, MainWall->GetComponentScale().Y, BottomZScale);
    FVector BottomWallLocation(LocalLocation.X, 0, -(MainWall->GetComponentScale().Z * 50 - BottomZScale * 50));

    BottomWall = NewBottomWall;
    BottomWall->AttachTo(this);
    BottomWall->Initialize(MainWall->StaticMesh, BottomWallLocation, BottomWallScale);
    BottomWall->SetVisibility(true);

    //TopWall

    float TopZScale = (MainWall->GetComponentScale().Z * 100 - (ZProjected + ObjectSize.Z / 2)) / 100;
    FVector TopWallScale((MainWall->GetComponentScale() - LeftWall->GetComponentScale() - RightWall->GetComponentScale()).X, MainWall->GetComponentScale().Y, TopZScale);
    FVector TopWallLocation(LocalLocation.X, 0, (MainWall->GetComponentScale().Z * 50 - TopZScale * 50));

    TopWall = NewTopWall;
    TopWall->AttachTo(this);
    TopWall->Initialize(MainWall->StaticMesh, TopWallLocation, TopWallScale);
    TopWall->SetVisibility(true);

    //Object
    PlacedObject->AttachTo(this);
    PlacedObject->SetStaticMesh(ObjectMesh);
    PlacedObject->SetRelativeLocation(LocalLocation + FVector(0, 0, 0));
    PlacedObject->SetWorldScale3D(FVector(1, 1, 1));

    MainWall->SetVisibility(false);

    Activate();

    IsBrokenDown = true;
}

The PlaceObject function takes Components that is created earlier just for trying purposes (Before I used NewObject and I am 250% sure that I used RegisterComponent() afterwards, and I am using it when I am creating these parameter ones as well).

In need of some help, after a long time, I have tossed on something... Thanks in advance.

Edit: UAutoRegisterSMComponent is a class derived from UStaticMeshComponent that only has bAutoActivate = true on its constructor. Using UStaticMeshComponent did not help too.

TLDR: Stuff get rendered when the component is first created on the actor with CreateDefaultSubobject, but does not register when created dynamically with NewObject with RegisterComponent().

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.