Download

Trying to create subobject

Hello everyone. I have two C++ classe.
One is a SplinePipe, the other one is a SplineObjectTrackGenerator. They both inherit from AActor.

When I tried to instantiate SplinePipe as a component of SplineObjectTrackGenerator, I thought I could use something like:


ASplinePipe* pipe = CreateDefaultSubobject<ASplinePipe>(TEXT("Pipe"));

But even though this compiles, it crashes the game when it starts. I narrowed down the crash to being caused by the line above, it seems.

Here is the error message


Fatal error: [File:D:\Build\++UE4+Release-4.15+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 117] 
No object initializer found during construction.


UE4Editor_Core!FDebug::AssertFailed() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\core\private\misc\assertionmacros.cpp:349]
UE4Editor_CoreUObject!UObject::CreateDefaultSubobject() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\coreuobject\private\uobject\obj.cpp:118]
UE4Editor_ImBack_2481!ASplineObjectTrackGenerator::BeginPlay() [c:\users\lord of xy\documents\unreal projects\imback\source\imback\splineobjecttrackgenerator.cpp:46]
UE4Editor_Engine!AActor::DispatchBeginPlay() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\actor.cpp:3097]
UE4Editor_Engine!AWorldSettings::NotifyBeginPlay() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\worldsettings.cpp:183]
UE4Editor_Engine!AGameStateBase::HandleBeginPlay() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\gamestatebase.cpp:177]
UE4Editor_Engine!UWorld::BeginPlay() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\world.cpp:3435]
UE4Editor_Engine!UGameInstance::StartPlayInEditorGameInstance() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\gameinstance.cpp:338]
UE4Editor_UnrealEd!UEditorEngine::CreatePIEGameInstance() [d:\build\++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\playlevel.cpp:3420]
UE4Editor_UnrealEd!UEditorEngine::PlayInEditor() [d:\build\++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\playlevel.cpp:2515]
UE4Editor_UnrealEd!UEditorEngine::StartQueuedPlayMapRequest() [d:\build\++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\playlevel.cpp:1215]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build\++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1522]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build\++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:391]
UE4Editor!FEngineLoop::Tick() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:3025]
UE4Editor!GuardedMain() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\launch.cpp:166]
UE4Editor!GuardedMainWrapper() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:134]
UE4Editor!WinMain() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:210]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Does anyone recognize this as a common problem? I feel like this is a simple fix, but I just don’t know the right function or syntax.

What are you trying to do here?

If ASplinePipe is an actor you’ve created yourself and you want to make it appear in the world, you can use ASplinePipe* Pipe = GetWorld()->SpawnActor<ASplinePipe>() to spawn the actor and store a reference to it.

CreateDefaultSubobject is used when you want to instantiate a subobject for an actor.
GetWorld()->SpawnActor is used when you want to spawn an actor somewhere in the world.

Basically, you’re trying to create an actor using a method used to create a subobject. They are two completely different things :slight_smile:

“CreateDefault” is meant to be used inside constructor function only.