FObjectInitializer::CreateDefaultSubobject changing object type results in invalid objects

I’ve had a problem like this because of blueprint parenting. There may be a better way, but I got around this by creating a duplicate of my base class and reparenting the blueprint to that. The editor will warn that there may be problems with the reparenting but it’s fine to do it since the duplicate base class is identical. Then I changed the component type in my original class and reparented back to it and it worked.

After that, there might be functionality in your blueprint that needs replacing. For example, a sphere doesn’t have a half-height.

If that doesn’t work, you can also try rebuilding it all from a clean project (from scratch – as in no saved/intermediate/etc directories). You can either delete directories yourself (if you know what you’re doing), or package the project to a zip and extract it into a new directory. GL.

I have a native abstract uclass called Grenade with an UCapsuleComponent defined as a UPROPERTY.

UPROPERTY ( VisibleDefaultsOnly, BlueprintReadOnly, Category = Mesh )
UCapsuleComponent* Collision;

Inside the constructor I initialized it like this:

this->Collision = objectInitializer.CreateDefaultSubobject<UCapsuleComponent>(
            this, TEXT("Collision"));
this->Collision->SetNotifyRigidBodyCollision(true);
this->Collision->SetCollisionProfileName(TEXT("Projectile"));
this->Collision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
this->Collision->SetCollisionObjectType(COLLISION_PROJECTILE);
this->Collision->SetCollisionResponseToChannel(ECC_PhysicsBody, ECR_Overlap);
this->Collision->bReturnMaterialOnMove = true;

this->RootComponent = this->Collision;

It works just fine. I’m able to throw the grenade and detonate it inside the game. Due to a few issues with UCapsuleComponent we decided to replace it with an USphereComponent. Unfortunately, the game crash upon changing the object type to USphereComponent.

UPROPERTY ( VisibleDefaultsOnly, BlueprintReadOnly, Category = Mesh )
USphereComponent* Collision;

And inside the constructor:

this->Collision = objectInitializer.CreateDefaultSubobject<USphereComponent>(
            this, TEXT("Collision"));
this->Collision->SetNotifyRigidBodyCollision(true);
this->Collision->SetCollisionProfileName(TEXT("Projectile"));
this->Collision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
this->Collision->SetCollisionObjectType(COLLISION_PROJECTILE);
this->Collision->SetCollisionResponseToChannel(ECC_PhysicsBody, ECR_Overlap);
this->Collision->bReturnMaterialOnMove = true;

this->RootComponent = this->Collision;

The details panel inside the blueprint class won’t show anything which in my estimation indicates it has not been initialized:

If I add this to OnConstruction in addition to the code in constructor of the class it works inside the game (though still the details panel is empty):

this->Collision = NewObject<USphereComponent>(
            this, TEXT("Collision"));
this->Collision->SetNotifyRigidBodyCollision(true);
this->Collision->SetCollisionProfileName(TEXT("Projectile"));
this->Collision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
this->Collision->SetCollisionObjectType(COLLISION_PROJECTILE);
this->Collision->SetCollisionResponseToChannel(ECC_PhysicsBody, ECR_Overlap);
this->Collision->bReturnMaterialOnMove = true;

I suppose the blueprint class caches the constructor somewhere, so I created another blueprint. The first time I open the blueprint the Details panel shows the properties for the collision component. If I run the game and crash it it won’t show anything any more. I tested this on both Linux/Clang and Windows/VC++. I even tried renaming Collision to Collision2 or removing the original blueprints class from the project. None of that works.

yes unreal buildsystem don know how to clean up hios own …
deleting all folder, apart from content,config,source might help aswell.

Thank you so much for the answer.

I cleaned up the project using git clean -d -f -x which removes Binaries, Intermediate, … directories from our project (anything not under source control or ignored inside .gitignore file). Rebuilt the project and it’s still the same.

$ git clean -d -f -x
Removing Binaries/
Removing Content/Collections/
Removing Content/Developers/
Removing DerivedDataCache/
Removing Intermediate/
Removing Plugins/UFSM/Binaries/
Removing Plugins/UFSM/Intermediate/
Removing Reminiscence.pro
Removing Reminiscence.pro.user
Removing ReminiscenceConfig.pri
Removing ReminiscenceDefines.pri
Removing ReminiscenceHeader.pri
Removing ReminiscenceIncludes.pri
Removing ReminiscenceSource.pri
Removing Saved/

I tried duplicating the C++ base class and reparented the blueprint class with the same results. I’ve seen many weird bugs in UE4; with this one I’m going nuts.

