I’m just trying to make 2 container classes for a bunch of variables and one of them having a variable of the type of the other class. I basically want to replace my blueprint-structs with this because structs can not be passed by reference. And I want these to be fully usable inside blueprints.
Data1.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Object.h"
#include "Data2.h"
#include "Data1.generated.h"
UCLASS()
class TESTDELETEME_API UData1 : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "BaseCharacter")
float test1 = 0.0f;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "BaseCharacter")
float test2 = 0.0f;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "BaseCharacter")
UData2* obj; // this one compiles sucessfully, but VS throws errors which is weird. How can it compile sucesfully but with errors?
UData2 obj; // doesn't compile
Data2* obj; // doesn't compile
Data2 obj; // doesn't compile
};
Data2.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Object.h"
#include "Data2.generated.h"
UCLASS()
class TESTDELETEME_API UData2 : public UObject
{
GENERATED_BODY()
public:
//Step 2: Expose a float property
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "BaseCharacter")
float Data2Object = 0.0f;
};
The second error line “Cannot open Data2.generated.h” indicates to me you didn’t run GenerateProjectFiles.bat, or if you added that class from the editor, maybe you changed the file name?
While I use stackoverflow at times as well, in this instance, they are going to be less than helpful, as UE4, has it’s own allocation/deallocation of objects, which I’m betting that is in full force, when you have the UCLASS macro being invoked, as well as the GENERATED_BODY. I know that, for my own code (Blueprint function library, i.e. all static routines), things smoothed out a lot when I used the “()” with “new” to get a object allocated. But I am not usng a “constructor” in the blueprint libary as it has a interface set up that is being called, upon the load of the DLL.
Thanks that indeed solved the error. Apparently I forgot to regenerate them at some point.
However, I’m unable to select Data1 or Data2 as the variable type in a blueprint. Also using “Construct from Class” works for Data1&Data2 but I can not promote them to variables (grayed out). Obviously I’m missing something to tell UE4 that this class can be used as a variable. But how?
Fatal error:
[File:D:\BuildFarm\buildmachine_++UE4+Release-4.11\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp]
[Line: 2250] UObject(const
FObjectInitializer&) constructor
called but it’s not the object that’s
currently being constructed with
NewObject. Maybe you trying to
construct it on the stack which is not
supported.
Same result as without the ().
As to initializing, the other object will need to exist. Is that what you wanted
UData2* obj = new UData1(); // create the second object in order to have a pointer to it
Obviously does not work because creating a new UData1 object and storing it in a variable of type UData2* makes no sense. I have 2 classes: Data1 and Data2, both of type UObject and both with exposed variables. And I would like Data1 to have an exposed variable of type Data2 AND initialize it automatically with a new instance of Data2.
I assume I MUST create it on the Heap (because that is what UE4 tells me) but how do I initialize “UData2* AnotherDataContainer” with a new instance of Data 2 on the heap?
I also checked other internet resources and I am (by using the new keyword) creating the object on the HEAP. I don’t know why UE4 complains about it being constructed on the Stack…
Okay I see so UE4 may perform some stuff under the hood that may cause it to get allocated to the stack instead. I removed the entire constructor and it’s declaration and wrote:
UData2* AnotherDataContainer = new UData2();
again. But same crash.
I now have a bare class that inherits from UObject with a simple exposed variable of another object. This is such simple and minimalistic code what could cause it then… And how do I force UE4 to allocate it on the HEAP (if that it the correct error message that is)? I google the error and only 5 results and none of them had the solution to my problem.
It works in blueprints however (but I prefer to do this in C++ as this is rather messy and also easily forgotten):
This is exactly what your after. I don’t have to do it this way, as I’m just allocating structures, that I don’t care if UE4 knows about them or not (i.e. Global data areas, that are all internal to the product, as well as my own cache, etc.)
Fatal error: [File:D:\BuildFarm\buildmachine_++UE4+Release-4.11\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2824]
NewObject with empty name can’t be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSuobject<> instead.
So I removed the constructor and tried it in the header:
And then I tried this instead in the header (removed CPP code):
UData1() { AnotherDataContainer = new UData2(); }
All result in the same crash+error:
UObject(const FObjectInitializer&) constructor called but it’s not the object that’s currently being constructed with NewObject. Maybe you trying to construct it on the stack which is not supported.
Combined with all the previous answer did the trick to make it all work. Thanks everyone. Sadly I can only mark 1 answer as the solution even though both answers together in this case solve it.
Even though I marked this one as the “accepted answer”, the other answer is also required to make everything work but I can not select 2 “accepted answers”.