How do I use NewObject()?

I have an actor class that has a USTRUCT. This USTRUCT houses an array of FVectors. This Actor class has a UFUNCTION that fills those 12 spots with the needed locations. Then in my GameMode class During StartPlay() I create an object of the actor class which houses the USTRUCT. At this point the game compiles but when I run it I get a break point. If I comment out the instatiation of the Actor class it work - so I’m pretty sure it’s related to how I’m doing that. I’m new to dealing with dynamic memory and pointers. I’ve been reading over information trying to fill in the holes of my understanding. I’m still having difficulty with how this works. Below is a brief summary of the parts before and including the instatiation of the actor class object.


//The Array in my USTRUCT...
UPROPERTY() TArray<FVector> IcoVerts;

//instantiating the USTRUCT
FVerts worldIcoCorners;

//The UFUNCTION that adds the data into the array
UFUNCTION() void IcoAddVert();

//The UFUNCTION adding data.
void AWorldCell::IcoAddVert()
worldIcoCorners.IcoVerts.Add( Needed FVector Data ));

void AGameMode_Maze::StartPlay()
//If this is included it, when running the game it always causes a break point.
 AWorldCell* WorldCellObject = NewObject<AWorldCell>();

//And finally the point where I'm trying to spawn an separate actor class.  This works by itself if the before mentioned parts are not included. 
ATestAct* TWN = World->SpawnActor<ATestAct>(ATestAct::StaticClass(), Location, Rotation);


Use ConstructObject(UClass*) insted of NewObject

Okay I changed it to:

AWorldCell* EF6World = ConstructObject<AWorldCell>(AWorldCell::StaticClass());

When I run the game I get another break. It opens the Casts.cpp file and bolds

UE_LOG(LogCasts, Fatal, TEXT("Cast of %s to %s failed"), FromType, ToType);

The problem still appears to be central to the object line. Am I using it correctly?

UE4 got some protection system which detects error before crash and post log so you know whats wrong and let it crash the game (which VS reponce with automatic break point), but because of that it shows code inside engine. Look on call stack and check last call from your module dll, it will lead you to buggus line of your code, looking on what he is trying to log you got some incorrect cast.

Also i realized your class has “A” prefix which means it’s a Actor, which means you should not use ConstructObject but SpawnActor, it might be also cause of incorrect cast somewhere in engine. Sorry i didn’t notice this ealier, you should use ConstructObject to create UObject, SpawnActor to create AActor.

If I Create an object of my actor class

AworldCell EF6World;

Then Call the method on that class


Which fills the TArray with the needed Locations. Then I use

ATestIcoCorner* TheeWallNinty = World->SpawnActor<ATestIcoCorner>(ATestIcoCorner::StaticClass(), EF6World.worldIcoCorners.IcoVerts[i] * 10000.f, FRotator::ZeroRotator);

Which spawns the actor into the world using those locations. Once I run the game I get a different break point:

"%s is not being constructed with either NewObject, NewNamedObject or ConstructObject."

I’ve tried NewObject and ConstructObject. I must be missing something here. My AWorldCell class is an actor class that I wanted to use to construct the layout of where my level geometry will spawn. I know the assets that spawn will also be actor classes, but should this construction class not be an actor class? Should I create a generic class that’s not one of UE4’s to handle this?

Paste whole header file of that class

You cannot do that :

 AworldCell EF6World;

It’s valid in standard C++ but UE4 forbid you of creating Actors like this. Like said, you need to use SpawnActor. Also, what you are trying to do, is to set static values to a class, in your case using the method IcoAddVert().

What you seem to want is to create a static variable for your IcoVerts array, and also declare your IcoAddVert() as static. Basically, you want the AworldCell to hold static values right? It’s seems like a weird way of doing it but anyway…

Your code could look something like this:

AworldCell::IcoAddVert(); //Static function call, that can do whatever to any static variables

//again, worldIcoCorners needs to be static
ATestIcoCorner* TheeWallNinty = World->SpawnActor<ATestIcoCorner>(ATestIcoCorner::StaticClass(), EF6World::worldIcoCorners.IcoVerts[i] * 10000.f, FRotator::ZeroRotator);

ConstructObject is deprecated from v4.9. Use NewObject.

1 Like