I wanted to make an inventory, and in order to separate the presentation logic and the internal part, I decided to make a separate class for the view and the item itself in the inventory.This text will be hidden
ItemBaseView.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ItemBaseView.generated.h"
class UItemBase;
UCLASS()
class THIRSTTOLIVE_API AItemBaseView : public AActor
{
GENERATED_BODY()
public:
AItemBaseView(TSubclassOf<UItemBase> ItemToCreate);
AItemBaseView();
protected:
virtual void BeginPlay() override;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Base Item Data")
UStaticMeshComponent* PickupMesh;
public:
virtual void Tick(float DeltaTime) override;
protected:
UPROPERTY(EditDefaultsOnly,BlueprintReadOnly, Category = "Item", meta = (AllowPrivateAccess))
UItemBase* Item;
};
ItemBaseView.cpp
#include "Inventory/ItemsRepresentation/ItemBaseView.h"
#include "ItemBase.h"
AItemBaseView::AItemBaseView(TSubclassOf<UItemBase> ItemToCreate)
{
PrimaryActorTick.bCanEverTick = true;
PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>("StaticMesh");
Item = NewObject<UItemBase>(ItemToCreate);
}
AItemBaseView::AItemBaseView()
{
}
void AItemBaseView::BeginPlay()
{
Super::BeginPlay();
}
void AItemBaseView::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
AutoPickupView.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AutoPickup.h"
#include "Inventory/ItemsRepresentation/ItemBaseView.h"
#include "AutoPickupView.generated.h"
class UInventoryComponent;
class USphereComponent;
UCLASS()
class THIRSTTOLIVE_API AAutoPickupView : public AItemBaseView
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision, meta = (AllowPrivateAccess))
USphereComponent* SphereCollision;
public:
AAutoPickupView(TSubclassOf<UItemBase> ItemToCreate);
AAutoPickupView();
protected:
UPROPERTY()
UAutoPickup* AutoPickupItem;
virtual void BeginPlay() override;
public:
UFUNCTION(BlueprintNativeEvent)
void Collect(UInventoryComponent* InventoryComponent);
virtual void Collect_Implementation(UInventoryComponent* InventoryComponent);
UFUNCTION()
virtual void OnOverlapBegin(class UPrimitiveComponent* newComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "Inventory/ItemsRepresentation/AutoPickupView.h"
#include "AutoPickup.h"
#include "InventoryComponent.h"
#include "ThirstToLiveMainCharacter.h"
#include "Components/SphereComponent.h"
AAutoPickupView::AAutoPickupView(TSubclassOf<UItemBase> ItemToCreate) : AItemBaseView(ItemToCreate)
{
}
AAutoPickupView::AAutoPickupView() : AItemBaseView(UAutoPickup::StaticClass())
{
SphereCollision = CreateDefaultSubobject<USphereComponent>("Sphere Collision");
SphereCollision->SetSphereRadius(32);
AutoPickupItem = Cast<UAutoPickup>(Item);
}
void AAutoPickupView::BeginPlay()
{
Super::BeginPlay();
SphereCollision->OnComponentBeginOverlap.AddDynamic(this, &AAutoPickupView::OnOverlapBegin);
}
void AAutoPickupView::Collect_Implementation(UInventoryComponent* InventoryComponent)
{
if (InventoryComponent->TryAddItemToInventory(AutoPickupItem))
{
SetActorHiddenInGame(true);
SetActorEnableCollision(false);
SetActorTickEnabled(false);
}
}
void AAutoPickupView::OnOverlapBegin(UPrimitiveComponent* newComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
bool bFromSweep, const FHitResult& SweepResult)
{
auto const ATLCharacter = Cast<AThirstToLiveMainCharacter>(OtherActor);
if (ATLCharacter)
{
Collect(ATLCharacter->GetInventory());
}
}
I tried to make the created class pass the required object class as a parameter to the parent constructor, and as a result, for each representation of the object, an internal representation class was created accordingly.
Assembly completed. But when starting the engine read access violation