I had an existing Blueprint class that I wanted to optimize by converting to C++. My plan was to create a C++ class, make the blueprint use the C++ class as a base class rather than just using Actor, making all the components that were in Blueprint in C++, deleting the duplicates from Blueprint, and then reimplementing all the logic in C++. This process was going fine until I reached the step for adding components to the Actor.
This existing class has many actors already placed within a few levels that I made, but I see that after adding components the location and then loading one of my levels the location of all of the actors in the Level are lost and get reset to 0,0,0.
This is the only code I have in the constructor:
PrimaryActorTick.bCanEverTick = true;
MySceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("My Scene Root"));
MySceneRoot->SetupAttachment(RootComponent);
TestSphere = CreateDefaultSubobject<USphereComponent>(TEXT("TestSphere"));
TestSphere->SetupAttachment(MySceneRoot);
If I comment out that code, the locations get retained just fine.
My theory is that changing the root is what caused this location reset. Only way I’m able to add new components right now in C++ without it breaking the locations of existing actors is to rename MySceneRoot to DefaultSceneRoot, this causes compilation to fail for the blueprint when I load up unreal engine because it conflicts with the default scene root name that was already in Blueprints, but when I go to my Collectible blueprint and see the compile error and recompile, everything works fine and the positions are retained and any new code I made that uses my TestSphere collider works. However, this sounds very hacky, and it seems like a bad sign that it would show a compile error every time I open the project.