So I’m trying to create a preview mesh of an object I’m spawning, so I’m trying to update the Static mesh of one of my actors StaticMeshComponents using SetStaticMesh in tandem with GetDefaultObject(), however it doesn’t seem to work.
In CPP_SmeltingCup.cpp
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);
}
I’m binding the SelectPrev/NextMould() to two buttons in my scene to access the functions, but the mesh is not updating when UpdateMould() is called.
MouldMesh is just a StaticMeshComponent, Moulds and relevant data are defined below:
Moulds in CPP_SmeltingPot is declared as:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Default")
TArray<TSubclassOf<ACPP_ItemMold>> Moulds;
I create a blueprint of CPP_SmeltingPot that I use in my scene and add several child blueprints(with differing static meshes) of ACPP_ItemMould to my Moulds array. Am I correct in assuming that GetDefaultObject() should return a different object when accessing each of these blueprints?
For good measure here is ItemMold and ItemBase:
CPP_ItemMold.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_ItemMold : public AItemBase
{
GENERATED_BODY()
public:
ACPP_ItemMold();
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 "SwordSmithGameMode.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);
};
I’m unsure what’s wrong; any help is appreciated. Let me know if any other information could be useful, thanks!