We keep our project under source control (git). I cleaned up the project using git clean -d -f -x which removes Binaries, Intermediate, … directories from our project. Rebuilt the project and it’s still the same.

not usng github, but i have a folder called : .vs which might be related to visual studio.

are u checking ur log files for crash reasons? another way to find bugs is to install unreal vs. and debug the unrealeditor and launch ur game in there.

i would advise u to try this first with a project unbound to github, since unreal has to be forced rebuild all projectfiles.
i had a wired bug, where the launcher still would start a old version that was in another directory and not even existed anymore. it actually launched the new version under visual studio, ueditor but would launch a false project when i used .uproject file.

Be sure to keep only those 3 folders (content,source,config). then generate new c++ files, cook the content in editor and then u should be fine.

Well, I’m not using Visual Studio since I’m developing on Linux (building is much faster than VS). Our artists use Windows and Visual Studio to build the engine and the project source (no launcher) and we added .vs and .vscode directories to our .gitignore which means git clean -d -f -x removes those directories for you (I checked the project directory after that, below are the the direcories).

drwxr-xr-x  8 babaei babaei 4096 Dec 17 16:37 .git
-rw-r--r--  1 babaei babaei   12 Aug 26 15:50 .gitattributes
-rw-r--r--  1 babaei babaei 1313 Nov  4 14:42 .gitignore
drwxr-xr-x  2 babaei babaei 4096 Dec 13 09:53 Config
drwxr-xr-x 18 babaei babaei 4096 Dec 17 16:37 Content
drwxr-xr-x  2 babaei babaei 4096 Dec 13 12:57 Deployment
drwxr-xr-x  4 babaei babaei 4096 Nov  4 14:48 Documentation
drwxr-xr-x  3 babaei babaei 4096 Nov  4 14:48 Plugins
-rw-r--r--  1 babaei babaei  971 Nov  7 16:54 README.md
-rw-r--r--  1 babaei babaei  613 Oct 15 10:57 Reminiscence.uproject
drwxr-xr-x  2 babaei babaei 4096 Dec 13 10:00 Scripts
drwxr-xr-x  5 babaei babaei 4096 Nov  7 12:50 Source

.git contains git-related stuff. Deployment and Scripts contain a few scripts and Documentation as the name suggests only contains docs. So, I assume that is out of question.

It seems I have no other choice except trying it on a new clean project.

did u try to rename ur collison component ?
USphereComponent* Collision;

u could try to keep the original calupse component and add spehre component under a new name. and rename the references. ?

i would try to
-rename object
-add calupsecomponent under old name

i would try to add debugmessages so u can figure out if the problem is the sphere component and not the unreal build system

and can u debug the editor, like i do it under visual studio and then start the project within it ?
that might be the best way to figure out whats going on.

It can’t ever be easy with this engine, hehe. Tho what you describe isn’t exactly what I said. I said to reparent twice. Go back to the way your project worked – with the capsule. Run it and make sure it doesn’t crash. Then duplicate the class and reparent the blueprint to the duplicate. Run it again and make sure it works. Finally, change the capsule to sphere in the original and reparent back. Change the blueprint where capsule and sphere aren’t compatible, and run it a final time.

Did you do that exactly?

If so then the only way I’d be able to help further is if you build it in “DebugGame”, get it to crash, and post the logs. It might also be wise to get it to work in Windows first, and then try Linux.

Ah, yesterday, on my first try I did this:

this->Collision = NewObject<USphereComponent>(
            this, TEXT("Collision2"));

I rebuilt the project opened the editor and it didn’t work.

Today, based on your suggestion I tried this (renamed the C++ variable too, still the original UCapsuleComponent type):

this->Collision2 = NewObject<UCapsuleComponent>(
            this, TEXT("Collision2"));

I had 3 blueprints classes (3 types of grenades) subclassed from this class. I opened one of them and pressed the Compile button inside Blueprint Editor and then the Save button. Then I renamed it back to (this time USphereComponent):

this->Collision = NewObject<USphereComponent>(
            this, TEXT("Collision"));

I rebuilt the project and reopened the editor now the Blueprint that I compiled and saved works. The other two won’t. So, I guess I could repeat the same procedure for those two.

Thank you for the detailed instructions. I have to add that I always build in DebugGame. When I go through editor code in the debugger I see part of the object is uninitialized so, I guess that’s why the details panel is empty.

By the way, I came up with a solution based on @Kanteh suggestion which was easier. It fixed the issue for me. See the other answer for more details, please.