Hey there,
similar to the Spline creation in Blueprints, i create my SplineMeshs along a Spline in my OnConstruction(…) function. This is working for the part where i can create splines before the game is starting (making the creation of different trees easier).
The problem is, that these Components are all deleted once i start the Game. I can see the actor in the Outliner, but it has no Components (despite the SplineComponent that is created outside of the OnConstruction(…)).
Here is the cpp Code of my Class. Please tell me if i’m missing some function or variable.
// Fill out your copyright notice in the Description page of Project Settings.
#include "UnknownProject.h"
#include "Spawner_Tree.h"
void ASpawner_Tree::OnConstruction(const FTransform & Transform)
{
Super::OnConstruction(Transform);
NumberOfSplinePoints = Spline->GetNumSplinePoints();
// Adding and removing entries in the BranchPointData Array based on the number of spline points
if (NumberOfSplinePoints > BranchPointData.Num())
{
FBranchData TempData;
BranchPointData.Add(TempData);
}
else if (NumberOfSplinePoints < BranchPointData.Num())
{
BranchPointData.RemoveAt(BranchPointData.Num() - 1);
}
// Calling the method to create SplineMeshs along the Spline
for (int z = 0; z < NumberOfSplinePoints - 1; z++)
{
if (z == NumberOfSplinePoints - 2 && bHasEndPoint)
{
CreateSplineMesh(z, EndMesh);
}
else
{
CreateSplineMesh(z, MainMesh);
}
}
// Creating the leafs mesh if the bool is true
if (bHasLeafs && NumberOfSplinePoints > 0)
{
UStaticMeshComponent* TempStaticMesh = ConstructObject<UStaticMeshComponent>(UStaticMeshComponent::StaticClass(), this);
TempStaticMesh->SetStaticMesh(LeafMesh);
TempStaticMesh->SetWorldLocation(Spline->GetWorldLocationAtSplinePoint(NumberOfSplinePoints - 1));
TempStaticMesh->SetWorldScale3D(LeafScale);
TempStaticMesh->SetWorldRotation(LeafRot);
TempStaticMesh->CreationMethod = EComponentCreationMethod::SimpleConstructionScript;
RegisterAllComponents();
}
}
void ASpawner_Tree::CreateSplineMesh(int32 _LoopIndex, UStaticMesh* _SplineMesh)
{
int32 NextLoopIndex = _LoopIndex + 1;
FVector CurrentPointPosition, NextPointPosition, CurrentPointTangent, NextPointTangent;
Spline->GetLocalLocationAndTangentAtSplinePoint(_LoopIndex, CurrentPointPosition, CurrentPointTangent);
Spline->GetLocalLocationAndTangentAtSplinePoint(NextLoopIndex, NextPointPosition, NextPointTangent);
FVector2D StartScale, EndScale;
StartScale.X = BranchPointData[_LoopIndex].PointWidth;
StartScale.Y = BranchPointData[_LoopIndex].PointHeight;
if (BranchPointData.IsValidIndex(NextLoopIndex))
{
EndScale.X = BranchPointData[NextLoopIndex].PointWidth;
EndScale.Y = BranchPointData[NextLoopIndex].PointHeight;
}
USplineMeshComponent* SplineMesh = ConstructObject<USplineMeshComponent>(USplineMeshComponent::StaticClass(), this);
SplineMesh->SetMobility(EComponentMobility::Movable);
SplineMesh->AttachParent = Spline;
SplineMesh->bCastDynamicShadow = true;
SplineMesh->SetStaticMesh(_SplineMesh);
SplineMesh->SetStartScale(StartScale);
SplineMesh->SetEndScale(EndScale);
SplineMesh->SetStartAndEnd(CurrentPointPosition, CurrentPointTangent, NextPointPosition, NextPointTangent);
SplineMesh->CreationMethod = EComponentCreationMethod::UserConstructionScript;
SplineMesh->ForwardAxis = ESplineMeshAxis::Z;
RegisterAllComponents();
}
ASpawner_Tree::ASpawner_Tree()
{
Spline = CreateDefaultSubobject<USplineComponent>(FName("Spline"));
RootComponent = Spline;
bReplicates = true;
}
EDIT: The only “workaround” so far is, calling “OnConstruction” in the BeginPlay() again