Hello,(and sorry for the other post on this function)
I try to make a collactable item in c++ and i can’t get the function to work.
I try to declare it but i don’t know how it work
this is my Item.cpp
#include "Components/BoxComponent.h"
#include "Item.h"
#include "Engine.h"
// Sets default values
AItem::AItem()
{
// 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;
TBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Box"));
TBox->SetGenerateOverlapEvents = true;
TBox->OnComponentBeginOverlap.AddDynamic(this, &AItem::TriggerEnter);
TBox->OnComponentEndOverlap.AddDynamic(this, &AItem::TriggerExit);
RootComponent = TBox;
SM_TBox = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Box Mesh"));
SM_TBox->AttachTo(RootComponent);
}
void AItem::GetPlayer(AActor* Player)
{
MyPLayerController = Cast<AItemMenuCharacter>(Player);
}
// Called when the game starts or when spawned
void AItem::BeginPlay()
{
Super::BeginPlay();
}
AItem::DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorBeginOverlapSignature, AActor *, MyPLayerController, AActor *, OtherActor)
{
}
void AItem::Pickup()
{
MyPLayerController->Inventory.Add(*ItemName);
GEngine->AddOnScreenDebugMessage(-1, 5.f , FColor::Green, TEXT("You Picked the item up !"));
Destroy();
}
// Called every frame
void AItem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (MyPLayerController != NULL) //Nullcheck player controller
{
if (MyPLayerController->bIsPickingUp && bItemIsWithinRange)
{
Pickup();
}
}
}
void AItem::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
bItemIsWithinRange = true;
GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Green, FString::Printf(TEXT("Press E to Pickup %s"), *ItemName));
GetPlayer(OtherActor);
}
void AItem::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
bItemIsWithinRange = false;
}
ad my item.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ItemMenuCharacter.h"
#include "Item.generated.h"
UCLASS()
class ITEMMENU_API AItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AItem();
UShapeComponent* TBox;
UPROPERTY(editAnywhere)
UStaticMeshComponent* SM_TBox;
AItemMenuCharacter* MyPLayerController;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorBeginOverlapSignature, AActor*, MyPLayerController, AActor*, OtherActor);
UPROPERTY(editAnywhere)
FString ItemName = FString(TEXT(""));
void Pickup();
void GetPlayer(AActor* Player);
bool bItemIsWithinRange = false;
UFUNCTION()
void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
UFUNCTION()
void TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
thank you