I am trying to instantiate an UObject subclass, but I have very strange errors when I do.
Fatal error: [File:D:\Build\++UE4+Release-4.18+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2484] EEarlyZPass::MAX_68678 is not being constructed with either NewObject, NewNamedObject or ConstructObject.
KERNELBASE
UE4Editor_ApplicationCore
UE4Editor_Core
UE4Editor_Core
UE4Editor_CoreUObject
UE4Editor_Underwater_9207!UChunk::UChunk() [d:\projects\underwater\source\underwater\chunk.cpp:3]
UE4Editor_Underwater_9207!UMapGeneratorComponent::CreateChunk() [d:\projects\underwater\source\underwater\mapgeneratorcomponent.cpp:97]
UE4Editor_Underwater_9207!UMapGeneratorComponent::execGenerateMap() [d:\projects\underwater\source\underwater\mapgeneratorcomponent.h:18]
UE4Editor_CoreUObject
UE4Editor_CoreUObject
The error complains that I am not using NewObject on something called EEarlyZPass::MAX_68678
(No idea what that is).
But here is the code instantiating the object where it says it crashes:
void UMapGeneratorComponent::CreateChunk(int x, int y)
{
FString ChunkName = GetChunkName(x, y);
UE_LOG(LogTemp, Warning, TEXT("Creating new chunk %s: [%i,%i] - %i"), *ChunkName, x, y, ChunkCreationIndex);
UChunk* NewChunk = NewObject<UChunk>();
Chunks.Insert(NewChunk, ChunkCreationIndex);
ChunkIDs.Add(ChunkName, ChunkCreationIndex);
ChunkCreationIndex++;
}
As you can see, I am using UObject.
Just in case, I made sure the constructor is empty:
UChunk::UChunk() {
}
So this constructor is not calling anything, the object is just being created and stored in a TArray, nothing else happens. Yet, I get this error.
Also might be a relevant note, my class was initially a normal C++ class, which I turned into a UObject as there was no way of storing it in a TMap, then it turned out TMap didn’t keep my object in memory so now I am storing it in a TArray.
I’m just trying to instantiate a class and store it in an array, but somehow this seems impossible in Unreal.
This problem also only started happening once I started trying to store the instances in a TMap/TArray. I could instantiate my class fine, but once I tried to store it it wouldn’t work until I made it a UObject, and now I can’t find a way that works.
Also for reference, the class definition:
#pragma once
#include "CoreMinimal.h"
#include "PerlinNoiseComponent.h"
#include "RuntimeMeshComponent.h"
#include "FMapParameters.h"
#include "MeshParameters.h"
#include "Chunk.generated.h"
UCLASS( ClassGroup=(CUSTOM) )
class UNDERWATER_API UChunk : public UObject
{
GENERATED_BODY()
public:
UChunk();
void Generate(int ChunkID, int ChunkX, int ChunkY, FMapParameters Parameters, UPerlinNoiseComponent* Noise, URuntimeMeshComponent* RuntimeMesh);
void GenerateVertices();
void GenerateTriangles();
void GenerateMesh();
void InitMeshToCreate();
FVector2D GetWorldPositionForGridPosition(float x, float y);
FVector2D CalculateUV(float x, float y);
float GetZPositionFromPerlinResult(float PerlinResult);
int GetNoiseIndexForCoordinates(int x, int y);
FVector GetNoiseValueAt(int x, int y);
void SetNoiseValueAt(int x, int y, FVector value);
private:
bool Generated;
int ChunkX;
int ChunkY;
FMapParameters Parameters;
UPerlinNoiseComponent* Noise;
URuntimeMeshComponent* RuntimeMesh;
int ChunkID;
MeshParameters MeshToCreate;
};