# Component added dynamically disappears in PIE

**URL:** <https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 21, 2015, 2:04am UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587 "2015-02-21T02:04:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dozeworthy\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dozeworthy\_1](https://forums.unrealengine.com/u/Dozeworthy_1)\
**Post date:** [February 21, 2015, 2:04am UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587/1 "2015-02-21T02:04:22Z")

</div>

I’m trying to create a custom component based on [this tutorial video][1] in order to facilitate level design. I basically want to be able to add it to a class and have it clone all scene components that are children of the root, offsetting them until an endpoint is hit.

To go from this:

[https://forums.unrealengine.com/attachment.php?attachmentid=26271&d=1424419462](https://forums.unrealengine.com/attachment.php?attachmentid=26271&d=1424419462)

to this:

[https://forums.unrealengine.com/attachment.php?attachmentid=26272&d=1424419528](https://forums.unrealengine.com/attachment.php?attachmentid=26272&d=1424419528)

and duplicate any meshes, shapes, etc.

It seems to work well enough in the Blueprint Component window, but when I place it in the scene and play in editor, all the added components disappear.

Stopped:

[https://forums.unrealengine.com/attachment.php?attachmentid=26273&d=1424419797](https://forums.unrealengine.com/attachment.php?attachmentid=26273&d=1424419797)

Playing:

[https://forums.unrealengine.com/attachment.php?attachmentid=26274&d=1424419817](https://forums.unrealengine.com/attachment.php?attachmentid=26274&d=1424419817)

This seems to clear up when I save the blueprint, or sometimes for seemingly no reason, and comes back when I change any properties of the wall.

More importantly, any added collision boxes disappear on play:

Stopped:

[https://forums.unrealengine.com/attachment.php?attachmentid=26275&d=1424420115](https://forums.unrealengine.com/attachment.php?attachmentid=26275&d=1424420115)

Playing:

[https://forums.unrealengine.com/attachment.php?attachmentid=26276&d=1424420126](https://forums.unrealengine.com/attachment.php?attachmentid=26276&d=1424420126)

which I can’t find any way to fix.

Here’s the code for the component:

```
    void UProceduralWallComponent::GenerateWall(FVector Endpoint)
{
	//Super::OnComponentCreated();

	AActor* owner = GetOwner();
	USceneComponent* root = NULL;

	if (owner)
		root = owner->GetRootComponent();

	if (!root)
		return;

	if (root->GetNumChildrenComponents() < 1)
		return;

	USceneComponent* child = root->GetChildComponent(0);

	float wallLength = Endpoint.Size();
	float width = child->Bounds.BoxExtent.Y*2/child->GetComponentScale().Y;
	FVector currentPoint = child->RelativeLocation;
	currentPoint.Y += width;

	TArray<USceneComponent*> clones;

	//clone first child of the root
	for (int i = 0; currentPoint.Y < wallLength; currentPoint.Y += width, ++i)
	{
		USceneComponent* currentComp = CloneComponent(child->GetClass(), child->GetName() + FString::FromInt(i), child, root);
		if (!currentComp)
			return;

		currentComp->SetRelativeLocation(currentPoint);
		clones.Add(currentComp);
	}

	//clone all children of the first child recursively
	CloneRecursive(child, clones);
}

void UProceduralWallComponent::CloneRecursive(USceneComponent* parent, TArray<USceneComponent*> &parentClones)
{
	TArray<USceneComponent*> children;
	parent->GetChildrenComponents(false, children);
	if (children.Num() == 0)
		return;

	for (USceneComponent* child : children)
	{
		int i = 0;
		TArray<USceneComponent*> childClones;
		for (USceneComponent* parentClone : parentClones)
		{
			USceneComponent* clone = CloneComponent(child->GetClass(), child->GetName() + FString::FromInt(i), child, parentClone);
			childClones.Add(clone);
			if (GEngine)
				GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, clone->GetName());
			++i;
		}
		CloneRecursive(child, childClones);
	}
}

USceneComponent* UProceduralWallComponent::CloneComponent(TSubclassOf<USceneComponent> CompClass, FString CompName, UObject* Template, USceneComponent* parent)
{
	FName name(*CompName);

	USceneComponent* newComp = ConstructObject<USceneComponent>(CompClass, GetOwner(), name, RF_NoFlags, Template);

	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, newComp->GetClass()->GetName());

	if (!newComp)
		return NULL;

	newComp->bCreatedByConstructionScript = true;
	newComp->OnComponentCreated();
	newComp->RegisterComponent(); 
	if (newComp->bWantsInitializeComponent)
		newComp->InitializeComponent();

	if (parent->GetOwner())
		parent->GetOwner()->AddOwnedComponent(newComp);

	newComp->AttachTo(parent);

	return newComp;
}

```

and the Blueprint calling it:

![30723-2015-02-21+23_16_44-bustablewall.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/8/0/b804d4ef5a99dbef0884ae616659f311d78dc62f.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_eb8faa9d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_eb8faa9d](https://forums.unrealengine.com/u/anonymous_user_eb8faa9d)\
**Post date:** [February 21, 2015, 5:02am UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587/2 "2015-02-21T05:02:30Z")

</div>

I’m not sure if this is the answer to your problem, but I don’t think you’re supposed to be doing anything complex in the constructor. The constructors are invoked all the time in the editor. Do you have the same problem if you move your constructor logic to the BeginPlay event?

---

<div class="post-metadata">

**Author:** ![Dozeworthy\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dozeworthy\_1](https://forums.unrealengine.com/u/Dozeworthy_1)\
**Post date:** [February 21, 2015, 5:40am UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587/3 "2015-02-21T05:40:27Z")

</div>

If I move it to BeginPlay, the wall has gaps in for some reason, but more importantly, the purpose of the component is to make walls easier to build in the editor, so it needs to execute at construction time for any changes to be visible. It runs fast enough, so complexity isn’t the issue, and it isn’t conceptually much different to the video I linked, just more flexible.

---

<div class="post-metadata">

**Author:** ![zamy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zamy](https://forums.unrealengine.com/u/zamy)\
**Post date:** [February 22, 2015, 2:32pm UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587/4 "2015-02-22T14:32:46Z")

</div>

As far as i recall:

- it’s quite normal that the effect vanishes when you rebuild the BP. It gets then any modification to the actor to have the Costruction Script get fired again.
- you can’t have more than one collision box per component, even if not per actor (but maybe i’m wrong). If this is the case, you’ll have to adapt and stretch your collision box to the new size.

---

<div class="post-metadata">

**Author:** ![Dozeworthy\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dozeworthy\_1](https://forums.unrealengine.com/u/Dozeworthy_1)\
**Post date:** [February 22, 2015, 11:58pm UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587/5 "2015-02-22T23:58:36Z")

</div>

- It doesn’t vanish when rebuilding, the construction runs fine every time in the editor. It’s just more likely for components to disappear when the level isn’t saved, which doesn’t happen for any other construction scripts I’m running.
- I’m added additional components for each collision box, and I already have multiple blueprints with multiple collision boxed on them, and those work fine.

---

<div class="post-metadata">

**Author:** ![Dozeworthy\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dozeworthy\_1](https://forums.unrealengine.com/u/Dozeworthy_1)\
**Post date:** [February 23, 2015, 1:36am UTC](https://forums.unrealengine.com/t/component-added-dynamically-disappears-in-pie/307587/6 "2015-02-23T01:36:56Z")

</div>

OK, looks like the problem was that the procedural components weren’t being serialized. I just called the FinishAndRegisterComponent() method of the owning actor to fix it, instead of RegisterComponent(). Alternatively, making a UProperty TArray of USceneComponent pointers and adding all the procedural components to it also worked.
