How do I use SetStaticMesh and SetMaterial?

In my header file I have:

            /** Static Mesh Comp, Set In BP Default Properties */
	UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "StaticMesh Components")
	TSubobjectPtr<UStaticMeshComponent> Block;

in my .cpp file I have:

Block = PCIP.CreateAbstractDefaultSubobject(this, TEXT("Block"));

static ConstructorHelpers::FObjectFinder StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

static ConstructorHelpers::FObjectFinder Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

Block->SetStaticMesh(StaticMesh.Object);
Block->SetMaterial(0, Material_Blue.Object);

The mesh and the material are never applied.

Yet It shows up fine in the blueprint and I can do all of this in blueprint. What did I do wrong?

Never mind, got it working.

if you have a correct answer that you found yourself, usually one posts the answer for it, as this way other people with the same problem can use it.

This is the correct answer. I made a mistake by in the editor, not in code.

well i couldnt use it as it was.

you called template functions without specializing the type.

Using “Block” as a variable name results in problems for me as well, leading to non compiling.

yes that is copy paste error, I miss the template type when I copy pasted it.

Using “Block” as a template name???

for some weird reason i couldnt use “Block” as name for the staticmeshcomponent, defined in the header file. as the VS2013 compiler complained about it !? :smiley:

Maybe its obvious but for me this is not working.

.h File:

UCLASS()
class ASolidBlock : public AActor
{
	GENERATED_UCLASS_BODY()
	virtual void BeginPlay() OVERRIDE;

	// static mesh
	TSubobjectPtr<UStaticMeshComponent> Block;
	
};

.cpp file:

ASolidBlock::ASolidBlock(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	Block = PCIP.CreateAbstractDefaultSubobject(this, TEXT("Block"));
	//static ConstructorHelpers::FObjectFinder <UStaticMeshComponent> StaticMesh("StaticMesh'/Game/SolidBlock.uasset'");
	//Block->SetStaticMesh(StaticMesh.Object);
}

void ASolidBlock::BeginPlay()
{
	GEngine->AddOnScreenDebugMessage(-1, 4, FColor::Blue, TEXT("SOLIDBLOCK BEGINPLAY"));
}

This leads to a compile error. The Line:

Block = PCIP.CreateAbstractDefaultSubobject(this, TEXT("Block"));

give the error:
error C2783: ‘TSubobjectPtrConstructor FPostConstructInitializeProperties::CreateAbstractDefaultSubobject(UObject *,FName,bool) const’ : could not deduce template argument for ‘TReturnType’

i also read in

about someone who wants to spawn meshes and has problems. would be great you could help :stuck_out_tongue: Maybe its too obvious and i dont see it :smiley:

The error is in the line:

Block = PCIP.CreateAbstractDefaultSubobject(this, TEXT("Block"));

and this should be:
Block = PCIP.CreateAbstractDefaultSubobject(this, TEXT(“Block”));

it worked for me.

The code above didn’t work for me. The line

Block = PCIP.CreateAbstractDefaultSubobject(this, TEXT("Block")); 

needs to be

Block = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("Block")); 

And the lines:

static ConstructorHelpers::FObjectFinder StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));
 
static ConstructorHelpers::FObjectFinder Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

should be

static ConstructorHelpers::FObjectFinder <UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));
 
static ConstructorHelpers::FObjectFinder <UMaterial>Material_Blue(TEXT("MaterialInstanceConstant'/Game/Materials/M_BaseColor_inst_Blue.M_BaseColor_inst_Blue'"));

The wrong formatting in the answerhub is because of the numbered list? i think it is.
if you choose “code sample” this should display the code as you paste it with right angle brackets like

<UStaticMeshComponent>

otherwise they get deleted.

btw, this doesn’t work anymore due to changes in the engine. Every example I found on the internet was wrong. You start thinking you are going crazy…

Here are the fixes:

Replace this:
TSubobjectPtr Block;

With this:
UStaticMeshComponent* Block;

Add this constructor in the header:
ASolidBlock(const class FObjectInitializer& PCIP);

Replace this:
ASolidBlock::ASolidBlock(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)

with this:
ASolidBlock::ASolidBlock(const class FObjectInitializer& PCIP) : Super(PCIP) {

That should do the trick.

1 Like