AddPaperSpriteComponent Blueprint Method on C++

Hi Everyone!

First of all thank you for all the help you can provide.
I’m following these series of tutorials from UnrealEngine [Creating a 2D Side-Scroller p.4][1] but translating it to C++. The reason, well… I haven’t found that many beginners tutorials on C++ so I took the ones on Blueprint and on my own started translating those. In this part of the tutorial he creates on the Construction Script a ladder with a variable amount of steps which you can increase or decrease just by dragging a Vector Widget. So far so good, I manage to make almost the same on C++ with the difference that the steps are not being deleted from the actor after a modification, instead it just keeps creating them, and when I try to decrease the size, for the reason I just explained, the steps aren’t deleted.

Even if i put just one step on the ladder and just drag the whole object around, at the moment of release it you can see that it has a lot of steps on the same location of the first one on top of the other.

So far the only difference between my code and the blueprint is the call to AddPaperSpriteComponent, I’m just using NewObject.

void ALadder::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);

	FVector DeltaLadder = LadderTop - LadderBottom;
	int32 lastIndex = FMath::TruncToInt((DeltaLadder.Z / RungOffset));

	for (int32 i = 0; i < lastIndex; ++i)
	{
		UPaperSpriteComponent* Step = NewObject<UPaperSpriteComponent>(this, UPaperSpriteComponent::StaticClass());
		if (Step)
		{
			Step->RegisterComponent();
			Step->AttachTo(this->RootComponent);
			Step->SetRelativeLocation(FVector(0.0f, 0.0f, (i * RungOffset)));
			Step->SetSprite(Sprite);
			Step->SetCollisionProfileName(TEXT("NoCollision"));
		}
	}

	TriggerBox->SetRelativeScale3D(FVector(1.0f, 1.0f, (DeltaLadder.Z / 2.0f)));
	TriggerBox->SetRelativeLocation(FVector(0.0f, 0.0f, (DeltaLadder.Z / 2.0f)));

	Mesh->SetRelativeScale3D(FVector(CollisionWidth, 20.0f, 1.0f));
	Mesh->SetRelativeLocation(LadderTop);
}

And here is the Blueprint version, which work perfectly.

http://prntscr.com/ayxw4e

How can I achieve that behavior? What am I missing?
As far as I can tell, the only difference is that I’m calling NewObject and not AddPaperSpriteComponent and that’s because I think it doesn’t exist (I couldn’t find it).

Well, I found a solution myself that is working for me now, but I’d like to know someone’s opinion if there is a straight forward way to do this other than my solution.

Here’s the code in case someone has the same problem.

void ALadder::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);

	FVector DeltaLadder = LadderTop - LadderBottom;
	int32 lastIndex = FMath::TruncToInt((DeltaLadder.Z / RungOffset));

	for (UPaperSpriteComponent* Step : Steps)
	{
		Step->UnregisterComponent();
		Step->DestroyComponent();
	}
	Steps.Empty();

	FString StepName;
	for (int32 i = 0; i < lastIndex; ++i)
	{
		StepName = "Step_" + FString::FromInt(i);
		UPaperSpriteComponent* Step = NewObject<UPaperSpriteComponent>(this, UPaperSpriteComponent::StaticClass(), FName(*StepName));
		if (Step)
		{
			Step->RegisterComponent();
			Step->AttachTo(this->RootComponent);
			Step->SetRelativeLocation(FVector(0.0f, 0.0f, (i * RungOffset)));
			Step->SetSprite(Sprite);
			Step->SetCollisionProfileName(TEXT("NoCollision"));
			Steps.Add(Step);
		}
	}

	TriggerBox->SetRelativeScale3D(FVector(1.0f, 1.0f, (DeltaLadder.Z / 2.0f)));
	TriggerBox->SetRelativeLocation(FVector(0.0f, 0.0f, (DeltaLadder.Z / 2.0f)));

	Mesh->SetRelativeScale3D(FVector(CollisionWidth, 20.0f, 1.0f));
	Mesh->SetRelativeLocation(LadderTop);
}

And Steps:

	TArray<class UPaperSpriteComponent *> Steps;

Is a private array that I delete and re-create every time the method is called.

Thanks, this answer gave my an idea on how to make AddPaperSpriteComponent in C++.