Reputation: 367
I have a "collector actor" containing two sub-actors, other actor 1 and 2. When I drag this ACollectionActor in the editor I have to manually select the subactors and toggle their mesh before they become visible. But, if I drag in the "OtherActor" in the editor the model shows up at once.
Does anyone know what goes wrong in my code?
And yes, "/Game/Models/other_model" works very fine below so no errors in path.
Thanks for anything...
CollectionActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "OtherActor.h"
#include "CollectionActor.generated.h"
UCLASS()
class IMPORTTEST_API ACollectionActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACollectionActorActor();
UPROPERTY()
USceneComponent* Root = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SubActor1", Meta = (MakeEditWidget = true))
AOtherActor* SubActor1 = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SubActor2", Meta = (MakeEditWidget = true))
AOtherActor* SubActor2 = nullptr;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
};
CollectionActor.cpp
#include "CollectionActor.h"
#include "OtherActor.h"
// Sets default values
ACollectionActor::ACollectionActor()
{
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = Root;
FAttachmentTransformRules l_Rules(EAttachmentRule::KeepRelative, false);
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
Mesh->AttachToComponent(Root, l_Rules);
SubActor1 = CreateDefaultSubobject<AOtherActor>(TEXT("Other1"));
SubActor1->AttachToActor(this, l_Rules);
SubActor1->SetActorLocation(FVector(-10, 0, 0));
SubActor2 = CreateDefaultSubobject<AOtherActor>(TEXT("Other2"));
SubActor2->AttachToActor(this, l_Rules);
SubActor2->SetActorLocation(FVector( 10, 0, 0));
}
// Called when the game starts or when spawned
void ACollectionActor::BeginPlay()
{
Super::BeginPlay();
}
OtherActor.cpp
#include "OtherActor.h"
// Sets default values
AOtherActor::AOtherActor()
{
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = Root;
Mesh = CreateDefaultSubobject< UStaticMeshComponent>("Mesh");
Mesh->AttachTo(Root);
const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/Models/other_model"));
Mesh->SetStaticMesh(MeshObj.Object);
}
// Called when the game starts or when spawned
void AOtherActor::BeginPlay()
{
Super::BeginPlay();
}
OtherActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "OtherActor.generated.h"
UCLASS()
class IMPORTTEST_API AOtherActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AOtherActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UPROPERTY()
USceneComponent* Root = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UStaticMeshComponent* Mesh = nullptr;
};
Upvotes: 2
Views: 183
Reputation:
AActor
expects to be ticked and drawn by the world.
Components depend on their parents for this functionality.
The ComponentTick != Tick, not literal code, but displays the concept, and other events, so the Child actor is not being updated in game.
By modifying the property, you are forcing the Actor update to occur in the editor(Object refresh on property change) and that works.
The actors need to be defined as a UChildActorComponent
to provide the proper event/callback wiring for an AActor
to be used as a component.
Upvotes: 2