Crash When Adding Static Mesh to Actor [C++]

UPDATE: For those who do a search and get this.

I’ve deduced the crash was actually from the // Code stuff about basic int32 math that hasn't yet crashed., as although I thought I had tested it in all cases, I hadn’t, and I guess I changed something at the same time I added the static mesh stuff.

Adding static meshes to actors is as follows:

Header:

	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	class UStaticMeshComponent* YourMesh;

.cpp:

	YourMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("YourMesh"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> YourMeshObject(TEXT("/Game/TheMeshYouWant")); // wherein /Game/ is the Content folder.

	YourMesh->SetStaticMesh(YourMeshObject.Object);

Howdy all.

I have a for loop wherein a “room” class is created a certain number of times. As of yet, this class has only contained some basic information, like what type of room it is, it’s length and width, where it should be placed, etc. I’m trying to visualize this in the editor by adding a StaticMeshComponent to the actor. Doing so causes the editor to crash in a heaping pile of fire and quietly muttered expletives.

The For Loop:

for (int i = 0; i < NumberFrameRects; ++i)
	{
		ACRoom01* aNewRect = GetWorld()->SpawnActor<ACRoom01>();
		// Code stuff about basic int32 math that hasn't yet crashed.
	}

CRoom01.h

UPROPERTY(BlueprintReadWrite, EditAnywhere)
		class UStaticMeshComponent* Block;

Only when those two lines are added, when a StaticMeshComponent is added to this item, does the game crash.

Hello , I’ve you tried to assign this staticMeshh in code (in the class constructor for example) to see if it’s still crash?

At a first glance, it seems he tries to reference a “null” object and that why he crashes

Also if possible, can you post the error it throw you when it crashes?

Gamer, Tried to give that a shot. Constructor is now as follows:

// Sets default values
ACBankRoom01::ACBankRoom01()
{

	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> BlockDebugMesh(TEXT("Content'/Box_Brush_StaticMesh'"));

	Block->SetStaticMesh(BlockDebugMesh.Object);
}

Unfortunately though, adding those two lines has created a new issue. Upon compiling, UE4 Editor crashed. The project will now no longer open. (Luckily I backed up last night :D)

What did I do wrong?

Hey -

To answer the second question first, declaring the static mesh component in your header and setting the static mesh in your constructor are two different things. To fix this you would want to take out the lines below “PrimaryActorTick” and replace them with Block = CreateDefaultSubobject(TEXT("Block")); With this, when you create a blueprint based on this class it will have a static mesh component attached to it. You can then set that static mesh through the editor or in code. To do so in code you would need to add a UStaticMesh pointer variable in your header (the same way you added the component) then use your ConstructorHelpers call and assign it to the static mesh rather than the static mesh component.

As for the crashes, the second crash is from trying to assign a static mesh to a static mesh property incorrectly. Without the log files (inside the project’s Saved/Logs folder) it would be difficult to say what the cause of the first crash is.

Cheers

Thanks !

I’m trying to go the code route for that, but I can’t seem to find what you mean with ConstructorHelpers.

Googling the problem, it seems like the code should look somewhat like this:

	Block = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Block"));

	BlockMesh = ConstructorHelpers::FObjectFinder(TEXT("Content/1x1x3Base.uasset"));

However, compiling yields this error: error C2955: 'ConstructorHelpers::FObjectFinder' : use of class template requires template argument list

I also tried

static ConstructorHelpers::FObjectFinder<UStaticMesh> BlockStaticMesh(TEXT("/Content/1x1x3Base.uasset"));

but I can’t seem to get that to work with Block->SetStaticMesh(BlockStaticMesh); given that the FObjectFinder doesn’t appreciate the use of pointers.

You would use the ConstructorHelper as you did in your second post: static ConstructorHelpers::FObjectFinder BlockDebugMesh(TEXT("Content'/Box_Brush_StaticMesh'"));

After this you would set your BlockMesh to the mesh stored in BlockDebugMesh with BlockMesh = BlockDebugMesh.Object

Still getting a ‘use of class template requires template argument list’ when using that. Is there an #include I’m forgetting, or should my constructor be more than ACBankRoom01::ACBankRoom01()?

Looking at the documentation for FObjectFinder (FObjectFinder | Unreal Engine Documentation) it looks like the necessary include would be “Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h” Let me know if adding the include helps or gives you the same/different message

Same message when adding #include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"

I’m a little confused on where this error is coming from. Is it pointing to the line of code with the ConstrutorHelper? If possible could you post the code for your class as well as the exact error message?

It’s that line, yup.

Errors:

error C2955: 'ConstructorHelpers::FObjectFinder' : use of class template requires template argument list

'ConstructorHelpers::FObjectFinder' : class has no constructors

Files too big for comment, so here is the header, and here is the .cpp.

Looking at the source file it looks like you’re missing the after FObjectFinder. This lets the compiler know what kind of object it’s supposed to be finding.

There’s a space after FObjectFinder, if that’s what you mean.

It seems AnswerHub doesn’t like angle brackets. after FObjectFinder you also need to add < UStaticMesh >

Huzzah! That solved that problem. Thanks!

Hello, i was having the same exact problem. What do you mean with you also need to add?
My line is

walk_a -> static ConstructorHelpers::FObjectFinder<UPaperFlipbook>(TEXT("PaperFlipbook'/Game/Sprites/Ken_Sprites/Animations/A_ken_walk.A_ken_walk'")).Object;

Where in the header i declared

UPROPERTY()
	class UPaperFlipbook* walk_a;

I fixed it, the error disappeared when i use = instead of → to set the object.

walk_a = static ConstructorHelpers::FObjectFinder<UPaperFlipbook> (TEXT("PaperFlipbook'/Game/Sprites/Ken_Sprites/Animations/A_ken_walk.A_ken_walk'")).Object;