How to derive from UObject and instantiate at runtime from C++?

I have the following class:

UCLASS(BlueprintType, Blueprintable)
class MYPROJECT_API UFloorplanGraph : public UObject
{
	GENERATED_BODY()
	
public:

	UFloorplanGraph(){}
	UFloorplanGraph(const FObjectInitializer& ObjectInitializer){}
	~UFloorplanGraph(){}
 
    // Some Properties ...

	virtual void Serialize(FArchive &Ar) override;
};  

At runtime, I am executing this piece of code:

UE_LOG(LogTemp, Log, TEXT("We detected an object and create it."));
Ref = NewObject<UObject>(DetectedClass);
UE_LOG(LogTemp, Log, TEXT("This point is never reached."));

In this context, detected class equals UFloorplanGraph::StaticClass(). But I get an error with the following message.

Class which was marked abstract was trying to be loaded.  It will be nulled out on save. None Object

As far as I understand I can not create an instance of UFloorplanGraph because it is abstract.

Question: What do I need to do to make it not abstract and instantiable?


This is a more detailed excerpt of the log file:

[2016.08.23-15.13.09:107][ 77]LogTemp: We detected a class: /Script/AeonDevelopment.FloorplanGraph
[2016.08.23-15.13.09:107][ 77]LogTemp: We detected an object and create it.
[2016.08.23-15.13.09:107][ 77]LogUObjectGlobals:Warning: Class which was marked abstract was trying to be loaded.  It will be nulled out on save. None Object
[2016.08.23-15.13.09:107][ 77]LogOutputDevice:Warning: 

Script Stack:
ArchitectureSuitBlueprintEditor_C.ExecuteUbergraph_ArchitectureSuitBlueprintEditor
ArchitectureSuitBlueprintEditor_C.InpActEvt_K_K2Node_InputKeyEvent_4

[2016.08.23-15.13.09:751][ 77]LogStats: FPlatformStackWalk::StackWalkAndDump -  0.644 s
[2016.08.23-15.13.09:751][ 77]LogOutputDevice:Error: === Handled ensure: ===
[2016.08.23-15.13.09:751][ 77]LogOutputDevice:Error: 
[2016.08.23-15.13.09:751][ 77]LogOutputDevice:Error: Ensure condition failed: false [File:D:\Build\++UE4+Release-4.13+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2139]
[2016.08.23-15.13.09:751][ 77]LogOutputDevice:Error: Class which was marked abstract was trying to be loaded.  It will be nulled out on save. None Object
[2016.08.23-15.13.09:752][ 77]LogOutputDevice:Error: Stack: 
[2016.08.23-15.13.09:752][ 77]LogOutputDevice:Error: UE4Editor-Core.dll!FWindowsPlatformStackWalk::StackWalkAndDump() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\windows\windowsplatformstackwalk.cpp:183]

And the error message keeps going like this… Afterwards, I immediately get an access violation error (trying to access null).


Any help is welcome!

Hey rYuxq-

Are you trying to add an instance of FloorplanGraph to the level? If this is the case, can you try inheriting the class from AActor instead of UObject? Are you adding the log messages in the BeingPlay function of the FloorplanGraph class or in another file? Are you getting the reported error in Visual Studio or inside the editor?

Hey ! I am calling a blueprint function library method (which is implemented in c++) from the level blueprint and which is effectively executing those 3 lines of code above. The log messages are print from this chain of function calls (in c++). The error message was an excerpt of the log file of the editor, since the editor crashes.
Also,I am not sure if I am trying do add it to the level (or rather, I don’t think I know what you mean) All i do is have a reference from the level blueprint to it, which should be fine. I am neither trying to spawn or place it anywhere, which is why I thought uobject would be good.

Ahh, maybe I should add that calling the “construct object” node from the level blueprint works perfectly fine. This error happens only when I call new object the way I described above. Also, there are two errors in total. One which is a failed (but handled) ensure and before returning from the new object function call, I also get a critical error (an access violation error)

You mentioned a crash, can you explain when the crash occurs? Please also include the callstack and log file from the crash.

For the UObject itself, you should be able to use ConstructObject at runtime. Can you also try replacing “DetectedClass” with UFloorplanGraph::StaticClass() directly to see if that helps?

Thanks Bruno, but your answer is slightly missing my point :stuck_out_tongue: detected class equals UFloorplanGraph::Staticclass(). The type in the template refers as far as I understood only to the return type of the function when you pass it a UClass *

Can you recommend me a site to upload the logfile and the callstack to? I think just posting it here would be too cumbersome for the reader.

Also, i will try passing UFloorplanGraph::Staticclass() directly once I am back home and compose a proper response.

The callstack and log files should both be able to be saved as .txt files which can then be uploaded as attachments or zipped together before adding them.

Actually, never mind. The crash happened because I used “NewObject” wrong. I used “DetectedClass” as Outer, even though I meant to use it as the class to create. DetectedClass as to be the second argument that I pass. Which leads me to another question. Is it save to use “GetWorld()” as Outer?

Actually, the code that is involved with all this is meant to deserialize an UObject from a file. Which leads me to a more important question: How should I deal with parent/outer references when I want to serialize a UObject? And how should I deserialize it again? I assume that this is a common question that comes with serialization…

I have been using the NewObject function wrong.

Ref = NewObject<UObject>(Outer, DetectedClass);

is the way to go.
I didnt provide an outer, thinking it was not necessary.

thank you for the support!