x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to spawn actors from another for Editor

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 = GetWorld()->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.

alt text

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 alt text

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.

Product Version: UE 4.19
Tags:
more ▼

asked Jul 17 '18 at 06:58 PM in C++ Programming

avatar image

osiris_dev
30 3 4 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I think best option for you is Actor Child Component, which let you attach actor like component and it should handle spawning for you

https://api.unrealengine.com/INT/API/Runtime/Engine/Components/UChildActorComponent/index.html

Other then that you could destroy old actor when new one is created in OnConstruction or use other events that AActor has in disposal

more ▼

answered Jul 17 '18 at 11:18 PM

avatar image

Shadowriver
37.1k 935 172 1116

avatar image osiris_dev Jul 18 '18 at 05:15 AM

Looks like a solution, thank you! I'll check this.

avatar image osiris_dev Jul 18 '18 at 01:07 PM

alt text It helped but not 100% Instead of 1 character I still have 2, also on movement number of second UnitCharacter grow, I think it recreates it on transform. First UnitCharaster stays the same.

avatar image osiris_dev Jul 18 '18 at 01:19 PM

Been able to fix that, so it finally works well! My great gratitude!!!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question