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"

Managing the lifetime of actors spawned in Consutrction script

Hey there, so my system is as follows: I have several "level pieces" that contain various "obstacles", some of which are spawned in the construction script. I have created several helper functions in C++ that spawn obstacles in particular layouts (Bezier curves, shapes etc.). These functions are called from the Blueprint Construction script of the level piece.

Previously I was using the Blueprint node "AddChildActorComponent" and it worked perfectly. Now I am changing this to C++ I am very confused as to what functions to actually use, and how to properly manage their lifetimes. I was using "GetWorld()->SpawnActor( ObstacleClass, Transform, SpawnParams)" which I would then add to an array of obstacles "TArray< AActor* > Obstacles" which was working correctly. I managed to delete / cleanup the array most of the time but it did not work when the level piece was deleted in the level editor (NOT during game run time). I tried using "OnDestroy", "EndPlay" & "BeginDestroy" and have had no luck, often with it just crashing.

Is there a way I can just hand the spawned obstacle off to the owned parent (the level piece) so that it is automatically handled / destroyed when the level piece is? What are the correct spawning and destroying functions I should be using? Should I be using AChildActorComponent? It seems like this should be extremely simple but I am having great difficulty understanding the huge variance in ways of doing things and what I should be actually using for this situation.

Thank you!

Product Version: UE 4.10
Tags:
more ▼

asked Jun 08 '16 at 10:53 PM in C++ Programming

avatar image

PantherNZ
66 5 7 15

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

1 answer: sort voted first
 Finally solved it:
 UChildActorComponent* NewComponent = NewObject< UChildActorComponent >( this );
 if( NewComponent )
 {
     NewComponent->SetChildActorClass( Class );
     NewComponent->AttachTo( RootComponent );
     NewComponent->RegisterComponent();
     NewComponent->bCreatedByConstructionScript_DEPRECATED = true;
     SpawnedChildObstacles.Add( NewComponent );
 }




more ▼

answered Jun 09 '16 at 10:33 AM

avatar image

PantherNZ
66 5 7 15

avatar image Artavazd256 Jan 17 '18 at 07:58 PM

You can use NewComponent->CreationMethod = EComponentCreationMethod::UserConstructionScript; instead of
NewComponent->bCreatedByConstructionScript_DEPRECATED = true;

(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