Hello everybody, i apologize in advance for any grammatical errors i from poland and yet learn english.
So i have problem with collision in Unreal Engine 4, here code Item.h
UCLASS()
class INVENTORY_API AItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AItem();
float wielkosc;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FString ItemName;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
int32 Value;
UFUNCTION(BlueprintImplementableEvent, Category="DMG")
virtual void Used();
UFUNCTION(BlueprintImplementableEvent,Category="DMGS")
virtual void Dropped();
void ReceiveActorBeginOverlap(class AActor* OtherActor);
UPROPERTY(EditAnywhere)
USphereComponent* Mesh;
void PickedUp();
UPROPERTY(EditAnywhere)
UStaticMeshComponent* SphereVisual;
};
Item.cpp
AItem::AItem()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
Mesh = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = Mesh;
Mesh->SetSphereRadius(90.0f);
Mesh->SetCollisionProfileName(TEXT("Pawn"));
Mesh->SetSimulatePhysics(true);
OnActorBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);
SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
SphereVisual->AttachTo(RootComponent);
wielkosc = 0.8f;
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereVisualAsset.Succeeded())
{
SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
SphereVisual->SetWorldScale3D(FVector(wielkosc));
}
}
void AItem::ReceiveActorBeginOverlap(class AActor* OtherActor)
{
if (Mesh)
{
Mesh->DestroyComponent(); // physical item has been picked up, destroy its visible component
}
}
i have a lot of problem with there, i don’t know what i make bad… I try create collision from 9 days and i can’t.
That is afaik not true. “override” is only a c++11 hint for the compiler to check for an equal method in a baseclass which can be overriden, resulting in a compiler error if none can be found, and the “virtual” keyword has no effect if a ReceiveActorBeginOverlap method in a AItem-derived class does not exist. Nevertheless, it is better coding style.
To the topic:
You want to check if an actor overlaps with your item.
You can use events in blueprints, or C++ events with dynamic delegates or use overloaded methods in derived classes.
As you are implementing in c++, and deriving from AActor, the fastest way would be to overload a base-XYZOverlap-method.
So, in your code, I would suggest to rename “ReceiveActorBeginOverlap” to “NotifyActorBeginOverlap” and remove the “OnActorBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);” line.
Of course, the ‘virtual’ keyword is only preferred if the method is actually meant to be overridden in a deriving class - too often people will label functions virtual when they’re not actually intending for people to inherit from the class. IIRC you don’t need to mark a function virtual for it to override a base class method that is a virtual function.
AItem::AItem(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
Mesh = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("CollisionMeshComponent0"));
Mesh->Mobility = EComponentMobility::Movable;
Mesh->SetNotifyRigidBodyCollision(true);
Mesh->SetSphereRadius(90.0f);
Mesh->SetCollisionProfileName(TEXT("Pawn"));
Mesh->OnComponentBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);
Mesh->SetSimulatePhysics(true);
Mesh->bGenerateOverlapEvents= true;
RootComponent = Mesh;
SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
SphereVisual->AttachTo(RootComponent);
wielkosc = 0.8f;
//OnActorBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereVisualAsset.Succeeded())
{
SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
SphereVisual->SetWorldScale3D(FVector(wielkosc));
}
}
void AItem::ReceiveActorBeginOverlap(class AActor* OtherActor)
{
Destroy();
}
.H
UCLASS()
class INVENTORY_API AItem : public AActor
{
GENERATED_BODY()
DECLARE_DELEGATE(Collision);
public:
// Sets default values for this actor's properties
AItem(const FObjectInitializer& ObjectInitializer);
float wielkosc;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
FString ItemName;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
int32 Value;
UFUNCTION(BlueprintImplementableEvent, Category="DMG")
virtual void Used();
UFUNCTION(BlueprintImplementableEvent,Category="DMGS")
virtual void Dropped();
void ReceiveActorBeginOverlap(class AActor* OtherActor);
UPROPERTY(EditAnywhere)
USphereComponent* Mesh;
void PickedUp();
UPROPERTY(EditAnywhere)
UStaticMeshComponent* SphereVisual;
};
I’m still not sure what the problem is - you say it isn’t working, but I don’t know WHAT isn’t working. Are you getting errors? Is it crashing? Is your overlap function not being called?