Hi guys, I have two issues with TArray in UE4.16. TArray is always being collected by GC, despite declaring as UPROPERTY(). Secondly, when I try to pick object up through overlap function another time the number of TArray is increased again. However, as soon as the overlap function is done the TArray will be emptied. This the code
PickItem.h
#pragma once
#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “Components/BoxComponent.h”
#include “PickItem.generated.h”
UCLASS()
class INVENTORY_API APickItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor’s properties
APickItem();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category=Storage) USceneComponent *_RootS;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Storage) UStaticMeshComponent *_MeshOfItem;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Storage) UBoxComponent _Container;
UPROPERTY(EditAnywhere)
TArray<APickItem> _Inventory;
FRotator _Rotation;
UFUNCTION() void OnOverlapBegin( UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION() void OnOverlapEnd( UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
void PrintOut();
void HideTheItem(bool CheckVisibillity);
bool bStatuteOfOverLap;
};
PickItem.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include “PickItem.h”
#include"Components/StaticMeshComponent.h"
#include"Engine.h"
// Sets default values
APickItem::APickItem()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
this->_RootS = CreateDefaultSubobject(TEXT(“SenceCo”));
this->_RootS = RootComponent;
this->_MeshOfItem = CreateDefaultSubobject(TEXT(“Mesh”));
this->_MeshOfItem->AttachToComponent(this->_RootS, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
this->_Container = CreateDefaultSubobject(TEXT(“Holder”));
this->_Container->SetWorldScale3D(FVector(2, 2, 2));
this->_Container->AttachToComponent(_RootS, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
this->_Container->bGenerateOverlapEvents = true;
this->_Container->OnComponentBeginOverlap.AddDynamic(this, &APickItem::OnOverlapBegin);
this->_Container->OnComponentEndOverlap.AddDynamic(this, &APickItem::OnOverlapEnd);
this->_Rotation = FRotator(0, 0, 0);
}
// Called when the game starts or when spawned
void APickItem::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APickItem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
this->_MeshOfItem->AddLocalRotation(this->_Rotation*DeltaTime);
PrintOut();
}
UFUNCTION() void APickItem::OnOverlapBegin(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
if (OtherActor != NULL && OtherComp != NULL&& OtherActor != this)
{
this->bStatuteOfOverLap = true;
APickItem* ite = NewObject(this, FName(“ball”));
this->_Inventory.Add(ite);
GEngine->AddOnScreenDebugMessage(1, 3, FColor::Red, *(FString().FromInt(this->_Inventory.Num())));
}
}
UFUNCTION() void APickItem::OnOverlapEnd(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex)
{
if (OtherActor != NULL && OtherComp != NULL&& OtherActor != this)
{
this->bStatuteOfOverLap = false;
int n = this->_Inventory.Num();
}
}
void APickItem::PrintOut()
{
}
void APickItem::HideTheItem(bool CheckVisibillity)
{
}