UPDATE: Solved, but could you please tell me why doing this problem was occurring? I really want to learn why these things happen to become a better UE4 C++ programmer.
Hi, bit of a strange issue I’m having here. If I drag my AItemAmmoBox class onto the scene, it shows an ammo box model as expected. However, if I drag my AItemVaccine onto the scene, it also shows the ammo box model.
I can’t begin to imagine what could be causing it, so the only thing I can suspect is that because both are implementations of AItem, they both somehow affect a single AItem object when setting their meshes, so the last one to do so ends up setting its mesh for all of the others.
Here’s my code:
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Item.h"
#include "ItemVaccine.generated.h"
UCLASS()
class EFFIGY_API AItemVaccine : public AItem
{
GENERATED_BODY()
public:
AItemVaccine();
};
#include "ItemVaccine.h"
AItemVaccine::AItemVaccine()
{
Super::InitItem("StaticMesh'/Game/Items/Models/ItemVaccine/ItemVaccine.ItemVaccine'");
}
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Item.h"
#include "ItemAmmoBox.generated.h"
UCLASS()
class EFFIGY_API AItemAmmoBox : public AItem
{
GENERATED_BODY()
public:
AItemAmmoBox();
};
#include "ItemAmmoBox.h"
AItemAmmoBox::AItemAmmoBox()
{
Super::InitItem("StaticMesh'/Game/Items/Models/ItemAmmoBox/ItemAmmoBox.ItemAmmoBox'");
}
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Item.generated.h"
UCLASS()
class EFFIGY_API AItem : public AActor
{
GENERATED_BODY()
protected:
void InitItem(char* path);
};
#include "Item.h"
#include "UObject/ConstructorHelpers.h"
#include "Components/StaticMeshComponent.h"
void AItem::InitItem(char* path)
{
class UStaticMeshComponent* BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BaseMesh"));
BaseMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> BaseMeshAsset(*FString(path));
BaseMesh->SetStaticMesh(BaseMeshAsset.Object);
BaseMesh->SetCollisionObjectType(ECollisionChannel::ECC_GameTraceChannel2);
}
FIXED: Removed ‘static’ from the FObjectFinder object by trial and error. Why was that causing this issue?