Download

Can't set mesh at runtime with SetStaticMesh based on default actor mesh?

So I am trying to set the mesh of an object to the default mesh of an actor, like so:



//mould selection
void ACPP_SmeltingPot::SelectPrevMould()
{
	CurrentlySelectedMould = (CurrentlySelectedMould-1) < 0 ? Moulds.Num()-1 : CurrentlySelectedMould - 1;
	UpdateMould();
}
void ACPP_SmeltingPot::SelectNextMould()
{
	CurrentlySelectedMould = (CurrentlySelectedMould + 1) % Moulds.Num();
	UpdateMould();
}

void ACPP_SmeltingPot::UpdateMould()
{
	MouldMesh->SetStaticMesh((Moulds[CurrentlySelectedMould].GetDefaultObject())->mesh->StaticMesh);

}


where Moulds is defined as follows within ACPP_SmeltingPot:


       UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
	TArray<TSubclassOf<ACPP_ItemMould>> Moulds;

	//currently selected mould index
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
	int CurrentlySelectedMould


CPP_ItemMould.h



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "ItemBase.h"
#include "CPP_FinishedProduct.h"
#include "CPP_ItemMold.generated.h"

/**
 * 
 */
UCLASS()
class SWORDSMITH_API ACPP_ItemMould : public AItemBase
{
	GENERATED_BODY()
public:

	ACPP_ItemMould();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
	TSubclassOf<class ACPP_FinishedProduct> createdItem;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
	float MetalRequired;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
		EMetalType MetalUsed;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
		bool bIsFull;

	bool bIsComplete;
	bool bNeedsDunking;
		
}


ItemBase.h



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"

#include "ItemBase.generated.h"

UCLASS()
class SWORDSMITH_API AItemBase : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AItemBase();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;
	
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadWrite, Category = "Default")
		USceneComponent* root;
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadWrite, Category = "Default")
		UStaticMeshComponent* mesh;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "State")
		bool bIsGrabbed;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Smithing")
		bool bIsHeating;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Smithing")
		float Heat;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Default)
		EMetalType MetalType;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Default)
		float MetalAmount;

	void ReEnablePhysics();
	void SetMetal(EMetalType type);
	

};


For whatever reason this is not working, I’m guessing I’m using the GetDefaultObject() method incorrectly to access the mesh?

Also: if there is a better way of doing this, I would love to know :slight_smile:

Let me know if more information is needed. Thanks!