Need collision for a Pickup item

I’ve been trying to make a class for items that the player can pick up on the map, but when I run the the debugger the function I made to handle this is never touched. I’m still learning how to code for C++ so I’m sorry if the answer was actually obvious.

Edit: A better way to think of what I’m trying to make is something like the coins in Mario.
Here is the code from the .cpp file.
#include “TurnBasedGame.h”
#include “PickUp_Item.h”

// Sets default values
APickUp_Item::APickUp_Item()
{
 	// 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;

	Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
	Sphere->InitSphereRadius(200.0f);
	RootComponent = Sphere;
	Sphere->SetCollisionProfileName(TEXT("Item"));
	

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	Mesh->AttachParent = RootComponent;

	Skeleton = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeleton"));
	Skeleton->AttachParent = RootComponent;
}

// Called when the game starts or when spawned
void APickUp_Item::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void APickUp_Item::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

//Allows a character to pick up the item
void APickUp_Item::PickupItem(class APawn* Player)
{
	FTransform item = this->GetTransform();
	FTransform pawn = Player->GetTransform();
	if (Sphere->IsOverlappingActor(Player))
	{
		this->Destroy();
		pickedUp = true;
	}
	Skeleton->DestroyComponent();
}

From where are you calling PickupItem? Did you bind it to input?

What do you mean by “where are you calling PickupItem”? Because I made an actor based on this class and when a Pawn touches it it’s still there. I mainly just assumed that the function would automatically kick in when the Pawn touches the sphere. Was that a wrong assumption?

I don’t think I can help you any further with this specific problem. But I think you would learn a lot by following this tutorial:

And possibly by doing so comming up with a better solution to what you’re trying to accomplish.

Would it help if I said what I’m trying to make is something like the rings in Sonic games?

I’m not familiar with Sonic and its rings, but from what I saw in YouTube videos, it seems that you want to pick up your rings through collision. But the way you wrote the code, you going to need some kind of loop to endlessly check whether something is deemed to be picked up or not. And it’s not the most efficient way.

A better way is to provide a collision response when your player hits the the collision sphere around the ring.

AMyPickup::AMyPickup(const FObjectInitializer& oi)
 	: Super(oi)
{
    CollisionComp = oi.CreateDefaultSubobject<USphereComponent>(this, TEXT("CollisionComp"));
    CollisionComp->InitSphereRadius(100.0f);
    CollisionComp->OnComponentBeginOverlap.AddDynamic(this, &AMyPickup::OnBeginOverlap);
    CollisionComp->SetCollisionProfileName(FName(TEXT("OverlapAllDynamic")));
    
    RootComponent = CollisionComp;
}

void AMyPickup::OnBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
        // Only let the main character interact with the ring.
	if (OtherActor->IsA(AMyCharacter::StaticClass()))
	{
		AMyCharacter *my = Cast<AMyCharacter>(OtherActor);
		my->AddToRingInventory(<ring value for instance>)
                Destroy();
	}
}

Set the radius as large as necessary.
If the ring is set to be destroyed, setting any other value in that class (like pickedUp = true) is useless.
The same goes with any other destroy calls like Skeleton->DestroyComponent().
The ring, its mesh and any relevant value attached to it will disappear with Destroy().

And stop using this-> all the time. :slight_smile:

EDIT:

Don’t forget to wrap your OnBeginOverlap with UFUNCTION() in .h file.