# C++ Deferred construction of UObject with parameters / initial setup?

**URL:** <https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176>\
**Category:** C++\
**Tags:** constructors, unreal-engine, UE4-26, CPP, question\
**Created:** [March 1, 2022, 6:32pm UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176 "2022-03-01T18:32:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [March 1, 2022, 6:32pm UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176/1 "2022-03-01T18:32:00Z")

</div>

I know new Actors can be spawned using:  
World-\>SpawnActorDeferred  
Which will allow me to set some public properties on them before finalizing the spawn process.

However, I want to do this on UObjects as well and have full access to private properties (if possible) before finalizing the spawn process. Is this possible?  
Or am I limited to implementing my own setup method for almost every object I create?

Best,  
Seda145

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [March 1, 2022, 11:45pm UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176/2 "2022-03-01T23:45:12Z")

</div>

You can declare the class doing the work as a _friend class_ for your custom UObject class.

Doing that will allow you to customize all private members of the new object from within the manipulator class’ methods.

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [March 2, 2022, 9:55am UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176/3 "2022-03-02T09:55:42Z")

</div>

Thank you, this is exactly what I needed.

Is it also possible to set up the properties on the UObject before it runs its constructor?  
Or pass the parameters to the constructor through NewObject()?  
Take for an example an inventory system, where the inventory creates a few items:

Inventory:

```auto
UItem* NewItem = NewObject<UItem>();
NewItem->SomePrivateProperty = SomeValue;
// NewItem->FinalizeConstruction() ???
```

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [March 2, 2022, 1:39pm UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176/4 "2022-03-02T13:39:40Z")

</div>

You cannot pass custom parameters to UObject-derived constructors. This includes actors. Deferred spawning does not “delay” the constructor since that is impossible.

Deferred Actor Spawning just delays script construction and component initialization until you call FinishSpawning()

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [March 2, 2022, 2:27pm UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176/5 "2022-03-02T14:27:56Z")

</div>

You can only use an exist UObject or Asset of same base type as a template.  
Example:

```auto
UItem* NewItem = NewObject<UItem>( this, NAME_None, RF_NoFlags, BaseItemAssetPtr );
```

Whatever values are in the base Asset/Object, goes as default value for the newly created item instance. If your Ptr is to a Class / BP class, you must pass as a template its classPtr-\>GetDefaultObject() to the NewObject\<\> method.

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [March 3, 2022, 2:16pm UTC](https://forums.unrealengine.com/t/c-deferred-construction-of-uobject-with-parameters-initial-setup/503176/6 "2022-03-03T14:16:14Z")

</div>

thank you
