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"

Why this code spawns two actors?

Hello. I am following the tutorials and, in the "on your own" chapter, I am wanted to learn how to attach complete actors to actors. I have managed to attach an actor to actor by spawning the actor I wanted to attach and attach its root component to the actor to be attached on. But the problem is, when this process is finished, I get two actors which I spawned instead of one.

Here is the code:

 MyActor::MyActor() { // Constructor
 // Spawn an actor and attach its root component to this actor
 UWorld* ThisWorld = GetWorld();
     if (ThisWorld)
     {
         FActorSpawnParameters SpawnParameters;
         SpawnParameters.Owner = this;
         SpawnParameters.Instigator = Instigator;
         FVector SpawnLocation = GetActorLocation() + FVector(100.0f, 0.0f, 0.0f);
 
         AOrbitingActor* NewOrbitingActor = ThisWorld->SpawnActor<AOrbitingActor>(SpawnLocation, GetActorRotation(), SpawnParameters);
         NewOrbitingActor->AttachRootComponentTo(RootComponent);
     }
   }

I get the spawned actor attached to my actual actor, plus I get one another spawned actor which isn't attached to anything. I don't want this extra spawned actor. How can I prevent this? Is this a bug?

Product Version: UE 4.10
Tags:
more ▼

asked Apr 25 '16 at 09:13 AM in C++ Programming

avatar image

Bora Semiz
27 5 6 10

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

2 answers: sort voted first

Make sure you also destroy the child when your actor is destroyed. When you drag a BP into the world, a preview object gets created, then deleted. The child remains, unattached to anything.

more ▼

answered Apr 25 '16 at 12:38 PM

avatar image

Plosnita
267 5 8 21

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

Do not use spawning actor inside constructors. Try spawining in PostInitializeComponents()

more ▼

answered Apr 25 '16 at 12:45 PM

avatar image

broly
514 35 54 78

avatar image Bora Semiz Apr 26 '16 at 07:13 AM

I have tried but the editor crashed after I hit play.

avatar image broly Apr 26 '16 at 10:36 AM

And what about BeginPlay?

Constructor is more global action than starting game. Spawning actors is gameplay event, so you can't spawn any actors outside a game.

Yes, it's mistake - PostInitializeComponents used for creating components, not actors. So to spawn actor you must use BeginPlay. But there different nouances: actor will be spawn on server and on client and this actors will be differs, but will appear as one in current context. It should be considered.

(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