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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Can Actors spawn Actors? (crash)

I'm trying to spawn an actor in my c++ code, but it seems to automatically get deleted. Furthermore, if I run the spawning event twice, the whole editor crashes. The code I use for spawning is the following:

 void AMyPawn::Shoot()
 {
     UWorld* World = GetWorld();
     if (World != nullptr)
     {
         FVector Location = GetActorLocation();
         FRotator Rotation = GetActorRotation();
         FActorSpawnParameters Params;
         Params.Owner = this;
         Params.Instigator = Instigator;
         Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
         Params.Name = TEXT("SPAWNEDDD");
         World->SpawnActor<AFloatingActor>(Location, Rotation, Params);
     }
 }

Which seems to be what one should do after reading other questions and resources on the web. FloatingActor's constructor:

 AFloatingActor::AFloatingActor()
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     UE_LOG(LogTemp, Warning, TEXT("Spawned an actor"));
     FloatingMagnitude = 20.f;
     InitialLifeSpan = 100.f;
     RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Salamander"));
 }

The one thing I see that is different from some tutorials is that they tell you to spawn actors from the GameMode. Is that the problem? Why, and if not, how do I get the actor to spawn correctly? As said, it shows up in the world outliner as "deleted actor" but the debug messages from the constructor do get printed.

Edit 2: I've discovered it does not crash, and indeed seems to work fine if I omit the spawn parameters when calling spawnActor, i.e.:

 World->SpawnActor<AFloatingActor>(Location, Rotation);

Edit: Here's the callstack that shows up after the crash. As said, it only crashes if I spawn the actor twice.

Callstack.txt

Product Version: UE 4.10
Tags:
call stack.txt (5.8 kB)
more ▼

asked Feb 02 '16 at 10:52 AM in C++ Programming

avatar image

Ljaniuk
1 1 2 4

avatar image Matthew J Feb 02 '16 at 01:52 PM

Hello Ljaniuk,

This should work correctly from what you've posted. I'd suggest running the console command "slomo 0.1" or even lower to slow down the time before you spawn the actor. This should allow you to see if the actor is being spawned and actually just flying off somewhere due to spawning inside of another collision. It could be hitting the Kill Z which would cause it to become a deleted actor.

Also, could you post the callstack of the crash you're getting when you try to spawn it a second time?

avatar image Ljaniuk Feb 02 '16 at 03:36 PM

Sure, added it. Also added another tidbit I discovered, it does seem to work if I don't add the spawn parameters.

I also have to work out how to make the actor have a staticmesh when spawned (until now I have added it as a component in the editor, but can't do that with an actor not yet spawned) so it shows up in the level. I know I can use the asset finder (as in the twin stick shooter example) in its constructor but I'd prefer not to have to change code when deciding for a different mesh. Does that mean I should add a UStaticMeshComponent* as a uproperty to the floatingActor, create a blueprint version of the class, and set the defualt mesh there? This is my current plan.

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

1 answer: sort voted first

I believe the reason it didn't work when you had the SpawnParams set was due to the setting you used for the SpawnCollisionHandlingOverride to ESpawnActorCollisionHandlingMethod::AlwaysSpawn. The default may be that it will not spawn on top of it but offset itself from any existing collision.

You're correct, the best way to do that would be to make it into a UPROPERTY so that you can set it from inside of the editor.

more ▼

answered Feb 02 '16 at 03:59 PM

avatar image Ljaniuk Feb 02 '16 at 05:51 PM

In that case, what situation should the setting be used in? I used it because spawning despite collision was the behavior I wanted, why should that cause a crash? Nevertheless, this answers my question, so you can add it as an answer if you like.

avatar image Matthew J Feb 02 '16 at 05:54 PM

I would imagine something else is causing the crash, such as the result of using that (the actor going bonkers and getting destroyed) which causes the crash instead.

(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