Hello,
I am trying to make a simple wargame unit, unit should contains number of animated skeleton meshes with weapons etc. so it’s not only the one model, so I decide to use an actor for that. This Character actor should be used for main unit formation - 10 characters in 2 lines. Manually spawn this characters is a problem, so I decide to spawn them in unit actor OnConstruction()
void AGameSceneUnit::OnConstruction(const FTransform& Transform)
{
GenerateUnit();
}
// generate unit
bool AGameSceneUnit::GenerateUnit()
{
// if(UnitCharacters.Num() != 0)
// return true;
FTransform initTranform;
initTranform = FTransform::Identity;
FActorSpawnParameters SpawnParameters;
SpawnParameters.Owner = this;
// formation is a component
auto components = GetComponentsByClass(UGameSceneUnitSquad::StaticClass());
for(auto& formation : components)
{
UGameSceneUnitSquad* squad = (UGameSceneUnitSquad*)formation;
squad->InitFormation();
UnitFormations.Push(squad);
}
if(UnitCharacterClass == nullptr)
{
UE_LOG(Wargame, Error, TEXT("AGameSceneUnit::GenerateUnit: no character class."));
return false;
}
if(UnitFormations.Num() == 0)
{
UE_LOG(Wargame, Error, TEXT("AGameSceneUnit::GenerateUnit: no formations."));
return false;
}
auto attachmentRule = FAttachmentTransformRules(EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, EAttachmentRule::SnapToTarget, false);
// create units
for(int32 i = 0; i < UnitsNumMax; ++i)
{
FString name = GetName();
name += "_";
name += FString::FromInt(i);
SpawnParameters.Name = *name;
auto Character = ()->SpawnActor<AGameSceneUnitChar>(UnitCharacterClass, initTranform, SpawnParameters);
Character->AttachToActor(this, attachmentRule);
UnitCharacters.Push(Character);
}
}
In result if I am not using
if(UnitCharacters.Num() != 0)
return true;
It generates lot of character actors attached and not to unit actor.
I also have add
// Called when destroys starts
void AGameSceneUnit::BeginDestroy()
{
for(auto character : UnitCharacters)
character->Destroy();
Super::BeginDestroy();
}
to check if it clears, in result I see that it deletes lot of actors too but not all of them
Question is how better to realize auto spawning of actors inside another actor not in gameplay (it’s easier) but also in Editor, because I want to see units on map edit if it’s possible.