# Base class Pointer not saved after instance

**URL:** <https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 15, 2016, 5:21am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502 "2016-02-15T05:21:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChaoSXDemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ChaoSXDemon](https://forums.unrealengine.com/u/ChaoSXDemon)\
**Post date:** [February 15, 2016, 5:21am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/1 "2016-02-15T05:21:49Z")

</div>

I have a base class:

```
UCLASS(Abstract, EditInlineNew)
class MYGAME_API UBaseClassObject : public UObject
{
	GENERATED_BODY()
	
public:
	UBaseClassObject();
}

```

I have a concrete sub-class:

```
UCLASS()
class MYGAME_API UConcreteClass : public UBaseClassObject 
{
	GENERATED_BODY()

public:
	UConcreteClass();

};

```

Here’s an actor that has a pointer to the base class:

```
UCLASS()
class MYGAME_API AUserActor : public AActor
{
	GENERATED_BODY()
	
public:	

	UPROPERTY(EditAnywhere, Instanced)
	UBaseClassObject * PolymorphicObject;

	AUserActor();

};

```

In the editor I can see that I have a drop down of None or my UConcreteClass as choice. I can successfully chose the sub-class but the problem is that during the constructor of the AUserActor, the base class pointer becomes null even though I have set it to the sub-class during editing time. Why is this happening?

```
AProceduralMeshActor::AProceduralMeshActor()
{
	if (PolymorphicObject != nullptr) //this line is always false ... PolymorphicObject is always null
	{
		//Do something with PolymorphicObject
	}
}
```

---

<div class="post-metadata">

**Author:** ![fszczemton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@fszczemton](https://forums.unrealengine.com/u/fszczemton)\
**Post date:** [February 16, 2016, 8:24am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/2 "2016-02-16T08:24:57Z")

</div>

What if you tried with the _FObjectInitializer_ constructor variant? It seems expected to have it as null in case of default constructor as there’s nothing there to set it.

---

<div class="post-metadata">

**Author:** ![ChaoSXDemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ChaoSXDemon](https://forums.unrealengine.com/u/ChaoSXDemon)\
**Post date:** [February 16, 2016, 8:39am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/3 "2016-02-16T08:39:01Z")

</div>

Very good suggestion! Unfortunately it did not work. At first I thought this must be it but now I’m noticing even after I save the object with the concrete sub-class, upon reopening the editor, it is also left to nullptr! Why is this happening?

---

<div class="post-metadata">

**Author:** ![fszczemton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@fszczemton](https://forums.unrealengine.com/u/fszczemton)\
**Post date:** [February 16, 2016, 10:36am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/4 "2016-02-16T10:36:58Z")

</div>

I checked the AActor constructor and it seems like it couldn’t have helped anyway:

```
AActor::AActor()
{
	InitializeDefaults();
}

AActor::AActor(const FObjectInitializer& ObjectInitializer)
{
	// Forward to default constructor (we don't use ObjectInitializer for anything, this is for compatibility with inherited classes that call Super( ObjectInitializer )
	InitializeDefaults();
}
```

---

<div class="post-metadata">

**Author:** ![fszczemton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@fszczemton](https://forums.unrealengine.com/u/fszczemton)\
**Post date:** [February 16, 2016, 10:44am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/5 "2016-02-16T10:44:27Z")

</div>

What If you override _PostInitProperties_ and check if in that function your property is also null?

Sorry, for not checking that myself, I don’t have access to my computer now.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [February 16, 2016, 10:49am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/6 "2016-02-16T10:49:56Z")

</div>

That’s because the C++ constructor is the very first thing that is executed when an instance is created, it’s before any of the property system code can kick in. You shouldn’t be putting anything in your constructor except assigning default values and maybe creating default subobjects.

Any logic dependent on property values needs to be executed later, probably in either `OnConstruction` (if you need your class to react immediately to changes in-editor) or in `BeginPlay` (if it’s just something that needs executing when the game starts).

---

<div class="post-metadata">

**Author:** ![ChaoSXDemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ChaoSXDemon](https://forums.unrealengine.com/u/ChaoSXDemon)\
**Post date:** [February 17, 2016, 3:33am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/7 "2016-02-17T03:33:04Z")

</div>

Indeed, very good point! So what’s the difference between OnConstruction vs BeginPlay? I think BeginPlay is safe to inter-component dependency right?

---

<div class="post-metadata">

**Author:** ![ChaoSXDemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ChaoSXDemon](https://forums.unrealengine.com/u/ChaoSXDemon)\
**Post date:** [February 17, 2016, 3:35am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/8 "2016-02-17T03:35:55Z")

</div>

This works! However physics is not working … am I missing something again?

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [February 17, 2016, 9:55am UTC](https://forums.unrealengine.com/t/base-class-pointer-not-saved-after-instance/347502/9 "2016-02-17T09:55:55Z")

</div>

Yeah BeginPlay is basically the last initialization function called, so all components should have been initialized by that stage. OnConstruction is more of an in-editor procedural setup thing, it gets triggered anytime you change any property of an actor in the editor.

Don’t know about the physics, can only suggest you check that all physics related properties have been initialized as you want them.
