# Managing the lifetime of actors spawned in Consutrction script

**URL:** <https://forums.unrealengine.com/t/managing-the-lifetime-of-actors-spawned-in-consutrction-script/360118>\
**Category:** World Creation\
**Tags:** actor, construction, Destruction, spawning, unreal-engine, question\
**Created:** [June 8, 2016, 10:53pm UTC](https://forums.unrealengine.com/t/managing-the-lifetime-of-actors-spawned-in-consutrction-script/360118 "2016-06-08T22:53:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PantherNZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PantherNZ](https://forums.unrealengine.com/u/PantherNZ)\
**Post date:** [June 8, 2016, 10:53pm UTC](https://forums.unrealengine.com/t/managing-the-lifetime-of-actors-spawned-in-consutrction-script/360118/1 "2016-06-08T22:53:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![PantherNZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PantherNZ](https://forums.unrealengine.com/u/PantherNZ)\
**Post date:** [June 9, 2016, 10:33am UTC](https://forums.unrealengine.com/t/managing-the-lifetime-of-actors-spawned-in-consutrction-script/360118/2 "2016-06-09T10:33:08Z")

</div>

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 );  
}

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ae3d98ae](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ae3d98ae](https://forums.unrealengine.com/u/anonymous_user_ae3d98ae)\
**Post date:** [January 17, 2018, 7:58pm UTC](https://forums.unrealengine.com/t/managing-the-lifetime-of-actors-spawned-in-consutrction-script/360118/3 "2018-01-17T19:58:41Z")

</div>

You can use  
NewComponent-\>CreationMethod = EComponentCreationMethod::UserConstructionScript;  
instead of  
NewComponent-\>bCreatedByConstructionScript\_DEPRECATED = true;
