Hey so, im stuck in a slight trouble…
So, i have an array of my “item” class that works as the player inventory
class AProojectCharacter : public ACharacter
{
GENERATED_UCLASS_BODY()
virtual void Tick(float DeltaSeconds) OVERRIDE;
/** items inventory */
UPROPERTY(EditAnywhere, Category = Inventory)
TArray<class AProojectItem*> ItemInventory;
void PickUpItem(class AProojectItem* Item);
};
then the function that handles the “picking” of the items and their introduction to the array
#include "Prooject.h"
#include "ProojectItem.h"
#include "ProojectCharacter.h"
//////////////////////////////////////////////////////////////////////////
// AProojectCharacter
AProojectCharacter::AProojectCharacter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Set size for collision capsule
CapsuleComponent->InitCapsuleSize(42.f, 96.0f);
CameraSocket = "Head";
WeaponSocket = "Weapon";
// Create a follow camera
FollowCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FollowCamera"));
FollowCamera->AttachTo(Mesh, CameraSocket);
FollowCamera->RelativeLocation = Mesh->GetSocketLocation(CameraSocket);
}
void AProojectCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
FVector CamLoc;
FRotator CamRot;
Controller->GetPlayerViewPoint(CamLoc, CamRot); // Get the camera position and rotation
const FVector StartTrace = CamLoc; // trace start is the camera location
const FVector Direction = CamRot.Vector();
const FVector EndTrace = StartTrace + Direction * 200;
// Perform trace to retrieve hit info
FCollisionQueryParams TraceParams(FName(TEXT("WeaponTrace")), true, this);
TraceParams.bTraceAsyncScene = true;
TraceParams.bReturnPhysicalMaterial = true;
FHitResult Hit(ForceInit);
if (()->LineTraceSingle(Hit, StartTrace, EndTrace, ECC_WorldStatic, TraceParams))
{
AProojectItem* NewItem = Cast<AProojectItem>(Hit.GetActor());
if (NewItem)
{
PickUpItem(NewItem);
}
}
}
void AProojectCharacter::PickUpItem(class AProojectItem* Item)
{
if (Item)
{
ItemInventory.Add(Item);
Item->PickedUp();
ItemInventory[0]->Used(this); // only works if i DONT destroy the item ( event gets fired), else event never gets fired
}
}
Item Class
Header
#pragma once
#include "GameFramework/Actor.h"
#include "ProojectItem.generated.h"
/**
*
*/
UCLASS()
class AProojectItem : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FString ItemName;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
int32 Value;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Item)
TSubobjectPtr<class UStaticMeshComponent> Mesh;
void PickedUp();
UFUNCTION(BlueprintNativeEvent,Category = Item)
void Used(AProojectCharacter* OwnerP);
};
cpp
#include "Prooject.h"
#include "ProojectItem.h"
#include "ProojectCharacter.h"
AProojectItem::AProojectItem(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
}
void AProojectItem::PickedUp()
{
Destroy();
}
void AProojectItem::Used_Implementation(AProojectCharacter* OwnerP)
{
}
Now, i have an blueprint using as subclass the Item class and inside the blueprint i have
The event gets fired and actually works IF i dont Destroy the actor in the world
Now this is an issue for me becouse i need to keep a “copy” of said item in the ItemInventory array so ,Since i need to make use of it later ( Display it in the UI etc )
Im a bit confused becouse before destroying the actor im adding it to the ItemInventory array so shouldnt it be kept there instead of pointing to the World Version?